Test if the given JSON matches a certain structure.
- Very simple structure-describing rules.
- Structure describer itself is a valid JSON
var JSM=require('./js-structure-match.js');
var jm=new JSM({a:'int',
b:'string',
c:'float',
d:[{a:'int',b:'string'}],
e:{a:'int',
b:'string'}})
console.log(jm.match({a:1,
b:'hello',
c:2.3,
d:[{a:1,b:'meow'},
{a:233,b:'meowmeow'}],
e:{a:1,
b:'meowmeow'}}))
Let's say we are matching data data
to the structure structure
var JSM=require('./js-structure-match.js');
var jm=new JSM(structure);
console.log(jm.match(data));
The output will be true
when following rules are satisfied:
structure
anddata
are both arrays anddata[i]
matchesstructure[0]
for eachi
structure
anddata
are both objects, and for every keykey
ofstructure
,data
have a key namedkey
anddata[key]
matchesstructure[key]
structure=='int'
anddata
is a intstructure=='float'
anddata
is a numberstructure=='string'
anddata
is a string
- Test if
structure
is valid - npm repository