-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix: array decorator #5559
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
fix: array decorator #5559
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -851,6 +851,27 @@ class TestModel { | |
| } | ||
| ``` | ||
|
|
||
| To define a nested array property, you must provide the `jsonSchema` field to | ||
| describe the sub-array property. For example: | ||
|
|
||
| ```ts | ||
| @model() | ||
| class TestModel { | ||
| // alternatively use @property.array('array') | ||
| @property.array(Array, { | ||
| jsonSchema: { | ||
| type: 'array', | ||
| items: {type: 'string'}, | ||
|
Comment on lines
+862
to
+864
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have another question of the design. Do we need to specify these two fields? if so, why do we need
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @agnes512 the If the current doc confuses you:
Any suggestion on how to make it more clear?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah I see, then I am good with it 👍 |
||
| }, | ||
| }) | ||
| nestedArr: Array<Array<string>>; | ||
| } | ||
| ``` | ||
|
|
||
| If the `jsonSchema` field is missing, you will get an error saying | ||
|
|
||
| > You must provide the "jsonSchema" field when define a nested array property' | ||
|
|
||
| ### Validation Rules | ||
|
|
||
| You can also specify the validation rules in the field `jsonSchema`. For | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -251,6 +251,34 @@ describe('build-schema', () => { | |
| expectValidJsonSchema(jsonSchema); | ||
| }); | ||
|
|
||
| it('properly converts nested array property when json schema provided', () => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have any tests on querying/filtering the nested arrays?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @agnes512
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mentioned it is because when I was triaging loopbackio/loopback-connector-postgresql#441, I realized that not all connectors support arrays/nested arrays. It'd be good if we can have them documented and tested. Just some thoughts 😄
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeah that's the problem, the original issue #4754 is not trying to support the full feature, it's a fix for the misuse of decorator.
It's not a trivial thing to test across connectors, let's do it in a separate story. |
||
| @model() | ||
| class TestModel { | ||
| // alternatively use @property.array('array') | ||
| @property.array(Array, { | ||
| jsonSchema: { | ||
| type: 'array', | ||
| items: {type: 'string'}, | ||
| }, | ||
| }) | ||
| nestedArr: Array<Array<string>>; | ||
| } | ||
|
|
||
| const jsonSchema = modelToJsonSchema(TestModel); | ||
| expect(jsonSchema.properties).to.deepEqual({ | ||
| nestedArr: { | ||
| type: 'array', | ||
| items: { | ||
| type: 'array', | ||
| items: { | ||
| type: 'string', | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
| expectValidJsonSchema(jsonSchema); | ||
| }); | ||
|
|
||
| it('properly converts properties with enum in json schema', () => { | ||
| enum QueryLanguage { | ||
| JSON = 'json', | ||
|
|
@@ -308,22 +336,20 @@ describe('build-schema', () => { | |
| }); | ||
| }); | ||
|
|
||
| it('properly converts properties with recursive arrays', () => { | ||
| it('throws for nested array property when json schema is missing', () => { | ||
| @model() | ||
| class RecursiveArray { | ||
| @property.array(Array) | ||
| recArr: string[][]; | ||
| } | ||
|
|
||
| const jsonSchema = modelToJsonSchema(RecursiveArray); | ||
| expect(jsonSchema.properties).to.eql({ | ||
| recArr: { | ||
| type: 'array', | ||
| items: { | ||
| type: 'array', | ||
| }, | ||
| expect.throws( | ||
| () => { | ||
| modelToJsonSchema(RecursiveArray); | ||
| }, | ||
| }); | ||
| Error, | ||
| 'You must provide the "jsonSchema" field when define a nested array property', | ||
| ); | ||
| }); | ||
|
|
||
| it('supports explicit primitive type decoration via strings', () => { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.