loopbackio / loopbackio/loopback-next
Allow RegExp values for `jsonSchema.pattern` field in property definition
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- TypeScript
- Star
- 5.1k
- Fork
- 1.1k
- Merge trung bình
- 2 ngày 21 giờ
- Pull request đã merge (30 ngày)
- 27
Mô tả
When creating a model, I'd like to specify pattern-based validation for my phoneNum property:
@property({
type: 'string',
jsonSchema: {
pattern: /\d{3}-\d{3}-\d{4}/,
},
})
phoneNum?: string;
At the moment, we require the pattern to be a string source:
pattern: '\\d{3}-\\d{3}-\\d{4}'
I find these strings problematic:
- Editors/IDEs don't highlight RegExp keywords
- It's easy to forget to escape
\characters. When they are not escaped, users end up with a different RegExp that expected - see #4218 for an example.
Workaround
Use RegExp.source property to convert from a RegExp instance to a source string.
@property({
type: 'string',
jsonSchema: {
pattern: /\d{3}-\d{3}-\d{4}/.source,
},
})
phoneNum?: string;
Acceptance criteria
Models (the important and also easy part)
Allow LB4 app developers to use RegExp pattern values in their model definitions, scoped to jsonSchema only for now (i.e. to apply during REST API validation only, not as a database constraint). Example property definition:
@property({
jsonSchema: {
pattern: /\d{3}-\d{3}-\d{4}/,
},
})
phoneNum?: string;
-
Implementation: The conversion from
RegExptostringshould probably happen insidemetaToJsonProperty()function. UseRegExp.prototype.sourceproperty to convert aRegExpinstance 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
jsonSchemaandjsonSchema.patternin@property()decorators - see e.g. https://loopback.io/doc/en/lb4/Model.html#supported-json-keywords
REST 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.
class MyController {
greet(
@param({
name: 'date',
in: 'path',
schema: {
type: 'string',
pattern: /^\d{4}-\d{2}-\d{2}$/,
}
})
today: string,
) {
return `Good morning, today is ${date}`;
}
}
Start by writing a group of (acceptance-level) tests to verify handling of RegExp patterns by our REST API layer:
- Define a controller with a method with a parameter using
pattern-based validation and a RegExp pattern value. - Send a request with a valid value expecting 200 response.
- Send a request with an invalid value expecting 4xx response.
- Verify that the OpenAPI spec document returned by the app includes the pattern converted to string via
.sourceproperty. 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) - Fix the implementation (and type definitions) as necessary to allow such test to pass.
- I think we will need to update
@paramdefinitions to allowRegExpvalues forpatternfields. - Figure out how to pass
RegExppatterns 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 required
Out of scope
pattern as a top-level model property:
@property({
pattern: /\d{3}-\d{3}-\d{4}/,
...
})
Quoting from https://github.com/strongloop/loopback-next/issues/4228#issuecomment-646700989:
The catch is that once
patternis a regular property metadata, it must be enforced both at REST API level (@loopback/restmust translate that pattern to AJV JSON Schema field) and at data-access level (by loopback-datasource-juggler), so it's more work to make this happen 🤷 The upside is that with a new property-level metadata, connectors can translate thepatternconstraint to a database constraint too (a PostgreSQL example). Related: Epic: Validation at Model/ORM level #1872.
🎆 Hacktoberfest 2020
Greetings 👋 to all Hacktoberfest 2020 participants!
Here are few tips 👀 to make your start easier:
- Before you start working on this issue, please leave a comment to let others know.
- Feel free to implement support for RegExp patterns in model property definitions only, it's ok to leave the rest for others to pick up.
- If you are new to GitHub pull requests, then you can learn about the process in Submitting a pull request to LoopBack 4.
- If this is your first contribution to LoopBack, then please take a look at our Developer guide
- Feel free to ask for help in
#loopback-contributorschannel, you can join our Slack workspace here.
See also #6456.
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Đối với phần công việc giới hạn trong model, hãy bắt đầu tại packages/repository-json-schema/src/build-schema.ts, ở metaToJsonProperty(), sau đó thêm các trường hợp vào packages/repository-json-schema/src/tests/unit/build-schema.unit.ts. Chạy các unit test của repository-json-schema và xác minh rằng các mẫu RegExp được biểu diễn bằng các chuỗi nguồn của chúng. Phần REST rộng hơn yêu cầu các acceptance test cho các request hợp lệ và không hợp lệ, cùng với tài liệu OpenAPI được tạo, trong khi bản cập nhật tài liệu nên đề cập đến việc sử dụng jsonSchema.pattern.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- openapi, typescript
- Lĩnh vực
- api, backend-api-design, documentation, testing
- Loại issue
- Tính năng
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 45/100