corenova / yang-js

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

Mandatory Fields in Read Only Data #125

Open Joel-Cripps opened 3 years ago

Joel-Cripps commented 3 years ago

Hey there, When validating mandatory fields in read-only data I receive a Data must be defined error.

The Code:

            let validateobjState = openconfig_interfaces_model.validate({
                        'openconfig-interfaces:interfaces': {
                            interface: [
                                {
                                    name: getRequest?.results[0]?.name,
                                    config: {
                                        name: getRequest?.results[0]?.name,
                                    },
                                    //Read-only data:
                                    state: {
                                        name: getRequest?.results[0]?.name,
                                        type: 'IF_ETHERNET', 
                                        mtu: getRequest?.results[0]?.mtu,
                                        'loopback-mode': false,
                                        description: getRequest?.results[0]?.description,
                                        enabled: getRequest?.results[0]?.status === 'up' ? true : false,
                                        ifindex: getRequest?.results[0]?.status?.vindex,
                                        'admin-status': 'DOWN',  <-----------Mandatory field
                                        'oper-status': getRequest?.results[0]?.status === 'up' ? 'UP' : 'DOWN'
                                    }
                                }
                            ]
                        }
                    });

The Yang Model:

    leaf admin-status {
      type enumeration {
        enum UP {
          description
            "Ready to pass packets.";
        }
        enum DOWN {
          description
            "Not ready to pass packets and not in some test mode.";
        }
        enum TESTING {
          description
            "In some test mode.";
        }
      }
      mandatory true;

The error:

(node:700819) UnhandledPromiseRejectionWarning: ExpressionError: [module(openconfig-interfaces)/grouping(interface-common-state)/leaf(admin-status)/mandatory] predicate validation error: AssertionError [ERR_ASSERTION]: data must be defined
    at Function.error (src/node_modules/yang-js/lib/element.js:62:17)
    at Function.error (src/node_modules/yang-js/lib/expression.js:140:21)
    at Function.apply (src/node_modules/yang-js/lib/expression.js:110:22)
    at Function.eval (src/node_modules/yang-js/lib/expression.js:127:23)
    at Function.eval (src/node_modules/yang-js/lib/yang.js:238:22)
    at Function.transform (src/node_modules/yang-js/lib/lang/extensions.js:1089:25)
    at Function.apply (src/node_modules/yang-js/lib/expression.js:93:33)
    at Property.<anonymous> (src/node_modules/yang-js/lib/property.js:199:34)
    at Property.set (src/node_modules/yang-js/lib/property.js:203:12)
    at Property.attach (src/node_modules/yang-js/lib/property.js:347:16)

If I change the value of mandatory to false, the validation is successful.
If I change the value to false and change admin-status to admin-status:"asdfasdfadsf" the validation will generate a correct enum error: (node:700190) UnhandledPromiseRejectionWarning: Error: [enumeration] type violation for 'asdfasdfadsf' on UP,DOWN,TESTING

let me know if you have any questions

sekur commented 3 years ago

Thanks @Joel-Cripps for reporting this issue. I'll take a look and let you know what I find.