-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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: add condition to pick correct schemas for fixed array items in topathschemainternal util #3916
Changes from all commits
57521c2
86c0034
ad9bc9f
2aad852
b4af751
b305a29
70d29df
21d1b13
892e3c1
adace34
1c74f35
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 |
---|---|---|
|
@@ -4,6 +4,17 @@ import { TestValidatorType } from './types'; | |
|
||
export default function toPathSchemaTest(testValidator: TestValidatorType) { | ||
describe('toPathSchema()', () => { | ||
let consoleWarnSpy: jest.SpyInstance; | ||
|
||
beforeAll(() => { | ||
// spy on console.warn() and make it do nothing to avoid making noise in the test | ||
consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(); | ||
}); | ||
|
||
afterAll(() => { | ||
consoleWarnSpy.mockRestore(); | ||
}); | ||
|
||
it('should return a pathSchema for root field', () => { | ||
const schema: RJSFSchema = { type: 'string' }; | ||
|
||
|
@@ -702,5 +713,182 @@ export default function toPathSchemaTest(testValidator: TestValidatorType) { | |
}, | ||
}); | ||
}); | ||
|
||
it('should handle fixed array items which are objects', () => { | ||
const schema: RJSFSchema = { | ||
type: 'object', | ||
properties: { | ||
str: { type: 'string' }, | ||
arr: { | ||
type: 'array', | ||
items: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
name: { type: 'string' }, | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
}; | ||
|
||
const formData = { | ||
str: 'string', | ||
arr: [{ name: 'name1' }], | ||
}; | ||
|
||
expect(toPathSchema(testValidator, schema, '', schema, formData)).toEqual({ | ||
$name: '', | ||
arr: { | ||
$name: 'arr', | ||
'0': { | ||
$name: 'arr.0', | ||
name: { | ||
$name: 'arr.0.name', | ||
}, | ||
}, | ||
}, | ||
str: { | ||
$name: 'str', | ||
}, | ||
}); | ||
}); | ||
|
||
it('should handle fixed array items which are objects, which have additionalItems in the schema and have additional items in the form data', () => { | ||
const schema: RJSFSchema = { | ||
type: 'object', | ||
properties: { | ||
str: { type: 'string' }, | ||
arr: { | ||
type: 'array', | ||
items: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
name: { type: 'string' }, | ||
}, | ||
}, | ||
], | ||
additionalItems: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const formData = { | ||
str: 'string', | ||
arr: [{ name: 'name1' }, 'name2'], | ||
}; | ||
|
||
expect(toPathSchema(testValidator, schema, '', schema, formData)).toEqual({ | ||
$name: '', | ||
arr: { | ||
$name: 'arr', | ||
'0': { | ||
$name: 'arr.0', | ||
name: { | ||
$name: 'arr.0.name', | ||
}, | ||
}, | ||
'1': { | ||
$name: 'arr.1', | ||
}, | ||
}, | ||
str: { | ||
$name: 'str', | ||
}, | ||
}); | ||
}); | ||
|
||
it("should handle fixed array items which are objects, which have additionalItems in the schema but don't have additional items in the form data", () => { | ||
const schema: RJSFSchema = { | ||
type: 'object', | ||
properties: { | ||
str: { type: 'string' }, | ||
arr: { | ||
type: 'array', | ||
items: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
name: { type: 'string' }, | ||
}, | ||
}, | ||
], | ||
additionalItems: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const formData = { | ||
str: 'string', | ||
arr: [{ name: 'name1' }], | ||
}; | ||
|
||
expect(toPathSchema(testValidator, schema, '', schema, formData)).toEqual({ | ||
$name: '', | ||
arr: { | ||
$name: 'arr', | ||
'0': { | ||
$name: 'arr.0', | ||
name: { | ||
$name: 'arr.0.name', | ||
}, | ||
}, | ||
}, | ||
str: { | ||
$name: 'str', | ||
}, | ||
}); | ||
}); | ||
|
||
it('should handle fixed array items which are objects, which have additionalItems as false in the schema but have additional items in the form data', () => { | ||
const schema: RJSFSchema = { | ||
type: 'object', | ||
properties: { | ||
str: { type: 'string' }, | ||
arr: { | ||
type: 'array', | ||
items: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
name: { type: 'string' }, | ||
}, | ||
}, | ||
], | ||
additionalItems: false, | ||
}, | ||
}, | ||
}; | ||
|
||
const formData = { | ||
str: 'string', | ||
arr: [{ name: 'name1' }, 'name2'], | ||
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'm wondering whether there should be a 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 can see that |
||
}; | ||
|
||
expect(toPathSchema(testValidator, schema, '', schema, formData)).toEqual({ | ||
$name: '', | ||
arr: { | ||
$name: 'arr', | ||
'0': { | ||
$name: 'arr.0', | ||
name: { | ||
$name: 'arr.0.name', | ||
}, | ||
}, | ||
}, | ||
str: { | ||
$name: 'str', | ||
}, | ||
}); | ||
expect(consoleWarnSpy).toHaveBeenCalledWith( | ||
'Unable to generate path schema for ".arr.1". No schema defined for it' | ||
); | ||
}); | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the moment, let's add a
console.warn()
here to let users know that they have unhandledformData
. Which means we want to update the test below with a check thatconsole.warn()
was called. This can be done with ajest.spyOn()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Added a message. Let me know if there is a change required.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@heath-freenome Should this log be moved up a level outside the for loop? We don't know how many items there will be in the array and if there are too many items, it might clutter the console.