문제

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.

도움이 되었습니까?

다른 팁

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']}}
// }}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top