Pregunta

Looking for a library that could validate input like, for example:

{ points: array of { x: positive and < 20, y: positive and < 15 } }

Preferably working on both server and client side and returning a boolean or throwing an exception.

What I explicitly don't need is string or form validation, I just want to check if JSON sent by client is safe for processing without a ton of dispersed checks as I go.

¿Fue útil?

Otros consejos

You could also try Skematic.

Data structure and rule validation engine. Robust schema for JS objects.

It lets you design data models, then format and validate data against those models. Works in browser (Skematic global) or Node/io.js.

To solve your request, something basic like this should work:

// -- Define a simple data structure
var YourData = {
  points: {
    type:'array', 
    default: [],
    schema: {
      x: { type: 'number', required: true, rules: { min:0, max:20 } },
      y: { type: 'number', required: true, rules: { min:0, max:15 } }
    }
  }
};

// -- Validate an object
Skematic.validate( YourData, {points:[ {x:-1,y:10} ]} );
// {valid:false, 
//  errors:{
//    points: {0:{x:['Failed: min']}}
// }}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top