NodeJS validation library for json objects
Asked Answered
E

5

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.

Embryo answered 25/6, 2017 at 12:50 Comment(0)
M
8

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.

Massachusetts answered 15/3, 2020 at 22:0 Comment(0)
V
5

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);
Voice answered 25/6, 2017 at 20:30 Comment(0)
A
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)
Alternate answered 29/3, 2018 at 20:43 Comment(0)
Q
1

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

Queri answered 25/12, 2019 at 14:4 Comment(0)
G
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
Genesia answered 15/3, 2020 at 21:52 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.