1
votes

I'm attempting to move an existing (and working) client-side jQuery validation schema to JSONSchema to allow myself to validate arbitrary JSON on both the client and server.

My application is essentially a bunch of gigantic forms with lots of complex logic determining which questions should be asked based on the user's response to other questions. The forms each have over 200 fields.

Right now I'm only doing client-side validation and that works well about 99% of the time. Browser issues have cropped up on a few occasions, but nothing catastrophic. That being said, I want to do server-side validation (!).

After reading the JSONSchema draft and browsing around some of the v3 implementations, it seems like I might lose some of the more complex rules that my application has come to depend upon. I want to be sure that I'm not missing something before moving too far in any direction.

Some examples:

"If x == 10, then y is required, otherwise it's optional"

10 could be a literal value, an enum, etc., but I need to be able to reference another field in the same structure and guarantee it's value not only exists, but is equivalent to a specific type / value.

I think this is addressed in this thread on the JSONSchema list.

"If x = today's date, and y = tomorrow's date, then x > y"

This logic will be used to ensure that the "from" date comes before the "to" date.

From what I can see there's nothing like this and the only way I can see doing it is passing in a freshly eval-ed chunk of JSON as the schema.

The closest thing I've found to meet the above needs is CERNY.

If I'm barking up the wrong tree, please let me know. I'm also looked into running backbone.js on both the client and server.

tl;dr;

I want to maintain one set of validation rules for large and complex forms and apply these validation rules to arbitrary JSON documents on both the client and server side.

1

1 Answers

0
votes

there is many tricks but not all of them are possible. For example if x == 10 then y is required can be achieved with something like (draft 3):

"type":[
{"properties":{"x":{"enum":[10]}, "y":{"required":true}}},
{"properties":{"x":{"disallow":[{"enum":[10]}]}}}
]

Let's say, it's possible but very tricky… a schema is basically supposed to validate the structure, not it's content (even if there is few properties for this)

Another possible way I personally do like is to "extend" the current validation graph with an external url based schema. The idea is to send parameters of the current document on an url which one will return a relevant schema according to those parameters.

Example:

{
"extends":{"$ref":"http://checkCustomValidity/{x}/{y}/"};
}

Where at "runtime" the schema sent back could be a {"disallow":"any"} if not allowed or {} if ok

This is useful as the url can be both used for the client and server side (your client will not be completely standalone but in some cases, you just cannot)

A real life usage for this is in cases where it is obliged to use a remote service. For example if I do have to check if my nickname is already used on the server during the registration. I code a server side web service answering to the request path: http://www.server.com/isNicknameUsed/{nickname}