javascript - Node + Json validate key nested array -


i have sample json. in json need check following things.

  1. valid json or not.
  2. name key required without empty(list name , item name).
  3. items array length need greater 5.

i attached code , not working. feel approach not good. can please suggest me correct path.

var test = '{ "lists": [ { "items": [ { "name": "curd0", "sequence": 3 }, { "name": "curd1", "sequence": 2 }, { "name": "curd2", "sequence": 1 }, { "name": "curd3", "sequence": 4 }, { "name": "curd4", "sequence": 10 }, { "name": "curd5", "sequence": 9 }, { "name": "curd6", "sequence": 8 }, { "name": "curd7", "sequence": 7 }, { "name": "curd8", "sequence": 6 }, { "name": "curd9", "sequence": 5 } ], "name": "curd family", "status": "new", "created_by": 100036, "created_on": "2016-05-05t13:18:26.169z" }, { "items": [ { "name": "milk0", "sequence": 3 }, { "name": "milk1", "sequence": 2 }, { "name": "milk2", "sequence": 1 }, { "name": "milk3", "sequence": 4 }, { "name": "milk4", "sequence": 10 }, { "name": "milk5", "sequence": 9 }, { "name": "milk6", "sequence": 8 } ], "name": "milk family", "status": "new", "created_by": 100036, "created_on": "2016-05-05t13:18:44.504z" }, { "items": [ { "name": "water0", "sequence": 3 }, { "name": "water1", "sequence": 2 }, { "name": "water2", "sequence": 1 }, { "name": "water3", "sequence": 4 }, { "name": "water4", "sequence": 10 }, { "name": "water5", "sequence": 9 }, { "name": "water6", "sequence": 8 } ], "name": "water family", "status": "new", "created_by": 100036, "created_on": "2016-05-05t13:19:02.329z" }, { "items": [ { "name": "fruit0", "sequence": 3 }, { "name": "fruit1", "sequence": 2 }, { "name": "fruit2", "sequence": 1 }, { "name": "fruit3", "sequence": 4 } ], "name": "fruit family", "status": "new", "created_by": 100036, "created_on": "2016-05-05t13:19:15.503z" } ] }';   function validate(data, callback) {      (index in lists) {         if (!lists.index.hasownproperty('name')) {             callback("name cannot empty");         }          var itemlist = lists.index.items;         if (itemlist.length < 5) {             callback("list need more 5 lenth");         }         ( = 0; < itemlist.length; i++) {             if (!itemlist[i].hasownproperty('name')) {                 callback("item name cannot empty");             }         }      }     callback(null); }   validate(test, function (err) {      console.log(err); }); 

every need :

function containsnameinlists(element) {   return element.hasownproperty('name'); }  function containsnameinitems(element) {     return element.items.every(containskeyname); }  function containskeyname(element) {     return element.hasownproperty('name'); }  function lengthsuperiorto5(element) {     return element.length > 5; }  function itemsarraylength(){     return element.items.every(lengthsuperiorto5); } 

you can test if string valid json string using json.parse

try play string test different functions.

demo

edit : i've added additionnal functions :

edit demo


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -