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?

2
  • How you have write you apis. Using any framework like express or you used simple http module Commented May 10, 2016 at 13:05
  • you can search for json schema too json-schema.org. May help. Commented May 10, 2016 at 13:15

2 Answers 2

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.

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

5 Comments

I checked package . but I am unable to validation. Can you please write with example. It will helpful for me lot.
This plugin will check the key is exist or not?
I updated the post. Those should get you going in the right direction. Both have docs and examples.
Thanks. How to check the input data is valid json format ?
If you're using express, bodyparser ensures that req.body is json. stackoverflow.com/questions/10005939/…
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: '[email protected]',
      picture: 'http://some-website.com/picture.png',
      firstname: 'Andrey',
      lastname: 'Test',
      status: 'active',
      bio: 'Ho',
      tags: ["user"]
   };

   validate("System.User", user); //true

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.