in ,

yusufnb / verify-json, Hacker News

yusufnb / verify-json, Hacker News
                    

        

Library to verify JSON structure easily using a lightweight JSON schema syntax

This project results from my need to verify JSON schema in a lightweight manner, without the need for an extensive definition and development code.

The schema syntax is minimalist and extremely easy to write.

npm install – s verify json

 const   verify  =  require  (   verify-json  ' )   import   verify       ' verify-json  ' 

 
 const  { verify 
} =(require     verify-json  " ;  
 let 
 json = {   markers :  [    {      name: "Rixos The Palm Dubai",      location: [25.1212, 55.1535],       favorite :   true ,       color :    "
 red " 
,     },     {       name : " Shangri-La Hotel "
,       location : [25.2084, 55.2719],       color :
 blue  ,     },   ], };     :     :! 

! for optional - required non null attribute of any type

// Skip all the quotations const schema = ` {markers: [{ name:string, location:[:lat,:long], favorite:! b, color }] } ; // customValidators are optional. See built-in validators.

 const 
  customValidators  ={    lat  :  ( val 
) =>  val >= -    && 
 val       long  :  ( val 
) =>  val >= -   && 
 val    };   let 

result = verify (json, schema, customValidators); console

.  log  (result);   //  true    json .  markers  ([0].   [0] =  ;   try  {    verify 

(json, schema, customValidators); } catch (error) {    console

.  log  ()   error  , error);   //  json.markers.0 .location.0: validation failed  } 

Following validators are built in and can be used directly -

{     string : _ isString ,     s : isString , //

 alias for string 

    number : _ is isNumber ,     n : isNumber , //

 alias for number 

    boolean : _ isBoolean ,     b : _ isBoolean , (

 alias for boolean 

    integer : _ isInteger ,     i : _ isInteger , //

 alias for integer 

    func : isFunction ,     f : isFunction , (

 alias for func 

     : ( ) => . match ( / ^ $ / , alpha-numeric }

Since lodash is a dependency, this method is also exposed as a lodash mixin. Once imported anywhere, you can simply use _. Verify to access it.

  _ .  verify 

(json, schema, customValidators)

MIT © Yusuf Bhabhrawala

   Read More

What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *

GIPHY App Key not set. Please check settings

Wekan: Open-source, trello-like kanban, Hacker News

China’s digital yuan to go live in May for government employees