corenova / yang-js

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

validation of Read-only data #121

Closed Joel-Cripps closed 4 years ago

Joel-Cripps commented 4 years ago

Hey there, From the docs it looks like validate is the way to validate read-only data against a schema. In the following case I get an error when trying to validate.

  let validateobj = openconfig_interfaces_model.validate(
                {
                    'openconfig-interfaces:interfaces': {
                        interface: [
                            {
                                name: config.results[0].name,
                                config: {
                                    name: config.results[0].name,
                                    type: 'IF_ETHERNET',
                                    mtu: config.results[0].mtu
                                },
                                state: {
                                    type: 'IF_ETHERNET'
                                }
                            }
                        ]
                    }
                },
            );

Error:

(node:25258) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'has' of undefined
    at Function.transform (x/src/node_modules/yang-js/lib/lang/extensions.js:689:26)
    at Function.Expression.apply (x/src/node_modules/yang-js/lib/expression.js:168:31)
    at Function.Expression.eval (x/src/node_modules/yang-js/lib/expression.js:201:21)
    at Function.Yang.eval (x/src/node_modules/yang-js/lib/yang.js:213:37)
    at Function.transform (x/src/node_modules/yang-js/lib/lang/extensions.js:1190:33)
    at Function.Expression.apply (x/src/node_modules/yang-js/lib/expression.js:168:31)
    at Function.Yang.validate (x/src/node_modules/yang-js/lib/yang.js:221:19)
    at OpenConfigInterpreter.ConvertRestToYang (x/src/dist/index.js:201:59)
    at ServerDuplexStream.<anonymous> (x/src/dist/GnmiProtoHandlers.js:132:54)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

It appears that ctx.store is undefined in this call.

      transform: function(data, ctx) {
        if (this.module.nodes.length && Object.isExtensible(data)) {

          if (!ctx.store.has(this.module.tag)) {
            this.debug("IMPORT: absorbing data for '" + this.tag + "'");
            this.module["eval"](data, ctx);
          }

A quick change of the code to

 if (ctx.store && !ctx.store.has(this.module.tag))

changes the error to :

(node:26481) UnhandledPromiseRejectionWarning: Error: cannot set data on read-only (config false) element

which appears to be returning the yang with bound data.

I am wondering if : 1) I am correctly using the validation function or if there is a missing option I should be providing, 2) If this is a bug in the validation code.

Thanks, Joel

sekur commented 4 years ago

Hi @Joel-Cripps, thanks for reporting this issue. Looks like this functionality is currently broken. I'll look into this further and let you know what I find.

sekur commented 4 years ago

Staged for 0.24 release.