user1575921
user1575921

Reputation: 1088

validate post json data server side

I use javascript nodejs as server side , each time get post from client side, I will validate the data, make sure data structure each key/value exist, so I do something like below.

I wonder is there any better idea to do this, maybe much readable,
because I put each level into one if(..) and can't change line like json/javascript object so it is long and hard to maintain .

usually people how to do this?
build a correct json object strcture, and compare with post data ?

any suggestion will be appreciate

correct data should be like:

request.data = {
  "user": {
    "status": "int",
    "role": {
      "role": "int"
    },
    "information": {
      "email": "text",
      "password": "text",
      "username": "text",
      "country": "text",
      ... a lot ....
    }
  },
}

server side validate

    var validateRequest = 'true';
    if (typeof request.data.user == 'undefined') {
      validateRequest = 'false';
    } else {
      if (typeof request.data.user.status == 'undefined' || typeof request.data.user.role == 'undefined' || typeof request.data.user.information == 'undefined') {
        validateRequest = 'false';
      } else {
        if (typeof request.data.user.role == 'undefined' || typeof request.data.user.information.email == 'undefined'  ... ) {
           validateRequest = 'false';
        }
      }
    } 

    if (validateRequest != 'false') {
      // validate data type

      // use data crud

    }

update

I try to set a correct json structure in server side, and compare with post, I found some json diff module but can't find how to correct usage only compare key

var a = {
  "information": {
    "email": "text",
    "password": "text"
  }
}

var b = {
  "information": {
    // "email": "text",
    "password": "text"
  }
}
console.log(jsondiffpatch.diff(a, b));
I only know if data not different will get undefined  
if b remove "email"   
get this in console ?? ¨ information: ¨ email: ÿ 'text', 0, 0 ¦ ¼ ¼
if b email val = textt 
get this information: ¨ email: ÿ 'text', 'textt' ¦ ¼ ¼

Upvotes: 1

Views: 232

Answers (2)

gnerkus
gnerkus

Reputation: 12019

You can utilize the jsondiffpatch module to observe differences in the structure of the request.

To obtain the diff function from the jsondiffpatch module:

var diff = require('jsondiffpatch').diff;

To validate the request, you can compare the request to a template. For example, a template is defined below:

var requestTemplate = {
  "user": {
    "status": "int",
    "role": {
      "role": "int"
    },
    "information": {
      "email": "text",
      "password": "text",
      "username": "text",
      "country": "text",
      ... a lot ....
    }
  },
}

To compare the request to the template, find the difference between the two:

var delta = diff(request.data, requestTemplate);

Search the delta for any property with a value of this signature:

'email': ['text', 0, 0]

This signature indicates that the property was deleted. For example, the value above indicates that the request.data object does not have an email property. The absence of this property means that the request is invalid.

You could define a function to perform the validation like so:

function validateRequest(template, requestData) {
  var delta = diff(requestData, template);

  // Search the delta for a deleted property. If found, invalidate the request.
  // If there is any key whose value looks like this:
  // 'email': ['text', 0, 0]
  // it means that the key has been removed and the request is invalid
}

Upvotes: 0

user1575921
user1575921

Reputation: 1088

I can't find way determine the result after use jsondiffpatch in my question, key different or value different,
so I find another module https://github.com/mirek/node-rus-diff it works just check $unset or $set

var diff = require('rus-diff').diff;

var result = diff(a, b);

if (typeof result.$unset != 'undefined') {
  console.log('different key');
}

Upvotes: 1

Related Questions