nv-validator-json-tree

1.0.24 • Public • Published

nv-validator-json-tree

  • nv-validator-json-tree
  • simplified version of nvjson AND nv-json-visitor
  • fewer data-type, but more fast
  • for testing nvlang fixed-jtype validate, normally USELESS

install

  • npm install nv-validator-json-tree

usage

   const {init} = require("nv-validator-json-tree"); 

example

    const {deepStrictEqual} = require("assert");

    var forest = init(10000);

    var j = {
        nu:null,
        num:77777,
        bool:true,
        str:'ssssss',
        ary: [0,'s',null,[10,20,30],{k1:'v',k2:0,k3:null,k4:[],k5:{}} ],
        dict: {
            nu:null,
            num:1.5,
            str:'s',
            ary: [0,'s',null,[10,20,30],{k1:'v',k2:0,k3:null,k4:[],k5:{}}],
            dict:{k5:true,k6:false }
        },
    }


    var rt = forest.creat_jtree(j);

    assert(rt.key_===null);
    assert(rt.val_===undefined);
    assert(rt.is_root());
    assert(rt.is_dict());
    assert(!rt.is_ele());
    assert(!rt.is_prop());





    deepStrictEqual(
          rt.creat_jtmpl(),
          {
            nu: undefined,
            num: undefined,
            bool: undefined,
            str: undefined,
            ary: [
              undefined,
              undefined,
              undefined,
              [ undefined, undefined, undefined ],
              { k1: undefined, k2: undefined, k3: undefined, k4: [], k5: {} }
            ],
            dict: {
              nu: undefined,
              num: undefined,
              str: undefined,
              ary: [
                undefined,
                undefined,
                undefined,
                [ undefined, undefined, undefined ],
                { k1: undefined, k2: undefined, k3: undefined, k4: [], k5: {} }
              ],
              dict: { k5: undefined, k6: undefined }
            }
          }
    )





    deepStrictEqual(
        rt.$sdfs_.map(nd=>nd.koi_),
        [
          null, 'nu',   'num', 'bool', 'str', 'ary',
          0,    1,      2,     3,      0,     1,
          2,    4,      'k1',  'k2',   'k3',  'k4',
          'k5', 'dict', 'nu',  'num',  'str', 'ary',
          0,    1,      2,     3,      0,     1,
          2,    4,      'k1',  'k2',   'k3',  'k4',
          'k5', 'dict', 'k5',  'k6'
        ]
    )

    deepStrictEqual(
        rt.pls_,
        [
          [],
          [ 'nu' ],
          [ 'num' ],
          [ 'bool' ],
          [ 'str' ],
          [ 'ary' ],
          [ 'ary', 0 ],
          [ 'ary', 1 ],
          [ 'ary', 2 ],
          [ 'ary', 3 ],
          [ 'ary', 3, 0 ],
          [ 'ary', 3, 1 ],
          [ 'ary', 3, 2 ],
          [ 'ary', 4 ],
          [ 'ary', 4, 'k1' ],
          [ 'ary', 4, 'k2' ],
          [ 'ary', 4, 'k3' ],
          [ 'ary', 4, 'k4' ],
          [ 'ary', 4, 'k5' ],
          [ 'dict' ],
          [ 'dict', 'nu' ],
          [ 'dict', 'num' ],
          [ 'dict', 'str' ],
          [ 'dict', 'ary' ],
          [ 'dict', 'ary', 0 ],
          [ 'dict', 'ary', 1 ],
          [ 'dict', 'ary', 2 ],
          [ 'dict', 'ary', 3 ],
          [ 'dict', 'ary', 3, 0 ],
          [ 'dict', 'ary', 3, 1 ],
          [ 'dict', 'ary', 3, 2 ],
          [ 'dict', 'ary', 4 ],
          [ 'dict', 'ary', 4, 'k1' ],
          [ 'dict', 'ary', 4, 'k2' ],
          [ 'dict', 'ary', 4, 'k3' ],
          [ 'dict', 'ary', 4, 'k4' ],
          [ 'dict', 'ary', 4, 'k5' ],
          [ 'dict', 'dict' ],
          [ 'dict', 'dict', 'k5' ],
          [ 'dict', 'dict', 'k6' ]
        ]
    )


    var nd1 = rt.koiget_chnd("nu")
    deepStrictEqual(nd1.val_,null)
    deepStrictEqual(rt.koiget_chnd("num").val_,77777)
    deepStrictEqual(rt.koiget_chnd("bool").val_,true)
    deepStrictEqual(rt.koiget_chnd("str").val_,'ssssss')
    var nd = rt.koiget_chnd("ary");
    deepStrictEqual(
        nd.calc(),
            [
             0,
             's',
             null,
             [ 10, 20, 30 ],
             { k1: 'v', k2: 0, k3: null, k4: [], k5: {} }
        ]
    );
    var nd = rt.koiget_chnd("dict");
    deepStrictEqual(
        nd.calc(),
        {
          nu: null,
          num: 1.5,
          str: 's',
          ary: [
            0,
            's',
            null,
            [ 10, 20, 30 ],
            { k1: 'v', k2: 0, k3: null, k4: [], k5: {} }
          ],
          dict: { k5: true, k6: false }
        }
    );

    deepStrictEqual(rt.plget_j([]),j)
    deepStrictEqual(rt.plget_j([ 'nu' ]),null)
    deepStrictEqual(rt.plget_j([ 'num' ]),77777)
    deepStrictEqual(rt.plget_j([ 'bool' ]),true)
    deepStrictEqual(rt.plget_j([ 'str' ]),'ssssss')
    deepStrictEqual(
        rt.plget_j([ 'ary' ]),
        [
          0,
          's',
          null,
          [ 10, 20, 30 ],
          { k1: 'v', k2: 0, k3: null, k4: [], k5: {} }
        ]
    )
    deepStrictEqual(
        rt.plget_j([ 'ary' ,3]),
        [ 10, 20, 30 ]
    )
    deepStrictEqual(
        rt.plget_j([ 'ary' ,3,1]),
        20
    )


    deepStrictEqual(
        rt.plget_j([ 'dict' ]),
        {
          nu: null,
          num: 1.5,
          str: 's',
          ary: [
            0,
            's',
            null,
            [ 10, 20, 30 ],
            { k1: 'v', k2: 0, k3: null, k4: [], k5: {} }
          ],
          dict: { k5: true, k6: false }
        }
    )
    deepStrictEqual(
       rt.plget_j([ 'dict','dict' ]),
       { k5: true, k6: false }
    )



    var jorig = {
        nu:null,
        num:77777,
        bool:true,
        str:'ssssss',
        ary: [0,'s',null,[10,20,30],{k1:'v',k2:0,k3:null,k4:[],k5:{}} ],
        dict: {
            nu:null,
            num:1.5,
            str:'s',
            ary: [0,'s',null,[10,20,30],{k1:'v',k2:0,k3:null,k4:[],k5:{}}],
            dict:{k5:true,k6:false }
        },
    }

    var j = {};
    var rt = forest.creat_jtree(j);

    rt.append(null,"nu");
    rt.append(77777,"num");
    rt.append(true,"bool");
    rt.append('ssssss',"str");
    deepStrictEqual(rt.calc(),{ nu: null, num: 77777, bool: true, str: 'ssssss' })

    var arynd = rt.append([0,'s',[10,20,30]],'ary');

    var midnd = arynd.koiget_chnd(2);
    var lbnd  = midnd.alsib(null);
    var rbnd  = midnd.arsib({k1:'v',k2:0,k3:null,k4:[],k5:{}});


    rt.append(
        {
            nu:null,
            num:1.5,
            str:'s',
            ary: [0,'s',null,[10,20,30],{k1:'v',k2:0,k3:null,k4:[],k5:{}}],
            dict:{k5:true,k6:false }
        },
        'dict'
    );
    deepStrictEqual(rt.calc(),jorig);


    var j = {};
    var rt = forest.creat_jtree(j);
    rt.plupdt([],[10,20,30]);
    deepStrictEqual(rt.calc(),[ 10, 20, 30 ])

    rt.pldel([1])
    deepStrictEqual(rt.calc(),[ 10,  30 ])


    var j = {
        nu:null,
        num:77777,
        bool:true,
        str:'ssssss',
        ary: [0,'s',null,[10,20,30],{k1:'v',k2:0,k3:null,k4:[],k5:{}} ],
        dict: {
            nu:null,
            num:1.5,
            str:'s',
            ary: [0,'s',null,[10,20,30],{k1:'v',k2:0,k3:null,k4:[],k5:{}}],
            dict:{k5:true,k6:false }
        },
    }


    var rt = forest.creat_jtree(j);

    deepStrictEqual(rt.clone().calc(),j);


    var j = {
        nu:null,
        num:77777,
        bool:true,
        str:'ssssss',
        ary: [0,'s',null,[10,20,30],{k1:'v',k2:0,k3:null,k4:[],k5:{}} ],
        dict: {
            nu:null,
            num:1.5,
            str:'s',
            ary: [0,'s',null,[10,20,30],{k1:'v',k2:0,k3:null,k4:[],k5:{}}],
            dict:{k5:true,k6:false }
        },
    }



    forest.ples(j)
    /*
    [
      [ [], [Function: is_dict], true ],
      [ [ 'nu' ], null, true ],
      [ [ 'num' ], 77777, true ],
      [ [ 'bool' ], true, true ],
      [ [ 'str' ], 'ssssss', true ],
      [ [ 'ary' ], [Function: isArray], true ],
      [ [ 'ary', 0 ], 0, true ],
      [ [ 'ary', 1 ], 's', true ],
      [ [ 'ary', 2 ], null, true ],
      [ [ 'ary', 3 ], [Function: isArray], true ],
      [ [ 'ary', 3, 0 ], 10, true ],
      [ [ 'ary', 3, 1 ], 20, true ],
      [ [ 'ary', 3, 2 ], 30, true ],
      [ [ 'ary', 4 ], [Function: is_dict], true ],
      [ [ 'ary', 4, 'k1' ], 'v', true ],
      [ [ 'ary', 4, 'k2' ], 0, true ],
      [ [ 'ary', 4, 'k3' ], null, true ],
      [ [ 'ary', 4, 'k4' ], [Function: isArray], true ],
      [ [ 'ary', 4, 'k5' ], [Function: is_dict], true ],
      [ [ 'dict' ], [Function: is_dict], true ],
      [ [ 'dict', 'nu' ], null, true ],
      [ [ 'dict', 'num' ], 1.5, true ],
      [ [ 'dict', 'str' ], 's', true ],
      [ [ 'dict', 'ary' ], [Function: isArray], true ],
      [ [ 'dict', 'ary', 0 ], 0, true ],
      [ [ 'dict', 'ary', 1 ], 's', true ],
      [ [ 'dict', 'ary', 2 ], null, true ],
      [ [ 'dict', 'ary', 3 ], [Function: isArray], true ],
      [ [ 'dict', 'ary', 3, 0 ], 10, true ],
      [ [ 'dict', 'ary', 3, 1 ], 20, true ],
      [ [ 'dict', 'ary', 3, 2 ], 30, true ],
      [ [ 'dict', 'ary', 4 ], [Function: is_dict], true ],
      [ [ 'dict', 'ary', 4, 'k1' ], 'v', true ],
      [ [ 'dict', 'ary', 4, 'k2' ], 0, true ],
      [ [ 'dict', 'ary', 4, 'k3' ], null, true ],
      [ [ 'dict', 'ary', 4, 'k4' ], [Function: isArray], true ],
      [ [ 'dict', 'ary', 4, 'k5' ], [Function: is_dict], true ],
      [ [ 'dict', 'dict' ], [Function: is_dict], true ],
      [ [ 'dict', 'dict', 'k5' ], true, true ],
      [ [ 'dict', 'dict', 'k6' ], false, true ]
    ]
    */

    forest.ples_full(j)
    /*
    [
      [
        [],
        {
          nu: null,
          num: 77777,
          bool: true,
          str: 'ssssss',
          ary: [Array],
          dict: [Object]
        },
        true
      ],
      [ [ 'nu' ], null, true ],
      [ [ 'num' ], 77777, true ],
      [ [ 'bool' ], true, true ],
      [ [ 'str' ], 'ssssss', true ],
      [ [ 'ary' ], [ 0, 's', null, [Array], [Object] ], true ],
      [ [ 'ary', 0 ], 0, true ],
      [ [ 'ary', 1 ], 's', true ],
      [ [ 'ary', 2 ], null, true ],
      [ [ 'ary', 3 ], [ 10, 20, 30 ], true ],
      [ [ 'ary', 3, 0 ], 10, true ],
      [ [ 'ary', 3, 1 ], 20, true ],
      [ [ 'ary', 3, 2 ], 30, true ],
      [ [ 'ary', 4 ], { k1: 'v', k2: 0, k3: null, k4: [], k5: {} }, true ],
      [ [ 'ary', 4, 'k1' ], 'v', true ],
      [ [ 'ary', 4, 'k2' ], 0, true ],
      [ [ 'ary', 4, 'k3' ], null, true ],
      [ [ 'ary', 4, 'k4' ], [], true ],
      [ [ 'ary', 4, 'k5' ], {}, true ],
      [
        [ 'dict' ],
        { nu: null, num: 1.5, str: 's', ary: [Array], dict: [Object] },
        true
      ],
      [ [ 'dict', 'nu' ], null, true ],
      [ [ 'dict', 'num' ], 1.5, true ],
      [ [ 'dict', 'str' ], 's', true ],
      [ [ 'dict', 'ary' ], [ 0, 's', null, [Array], [Object] ], true ],
      [ [ 'dict', 'ary', 0 ], 0, true ],
      [ [ 'dict', 'ary', 1 ], 's', true ],
      [ [ 'dict', 'ary', 2 ], null, true ],
      [ [ 'dict', 'ary', 3 ], [ 10, 20, 30 ], true ],
      [ [ 'dict', 'ary', 3, 0 ], 10, true ],
      [ [ 'dict', 'ary', 3, 1 ], 20, true ],
      [ [ 'dict', 'ary', 3, 2 ], 30, true ],
      [
        [ 'dict', 'ary', 4 ],
        { k1: 'v', k2: 0, k3: null, k4: [], k5: {} },
        true
      ],
      [ [ 'dict', 'ary', 4, 'k1' ], 'v', true ],
      [ [ 'dict', 'ary', 4, 'k2' ], 0, true ],
      [ [ 'dict', 'ary', 4, 'k3' ], null, true ],
      [ [ 'dict', 'ary', 4, 'k4' ], [], true ],
      [ [ 'dict', 'ary', 4, 'k5' ], {}, true ],
      [ [ 'dict', 'dict' ], { k5: true, k6: false }, true ],
      [ [ 'dict', 'dict', 'k5' ], true, true ],
      [ [ 'dict', 'dict', 'k6' ], false, true ]
    ]
    */



    var jvali = {
        ary: [
                {
                            s:(v,kori,pl,k,i,self) => typeof(v)==='string',
                            rng:(v,kori,pl,k,i,self) =>(v>=10) && (v<=20)
                },
                (v,kori,pl,k,i,self) => v.length===3,
                    (v,kori,pl,k,i,self) => Object.keys(v).length ===1
            ]
    }

    var jrequired = {
        ary: [
                {
                            s:false,
                            rng:false
                },
            false
            ]
    }

    var validator = forest.creat_validator(jvali,jrequired);
    let dump      = validator.dump_for_validator()
    console.dir(dump,{depth:null})

    /*
    {
      jvali: {
        ary: [
          {
            s: "(v,kori,pl,k,i,self) => typeof(v)==='string'",
            rng: '(v,kori,pl,k,i,self) =>(v>=10) && (v<=20)'
          },
          '(v,kori,pl,k,i,self) => v.length===3',
          '(v,kori,pl,k,i,self) => Object.keys(v).length ===1'
        ]
      },
      jrequired: [
        [ [], true ],
        [ [ 'ary' ], true ],
        [ [ 'ary', 0 ], true ],
        [ [ 'ary', 0, 's' ], false ],
        [ [ 'ary', 0, 'rng' ], false ],
        [ [ 'ary', 1 ], false ],
        [ [ 'ary', 2 ], true ]
      ]
    }
    */

    var validator = forest.load_validator_from_dump(dump)
    /*
    //this is for load code from database,only FOR test using

    > var validator = forest.load_validator_from_dump(dump)
    Dangerous!!

    > validator.calc()
    {
      ary: [
        { s: [Function (anonymous)], rng: [Function (anonymous)] },
        [Function (anonymous)],
        [Function (anonymous)]
      ]
    }
    */




    var j = {
        nu:null,
        num:77777,
        bool:true,
        str:'ssssss',
        ary: [0,'s',null,[10,20,30],{k1:'v',k2:0,k3:null,k4:[],k5:{}} ],
        dict: {
            nu:null,
            num:1.5,
            str:'s',
            ary: [0,'s',null,[10,20,30],{k1:'v',k2:0,k3:null,k4:[],k5:{}}],
            dict:{k5:true,k6:false }
        },
    }


    var rt = forest.creat_jtree(j);

    deepStrictEqual(
         rt.calc_treat_null_as_empty(),
        {
          num: 77777,
          bool: true,
          str: 'ssssss',
          ary: [ 0, 's', [ 10, 20, 30 ], { k1: 'v', k2: 0, k4: [], k5: {} } ],
          dict: {
            num: 1.5,
            str: 's',
            ary: [ 0, 's', [ 10, 20, 30 ], { k1: 'v', k2: 0, k4: [], k5: {} } ],
            dict: { k5: true, k6: false }
          }
        }
    )


    > rt.pl_nudel(["dict"])
    Node(8) [7 %ljqoxiwg:7% ] [
      Forest {
        fid: 'ljqoxiwgxbaqipdjeiwvtqvmhghrkwyajdze',
        max_size: 10000,
        idpool: { minid_: 1, maxid_: 10000, used_: 40, lefted_: 9960 }
      },
      7,
      false,
      60,
      null,
      'dict',
      undefined,
      true
    ]
    > rt.calc_treat_null_as_empty()
    {
      num: 77777,
      bool: true,
      str: 'ssssss',
      ary: [ 0, 's', [ 10, 20, 30 ], { k1: 'v', k2: 0, k4: [], k5: {} } ]
    }
    > rt.pl_nu_recover_container(["dict"])
    Node(8) [7 %ljqoxiwg:7% ] [
      Forest {
        fid: 'ljqoxiwgxbaqipdjeiwvtqvmhghrkwyajdze',
        max_size: 10000,
        idpool: { minid_: 1, maxid_: 10000, used_: 40, lefted_: 9960 }
      },
      7,
      false,
      60,
      undefined,
      'dict',
      undefined,
      true
    ]
    > rt.calc_treat_null_as_empty()
    {
      num: 77777,
      bool: true,
      str: 'ssssss',
      ary: [ 0, 's', [ 10, 20, 30 ], { k1: 'v', k2: 0, k4: [], k5: {} } ],
      dict: {
        num: 1.5,
        str: 's',
        ary: [ 0, 's', [Array], [Object] ],
        dict: { k5: true, k6: false }
      }
    }
    >


    var t = creat_jtree({y:Symbol("sym"),bi:[6n,Symbol("sym"),7n]})


    > t.calc()
    { y: Symbol(sym), bi: [ 6n, Symbol(sym), 7n ] }
    > 
    > t.dump_for_light_nvdbj()
    { y: 'sym', bi: [ '6', 'sym', '7' ] }
    > 
    > 

