Closed Vafa-Andalibi closed 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.
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,
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"
.
I'm trying to use the variation of the example in the Quick Start to validate a data which does not match the schema:
The
model
clearly does not match the schema because it doesn't have thea
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,