Validation rule using "must"

I wanted to write a validation rule in a yang file that should restrict deletion of employees when the total count of grade-2 employees is 1


container employees{
list employee{
leaf name{
leaf grade{

whether this could be achieved through “must” keyword. If yes, please let me know how?

Assuming that your list keys are name and grade you can do something like this:

  container employees {
    must "boolean(employee[name][grade=2])" {
	"Total count of grade 2 employees must not be less than 1 once the list is initialized";
    list employee {
      key "name grade";
      leaf name {
	type string;
      leaf grade {
	type int32;

I think the must statement above works for blocking deletion of the last grade-2 employee.
But I think the requirement here is to block deletion of any employee (other grades) if there is only one grade-2 employee in the list.

Priyadarshini, Can you confirm this?

If this is the case, then you cannot do this via must statements.

The reason is because what is validated is data and not operations.

The configurations:

Configuration 1:

Employees employee userA 1
Employees employee userB 1
Employees employee userC 2


Configuration 2:

Employees employee userA 1
Employees employee userC 2

Are both valid configurations but you want to control the operation that takes us from configuration 1 to configuration 2 which is not possible in YANG.

Your only option in this case is to use the validation callback and read the new candidate configuration and compare it against the committed configuration. You can then return an error code if you decide something is wrong.

Thanks Cohult and Nabil.

what Cohult mentioned exactly answered my query and it worked as well …!
must “boolean(employee[name][grade=2])” {error-message “Total Count…”}

Thanks a Lot.

Apart from this, I had a chance to try with “count” which too got worked.
must “count (employee[name][grade=2]) >=1” {error-message “Total Count…”}