METHODS

forest

    forest.creat_jtree               forest.creat_validator           forest.load_validator_from_dump
    forest.ples                      forest.ples_full

node

        nd._is_ele                                 nd._is_prop                                nd.alsib
        nd.alsib_ary                               nd.alsib_dict                              nd.alsib_leaf
        nd.append                                  nd.append_ary                              nd.append_dict
        nd.append_leaf                             nd.arsib                                   nd.arsib_ary
        nd.arsib_dict                              nd.arsib_leaf                              nd.calc
        nd.calc_treat_null_as_empty                nd.clone                                   nd.constructor
        nd.creat_jtmpl                             nd.dump_for_validator                      nd.idx_
        nd.is_ary                                  nd.is_container                            nd.is_dict
        nd.is_ele                                  nd.is_leaf                                 nd.is_optional
        nd.is_prop                                 nd.is_required                             nd.is_root
        nd.key_                                    nd.koi_                                    nd.koiget_chnd
        nd.optional                                nd.pl_                                     nd.pl_disconn
        nd.pl_nu_recover_container                 nd.pl_nudel                                nd.pldel
        nd.plget_j                                 nd.plget_jr                                nd.plget_nd
        nd.plget_r                                 nd.plget_v                                 nd.plget_vr
        nd.pls_                                    nd.plset                                   nd.plset_dflt
        nd.plupdt                                  nd.require                                 nd.required_
        nd.reset_calced                            nd.set_typ_to_ary                          nd.set_typ_to_dict
        nd.typ_                                    nd.val_


        nd.dump_for_light_nvdbj
           //only support symbol bigint func-like
        nd.dump_for_light_nvdbj_treat_null_as_empty
           //only support symbol bigint func-like   

        nd.dtype_
           //this is USELES in this pkg, just for compatible WITH nvdb
           //   its used in nvdb 
           //   nvdb can directly save map/set/typed-array

