-
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] Fix custom validation playground example #3858
Conversation
- update `validation` example to use RJSF v5 API - add types for all samples and fix/ignore uncovered type issues - Spread sample data as extra form props
@@ -39,7 +40,7 @@ export default function Playground({ themes, validators }: PlaygroundProps) { | |||
experimental_defaultFormStateBehavior: { arrayMinItems: 'populate', emptyObjectFields: 'populateAllDefaults' }, | |||
}); | |||
const [FormComponent, setFormComponent] = useState<ComponentType<FormProps>>(withTheme({})); | |||
const [templates, setTemplates] = useState<Partial<TemplatesType>>(); | |||
const [otherFormProps, setOtherFormProps] = useState<Partial<FormProps>>({}); |
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.
Capture all of the form props that only change on load in this object. templates
can be included, so they don't need their own state variable.
function customValidate({ pass1, pass2 }: { pass1: string; pass2: string }, errors: any) { | ||
if (pass1 !== pass2) { | ||
errors.pass2.addError("Passwords don't match."); | ||
} | ||
return errors; | ||
} | ||
|
||
function transformErrors(errors: { name: string; property: string }[]) { | ||
const transformErrors: ErrorTransformer = (errors) => { | ||
return errors.map((error) => { | ||
if (error.name === 'minimum' && error.property === 'instance.age') { | ||
if (error.name === 'minimum' && error.schemaPath === '#/properties/age/minimum') { | ||
return Object.assign({}, error, { | ||
message: 'You need to be 18 because of some legal thing', | ||
}); | ||
} | ||
return error; | ||
}); | ||
} | ||
}; |
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.
This example was never updated when the API changed in v5.
@@ -0,0 +1,3 @@ | |||
import { FormProps } from '@rjsf/core'; | |||
|
|||
export type Sample = Omit<FormProps, 'validator'>; |
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.
Main cause for the playground example breaking was that it didn't get updated when the API changed in v5. This is more easily caught if the samples are all typed.
@@ -67,8 +69,11 @@ export default { | |||
password: 'noneed', | |||
}, | |||
extraErrors: { |
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.
Consider using the ErrorSchemaBuilder
|
||
export type Sample = keyof typeof samples; | ||
export const samples = Object.freeze(_samples); |
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.
Consider using "deep-freeze-es6": "^1.4.1",
for this
return errors.map((error) => { | ||
if (error.name === 'minimum' && error.property === 'instance.age') { | ||
if (error.name === 'minimum' && error.schemaPath === '#/properties/age/minimum') { |
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.
Does this work with ajv v6?
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.
Yes
@@ -76,3 +76,5 @@ export default { | |||
}, | |||
}, | |||
}; | |||
|
|||
export default propertyDependencies; |
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.
TODO: Fix UI schema contains a non-latin1 apostrophe.
- Use ErrorSchemaBuilder - Deep freeze playground samples - Remove non-latin1 apostrophes
Fix #3856 playground issues
validation
example to use RJSF v5 API