12

I need to validate some object in my NodeJS app. I have already used an awesome library express-validator, it works perfectly, but now I need to validate different object, not only requests and as far as express validator leverages validator library, that in turn doesn't support types other than the string type.

I have found different variants like Jsonschema, Ajv

They offer great features, but I need to be able to set error message and than just catch an exception or parse it from return object. Like that

 var schema = {
    "id": "/SimplePerson",
    "type": "object",
    "properties": {
      "name": {"type": "string", "error": "A name should be provided"},
      "address": {"$ref": "/SimpleAddress"},
      "votes": {"type": "integer", "minimum": 1}
    }
  };

So I can set an error message for every property.

Is there any existing solution to achieve this functionality ?

POSSIBLE SOLUTION

I have found a great library JSEN It provides necessary features.

5 Answers 5

10

Three powerful and popular libraries you can use for JSON validation are

AJV: https://github.com/epoberezkin/ajv

JOI: https://github.com/hapijs/joi

JSON validator: https://github.com/tdegrunt/jsonschema

All of these libraries allow you to validate different data types, do conditional validation, as well as set custom error messages.

Sign up to request clarification or add additional context in comments.

Comments

6

One solution is to use Joi library : https://github.com/hapijs/joi

This library is well maintained, used and offer lots of flexibility and possible actions.

Example :

const Joi = require('joi');

const schema = Joi.object().keys({
    name: Joi.string().error(new Error('A name should be provided')),
    address: Joi.ref('$SimpleAddress'),
    votes: Joi.number().min(1),
});

// Return result.
const result = Joi.validate(yourObject, schema);

Comments

1

I use Json Pattern Validator

 npm install jpv --save

usage

  const jpv = require('jpv');

  // your json object
  var json = {
      status: "OK",
      id: 123,
      type: {}
  }

  // validation pattern
  var pattern = {
      status: /OK/i,
      id: '(number)',
      type: '(object)'
  };

  var result = jpv.validate( json , pattern)

Comments

1

You can also try nonvalid, a library that supports callback-based validation with custom checks and errors (disclaimer: it is written by me).

Comments

1

I'm about to embark on validation of JSON submissions to my web service and will be using tcomb-validation. It's a lightweight alternative to JSON schema and is based on type combinators.

Example of 'intersections':

var t = require('tcomb-validation');
var Min = t.refinement(t.String, function (s) { return s.length > 2; }, 'Min');
var Max = t.refinement(t.String, function (s) { return s.length < 5; }, 'Max');
var MinMax = t.intersection([Min, Max], 'MinMax');

MinMax.is('abc'); // => true
MinMax.is('a'); // => false
MinMax.is('abcde'); // => false

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.