0

I have few questions in server side.

I have more than 50 API.

Need to check every API (GET,POST) method the following things.

  1. Validate the input is valid json.
  2. Input data need to check the some of the key name with data.

For example 1:

{
        "name": "<city name1>",
        "status": 1,
        "id" : 3,
        "code" : 5,
        "abcd" : "44",
      ---------------
        ------------
}

In this above Input I need to check few key are mantory. eg: name, code

Example 2:

{
    "country": [
        {
            "state": [
                {
                    "name": "<city name1>",
                    "status": 1
                }, {
                    "name": "<city name2>",
                    "status": 2
                }, {
                    "name": "<city name3>",
                    "status": 3
                }
            ],
            "name": "<state Name1>"
        }, {
            "state": [
                {
                    "name": "<city name1>",
                    "status": 1
                }, {
                    "name": "<city name2>",
                    "status": 2
                }, {
                    "name": "<city name3>",
                    "status": 3
                }
            ],
            "name": "<state Name2>"
        }
    ]
}

In above example I need to check the state and city > name is mandatory.

I checked the packages but not get suitable one for me.

Have any packages for check the json validation?

RSKMR
  • 1,702
  • 1
  • 22
  • 54

2 Answers2

1

I use validator. It has many functions like isEmail, isAlphaNumeric etc...

It also supports sanitization. Samples and tests in the repo.

To check if key exists, you can use standard hasOwnProperty

Check if a key exists inside a json object

Those options will be fast and offer the ability to craft very specific custom checking and better error messages back to the consumer.

Another option is json schema. Might be faster peformance wise to simply check code (above) but with json schema you could create common code in your api that loads schema files for each api so may lead to less code.

An example lib.

https://www.npmjs.com/package/jsonschema

So, it's a balance of more/less code, performance and how custom you want your checking and error messages to be.

Community
  • 1
  • 1
bryanmac
  • 37,512
  • 9
  • 85
  • 95
  • I checked package . but I am unable to validation. Can you please write with example. It will helpful for me lot. – RSKMR May 10 '16 at 13:07
  • This plugin will check the key is exist or not? – RSKMR May 10 '16 at 13:08
  • I updated the post. Those should get you going in the right direction. Both have docs and examples. – bryanmac May 10 '16 at 13:16
  • Thanks. How to check the input data is valid json format ? – RSKMR May 10 '16 at 13:24
  • If you're using express, bodyparser ensures that req.body is json. http://stackoverflow.com/questions/10005939/how-to-consume-json-post-data-in-an-express-application – bryanmac May 11 '16 at 01:21
0

If you esthete please try this solution https://github.com/askucher/ftjs.

npm install ftjs

You can define type definition file (example)

#SimpleTypes

String         : /.?/

Integer        : Global.Integer

Int            : Integer

Boolean        : Global.Boolean

Bool           : Boolean

Double         : Global.Double

Numeric        : Double | Integer

Null           : Global.Null

Undefined      : Global.Undefined

Email          : /^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i

Strings        : [String]

#TypeExtensions

String...
Min min        : /^.{#{min},}$/
Max max        : /^.{,#{max}}$/
Range min max  : /^.{#{min},#{max}}$/

Integer...
Min min        : @ >= min
Max max        : @ <= max


#Enums

Status         : "active" | "inactive"

Missing        : Null | Undefined


#ComplexTypes

User
------------
email          : Email
picture        : String
firstname      : String Range(5,20)
lastname       : String Min(5) Max(20)
status         : Status
bio            : String | Missing
tags           : [String]

 var types = require("ftjs");
   var fs = require("fs");

   var validate = types({
      System: fs.readFileSync("./examples/System.ft").toString("utf8")
   });

   var user = {
      email: 'a.stegno@gmail.com',
      picture: 'http://some-website.com/picture.png',
      firstname: 'Andrey',
      lastname: 'Test',
      status: 'active',
      bio: 'Ho',
      tags: ["user"]
   };

   validate("System.User", user); //true
Andrey Stehno
  • 297
  • 1
  • 4
  • 7