Search code examples
angularjsvalidationattributesjsonschemaclient-side-validation

Validate one attribute of an object using json schema


I'm currenty using tv4 (json-schema validator) to validate (client-side) an object with a json-schema, which worked pretty well.

But when I'm changing one value of an attribute, I want that this value is the only one, which should be validated.

For example I have a user:

var user = { Name: 'Username', Age: 20 };

And the schema:

{
    "title": "UserValidation",
    "type": "object",
    "properties": {
        "Name": {
            "minLength": 4
        },
        "Age": {
            "type": "int"
        }
    }
}

And when I only change the name, I don't want to check, if the attribute "Age" is valid.

Reason: When I have a big object (with sub-models etc.) it will validate all properties. And I don't know if it's gonna be a performance problem (some day...).

  • Is this a possible case with schema-validation?
  • Or are there better ways to validate objects?

I'm using AngularJS btw.

Best regards and thanks.


Solution

  • You don't have a performance problem until you have a performance problem. :)

    There's no technical reason a partial-update validator like that shouldn't exist (I know of at least one, but it's not exactly speedy in the first place). There are also faster "from-scratch" validators (some with benchmarks), including some with very similar APIs/error-reports, so if in the future you do start seeing performance problems then you could switch then without much fuss.

    However, given that this is happening client-side (so you don't have scaling problems with more users), I wouldn't worry about it yet.