corenova / yang-js

YANG parser and composer
Apache License 2.0
56 stars 18 forks source link

How to validate the data? #104

Closed Vafa-Andalibi closed 5 years ago

Vafa-Andalibi commented 5 years ago

I'm trying to use the variation of the example in the Quick Start to validate a data which does not match the schema:

const Yang = require('yang-js');
const schema = `
  container foo {
    leaf a { 
type string; 
mandatory true;
}
    leaf b { type uint8; }
  }
`;
const model = Yang.parse(schema).eval({
  foo: {
    b: 10,
  }
});

The model clearly does not match the schema because it doesn't have the a leaf and it's mandatory. But I can't find where is the result of the "validation" is. Is there any data field somewhere indicating "the parsed data does not match the schema"?

Thanks you,

sekur commented 5 years ago

Hi @Vafa-Andalibi

You should see something like following:

$ node
> const Yang = require('.');
> const schema = `container foo { leaf a { type string; mandatory true; } leaf b { type uint8; } }`;
> const model = Yang.parse(schema).eval({ foo: { b: 10 } });
ExpressionError: [container(foo)/leaf(a)/mandatory] predicate validation error: AssertionError [ERR_ASSERTION]: data must be defined
    at Function.Element.error (/home/peter/hack/yang-js/lib/element.js:60:15)
    at Function.Expression.error (/home/peter/hack/yang-js/lib/expression.js:191:40)
    at Function.Expression.apply (/home/peter/hack/yang-js/lib/expression.js:165:20)
    at Function.Expression.eval (/home/peter/hack/yang-js/lib/expression.js:178:21)
    at Function.Yang.eval (/home/peter/hack/yang-js/lib/yang.js:287:37)
    at Function.transform (/home/peter/hack/yang-js/lib/lang/extensions.js:866:32)
    at Function.Expression.apply (/home/peter/hack/yang-js/lib/expression.js:148:31)

Basically, errors during parse/eval will throw an error.

Vafa-Andalibi commented 5 years ago

Thank you for your prompt reply. Is there anyway to revalidate the model? Take this code for instance:


const Yang = require('yang-js');
const schema = `
  container foo {
    leaf a { 
       type string; 
       mandatory true;
       }
    leaf b { type uint8; }
  }
`;
const model = Yang.parse(schema).eval({
  foo: {
    a:"apple",
    b: 10,
  }
});

console.log(model.foo.a);
model.foo.a = 123;
console.log(model.foo.a);

Here as you see I'm setting integer to a whereas it should only be string. But the last line gets the value successfully even though it does not match the model. The output I get is:

$ node app.js
apple
123

So how an I revalidate the model?

Thanks again,

sekur commented 5 years ago

yang-js tries to be forgiving about type conversions. When you pass in a number to string, it will try to convert it to a string equivalent. When you pass in a string to a number, it will try to convert it to a number equivalent. Only when the conversion fails, it throws an error.

The way you are triggering revalidation is correct, it's just that yang-js auto-converted 123 into "123".