-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow RegExp values for jsonSchema.pattern
field in property definition
#4228
Comments
AFAIK the basic idea of openapi.json is to transport this I think there is no benefit of allowing |
@derdeka This does seem like a non-standard feature at first glance but I think it can be beneficial. As per-#4218, regexp has certain flags that use the backslash which may interfere with JavaScript escape character. This means that those flags would need to be escaped with a double-backslash. This makes the final, written regexp non-standard when copy-and-pasted to elsewhere. So this feature may benefit by allowing regexp to be written in the standardised format without having to write JavaScript escape characters. |
The idea is to allow developers to write the pattern as a JavaScript regular expression and let the framework convert the RegExp object into a string pattern before passing it to JsonSchema/OpenApi/AJV or any other place. The benefits for user experience are described in the issue description. Unless you prefer to write your regular expressions as {
stringPattern: '\\d{3}-\\d{3}-\\d{4}',
regexPattern: /\d{3}-\d{3}-\d{4}/,
} |
I was thinking about this, and I'm retracting away from my previous stance. From my understanding, Instead, could I propose we make a root @property({
pattern: /\d{3}-\d{3}-\d{4}/,
...
}) This would put the responsibility on LB4 to validate the request instead of AJV. This would also be in-line with providing first-class support for A downside would be feature duplication: // This creates a double-validation from AJV and LB4
@property({
pattern: /\d{3}-\d{3}-\d{4}/,
jsonSchema: {
pattern: /\d{3}-\d{3}-\d{4}/,
},
}) But since AJV should be swappable, this may be a good step to moving away from over-reliance on AJV. |
I see your point. Can we perhaps enhance our built-in AJV version to allow Another problem is that
Personally, I think it's ok to normalize
I like that idea 👍 The catch is that once |
When creating a model, I'd like to specify pattern-based validation for my
phoneNum
property:At the moment, we require the pattern to be a string source:
pattern: '\\d{3}-\\d{3}-\\d{4}'
I find these strings problematic:
\
characters. When they are not escaped, users end up with a different RegExp that expected - see Use \d in AJV pattern doesn't work #4218 for an example.Workaround
Use
RegExp.source
property to convert from a RegExp instance to a source string.Acceptance criteria
Models (the important and also easy part)
Allow LB4 app developers to use RegExp
pattern
values in their model definitions, scoped tojsonSchema
only for now (i.e. to apply during REST API validation only, not as a database constraint). Example property definition:Implementation: The conversion from
RegExp
tostring
should probably happen insidemetaToJsonProperty()
function. UseRegExp.prototype.source
property to convert aRegExp
instance to a string.Test coverage: Add tests to verify the new behavior, e.g. to
packages/repository-json-schema/src/__tests__/unit/build-schema.unit.ts
Docs: Update documentation to explain users how to use
jsonSchema
andjsonSchema.pattern
in@property()
decorators - see e.g. https://loopback.io/doc/en/lb4/Model.html#supported-json-keywordsREST API layer (optional, could be difficult to implement)
Allow LB4 app developers to use RegExp values in OpenAPI spec metadata, e.g. provided via Controller decorators.
Start by writing a group of (acceptance-level) tests to verify handling of RegExp patterns by our REST API layer:
pattern
-based validation and a RegExp pattern value..source
property. Quoting from https://github.com/OAI/OpenAPI-Specification/blob/3.0.1/versions/3.0.1.md#properties: pattern (This string SHOULD be a valid regular expression, according to the ECMA 262 regular expression dialect)@param
definitions to allowRegExp
values forpattern
fields.RegExp
patterns to AJV so that it can perform pattern-base validation. It's possible that AJV supports RegExp values out of the box, in which case no changes may be requiredOut of scope
pattern
as a top-level model property:Quoting from #4228 (comment):
🎆 Hacktoberfest 2020
Greetings 👋 to all Hacktoberfest 2020 participants!
Here are few tips 👀 to make your start easier:
#loopback-contributors
channel, you can join our Slack workspace here.See also #6456.
The text was updated successfully, but these errors were encountered: