2

We have a web app containing several forms, the majority of them with a few dozen fields. Right now we create json objects with the data entered in the forms and send those objects to REST services which, in turn, persist that data into a NoSQL database (MongoDB).

Currently, we´re using jquery-validator to perform validation at client side. However, we require to validate the data using the same JSONSchema (both on client and server sides).

Is there any way to use jquery-validate (or other similar plugin) to do this? I'm thinking maybe there is a way to replace the "rules" defined for jquery-validate with the JSONSchema file.

I've tried using "Alpaca" which can both generate and validate the forms using JSONSchema, but the problem I've come across with Alpaca is that it's not capable of handling nested json objects, which I have a lot of, it only seems to work with simple json structures.

Any help would be greatly appreciated.

UPDATE

Let's say I have the following jquery-validate rules (fairly simple for example purposes):

$('#formId').validate({
    rules: {
        firstField: {
            required: true
        },
        secondField: {
            required: true,
            maxlength: 80
        }
    }
});

And I have the following JSONSchema file:

{
    "$schema": "http://json-schema.org/draft-03/schema#",
    "type": "object",
    "additionalProperties": false,
    "description": "MyJsonSchema",
    "properties": {
            "firstField": {
                "description": "some text here",
                "type": "string",
                "maxLength": 15,
                "required": true
            },
            "secondField": {
                "description": "some text here",
                "type": "string",
                "maxLength": 80,
                "required": true
            }
        }
    }

What I would like to do is find a way to replace the "rules" from the jquery-validate plugin and instead just read the JSONSchema properties, so that I have the validation rules for all my forms in the JSONSchema files (which I can use for client side and server side validation) instead of coding the rules for every form.

Is there any way to do that?

As I've said before, using something like AlpacaJS you can render and validate the forms from a JSONSchema file, but I haven't being able to use Alpaca with more complex json structures.

  • You're going to have to show some code examples and ask a more specific question. As it stands, your question is too broad. – Sparky Jan 03 '14 at 16:41
  • Easiest way is possibly to [extract the form data as an object](http://stackoverflow.com/questions/2403179/how-to-get-form-data-as-a-object-in-jquery) - you can then validate that using any JSON Schema library you choose. – cloudfeet Jan 17 '14 at 18:29

1 Answers1

1

There are several JSON Schema validators in JavaScript. Se the JSON Schema Software page.

Missing on that page is JSON Form, which can both generate the form and validate it (using JSV).

fiddur
  • 1,768
  • 15
  • 11