Validate the given json against defined yang

I want to validate a JSON against the defined yang. But the validation should be performed in such a way that, if any of the JSON key is not defined as a node in the yang, still the validation should be performed for the rest of keys present in the JSON

Sample JSON

{
“employee:employee_info” :{
“id” : “018”,
“name”: “John”,
“salary”: “500000s”,
“address”: “ABC”,
“phone_no”: “90292920222”,
“department”: “xxx” // this node not defined in the employee.yang
}
}

Yang definition

employee.yang

module employee {
container employee_info {
leaf id {
type string;
mandatory “true”;
description
“employee id.”;
}
leaf name {
type string;
mandatory “true”;
description
“employee name.”;
}
leaf address {
type string;
description
“employee address.”;
}
leaf phone_no {
type string;
mandatory “true”;
description
“employee contact no.”;
}
leaf salary {
type string;
mandatory “true”;
description
“employee salary.”;
}
}
}

My expectation is whatever(keys) that is present in the JSON can be validated against the yang if present. If the yang does not have the node definition of any of the JSON key( in the above case, the json key “department” is not been defined in the yang ) then the validation should be ignored for that particular node.Is there any possible to achieve it?

How do I tell yang to validate only the nodes that is been defined within it, (i.e) if a JSON has different keys which is not defined as part of yang, then ignore those keys in the JSON and go ahead with the validation?

YANG doesn’t validate by itself. It’s a language does describes your data schema.

I am not sure if you meant to validate the schema itself or the data: They are 2 different things. One is a holder of data, the other is the data.

In your case I would write a script that takes each JSON key/value and tries a NETCONF edit-config RPC. If it returns OK then OK otherwise the path generated based on the key in json doesn’t exist in the schema. I will have to iterate for every key.

I am sure there are other hacks to do this.

Any detailed suggestion/way of validating a JSON with the defined YANG, using some validator which agrees with the condition below:

If a Key in the input json is not been defined in the YANG, then validator should not throw an error.Check only whether the nodes that is defined in the YANG may/may not be present in the JSON.

I tried validating by using pyang, json2xml & yang2dsdl as follows but the above mentioned condition could nt be satisfied.

Step 1: pyang -f jtox -o employee.jtox employee.yang
The file “employee.jtox” gets generated which holds the schema definition from the defined yang file

Step 2: json2xml -t config -o employee.xml employee.jtox sample_employee.json
By passing the json and .jtox file as the input, the json2xml plugin gives an .xml file as the output.
The above command gives the following error message on the mentioned scenario:
json2xml: error at /employee:employee_info/department - invalid node

Step 3: yang2dsdl -t config employee.yang
Generates the dsdl schemas from the defined .yang file

Step 4: yang2dsdl -s -j -b employee -t config -v employee.xml
validates the syntax,semantic against the .xml file derived from json in Step 2.