Closed jdillard closed 5 years ago
I have started looking into that
The YANG "pattern" statement, which is an optional substatement to the "type" statement, takes as an argument a regular expression in XML Schema (xsd) dialect https://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs
Mentioned format is not fully compatible with Swagger pattern which uses JavaScript/ECMA 262 regular expression dialect (https://www.ecma-international.org/ecma-262/8.0/index.html#sec-lexical-and-regexp-grammars). Below are main differences/limitations that my lead to bizarre issues when it comes to validation using XSD regex treated as as ECMA 262 dialect:
XSD regex dialect supports Unicode category classes (https://www.w3.org/TR/xmlschema-2/#charprop) which are not supported in ECM 262 dialect
character class subtraction (https://www.w3.org/TR/xmlschema-2/#dt-charexpr )is not supported in ECMA 262
\w, \d and \s shorthand classes can match Unicode symbols, and in ECMA 262 they do not.
\i, \l, \c, \C "multi-character escapes" (https://www.w3.org/TR/xmlschema-2/#dt-ccesN) are not supported in ECMA 262
"." in XML Schema matches any Unicode character (including astral characters) whereas in ECMA 262 it matches one half of a surrogate pair.
This is unfortunate, but you make an excellent point about the lack of compatibility between the two dialects. Thank you for the information.
The swagger schemas currently only shows a type of string, without pulling in the associated pattern, e.g. in typedef ipv4-address, if it exists. Looking into it, Swagger supports the pattern field in the parameter object, so it would be nice to pull that in to help with validation on the API calls.
We started looking into it, but weren't sure the best way to implement. We found a getPattern function in swagger-core, but wasn't sure where to tie that in. If anyone has ideas on how to get the patterns in, I think that would be a helpful addition to the yang2swagger tool.