Skip to content
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

Merged
merged 4 commits into from
Oct 5, 2023

Conversation

nickgros
Copy link
Contributor

@nickgros nickgros commented Sep 8, 2023

Fix #3856 playground issues

  • 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

- 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>>({});
Copy link
Contributor Author

@nickgros nickgros Sep 8, 2023

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.

Comment on lines +4 to +20
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;
});
}
};
Copy link
Contributor Author

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'>;
Copy link
Contributor Author

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: {
Copy link
Member

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);
Copy link
Member

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') {
Copy link
Member

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?

Copy link
Contributor Author

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;
Copy link
Contributor Author

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.

nickgros and others added 3 commits October 2, 2023 12:03
- Use ErrorSchemaBuilder
- Deep freeze playground samples
- Remove non-latin1 apostrophes
@heath-freenome heath-freenome merged commit e487116 into rjsf-team:main Oct 5, 2023
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Playground Validation Not Working
2 participants