API

            {
              init: [Function: init],
              T: {
                N2SEQ: {
                  un: 0,
                  nu: 10,
                  bool: 20,
                  t: 21,
                  f: 22,
                  num: 30,
                  int: 31,
                  float: 32,
                  str: 40,
                  ary: 50,
                  dict: 60
                },
                SEQ2N: {
                  '0': 'un',
                  '10': 'nu',
                  '20': 'bool',
                  '21': 't',
                  '22': 'f',
                  '30': 'num',
                  '31': 'int',
                  '32': 'float',
                  '40': 'str',
                  '50': 'ary',
                  '60': 'dict'
                },
                is_unset: [Function: is_unset],
                is_ary: [Function: is_ary],
                is_dict: [Function: is_dict],
                is_prim: [Function: is_prim]
              },
              _pl: {
                haspl: [Function: haspl],
                plget: [Function: plget],
                pldflt: [Function: pldflt]
              },
              _is: {
                typof: [Function: typof],
                instof: [Function: instof],
                _t: true,
                _f: false,
                _nu: null,
                _un: undefined,
                _A: [Function: Array],
                creat_nuary: [Function: creat_nuary],
                _O: [Function: Object],
                _N: [Function: Number],
                _Y: [Function: Symbol],
                _S: [Function: String],
                _J: Object [JSON] {},
                J2S: [Function: stringify],
                S2J: [Function: parse],
                jdcp: [Function: jdcp],
                jes2d: [Function: jes2d],
                is_container: [Function: is_container],
                is_ary: [Function: isArray],
                _is_empty_ary: [Function: _is_empty_ary],
                is_empty_ary: [Function: is_empty_ary],
                is_dict: [Function: is_dict],
                __is_empty_dict: [Function: __is_empty_dict],
                _is_empty_dict: [Function: _is_empty_dict],
                is_empty_dict: [Function: is_empty_dict],
                _is_prim: [Function: _is_prim],
                is_prim: [Function: is_prim],
                is_unnu: [Function: is_unnu],
                is_un: [Function: is_un],
                is_nu: [Function: is_nu],
                is_bool: [Function: is_bool],
                is_tru: [Function: is_tru],
                is_fls: [Function: is_fls],
                is_str: [Function: is_str],
                is_num: [Function: is_num],
                is_int: [Function: is_int],
                is_ary_idx: [Function: is_ary_idx],
                is_float: [Function: is_float],
                _is_leaf: [Function: _is_leaf],
                is_leaf: [Function: is_leaf],
                is_json: [Function: is_json],
                is_cls_or_func: [Function: is_cls_or_func],
                is_sync_validator: [Function: is_sync_validator],
                is_async_validator: [Function: is_async_validator]
                is_sym,
                is_bigint
              }
            }

LICENSE

  • ISC

/nv-validator-json-tree/

    Package Sidebar

    Install

    npm i nv-validator-json-tree

    Weekly Downloads

    0

    Version

    1.0.24

    License

    ISC

    Unpacked Size

    48.6 kB

    Total Files

    9

    Last publish

    Collaborators

    • ihgazni2