How to restrict the length of a string type of the leaf to some value?

I have a leaf of type string, whose length I want to restrict to ‘<= 32’.
Leaf is defind as:
leaf aaa{
type string {
pattern ‘[a-zA-Z0-9_.][a-zA-Z0-9_.-]*[$]?’;
}
description " ";
tailf:info " ";
}
I have an idea about the ‘must’ statement but not sure how can I mentioned the size/length of a string in must statement.

Any help please!!

See the length statement: RFC 7950: The YANG 1.1 Data Modeling Language

Hi @cohult,
Thanks for your reply.
Actually using ‘length ’ working fine but its providing very generic error i.e. syntax error: “<provided_username>” has a bad length/size.

Is there any option to customize this error with something like this: “Error: The user name supplied is too long, should not be larger than 32 Chars.” ?

Thanks in advance!

See the “error-message” substatement to the length statement. RFC 7950: The YANG 1.1 Data Modeling Language
There are several examples that use the “error-message” substatement in the RFC.

I tried with error-message already, which is providing the error under possible-completion and not as a syntax-error. Ex. as below:
length “1…32” {
error-message “The user name supplied is too long, should not be larger than 32 Chars.”;
}
o/p:
system aaa authentication users user setpasswordfailureforlongusername ?
Possible completions:
Error: The user name supplied is too long, should not be larger than 32 Chars.
appliance-1(config)#

Expectations are;
system aaa authentication users user setpasswordfailureforlongusername
-------------------^
syntax error: "The user name supplied is too long, should not be larger than 32 Chars.

Try with length 0…32 instead of 1…32