0

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.

1
  • json is always safe.look for jsonschema onnpm. Commented May 11, 2014 at 10:50

2 Answers 2

1

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']}}
// }}
Sign up to request clarification or add additional context in comments.

Comments

0

Never mind, wrote it myself.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.