Skip to content

Fix: Better validation messages using schema/uiSchema title if it exists #3337

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

Merged
merged 28 commits into from
Jan 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
be8c01c
use title if available for validation errors
dagda1 Dec 29, 2022
7e8c3e4
add title tests to validator-ajv8 validator tests
dagda1 Jan 3, 2023
6dd6877
use scoped lodash/get
dagda1 Jan 6, 2023
2db97d4
set verbose in createAjvInstance
dagda1 Jan 6, 2023
0d37edd
use parentSchema for getting title
dagda1 Jan 6, 2023
3607407
remove . in validator tests
dagda1 Jan 6, 2023
bc3a3de
rename local vars
dagda1 Jan 6, 2023
3255cad
remove duplicate local var
dagda1 Jan 6, 2023
6c866d9
Update packages/validator-ajv8/test/validator.test.ts
dagda1 Jan 6, 2023
31575b7
use params.missingProperty for currentProperty
dagda1 Jan 6, 2023
50383bb
check uiSchema for ui:title override in transformRJSFValidationErrors
dagda1 Jan 13, 2023
b52cb64
revert package-lock.json files
dagda1 Jan 13, 2023
7bb4a5b
Update packages/validator-ajv8/src/validator.ts
dagda1 Jan 13, 2023
d51c34f
Update packages/validator-ajv8/src/validator.ts
dagda1 Jan 13, 2023
8b6fd50
add getUiOptions import
dagda1 Jan 13, 2023
83985a1
remove type arguments in call to getUiOptions
dagda1 Jan 13, 2023
f12a552
revert to leading period in default stack message
dagda1 Jan 14, 2023
c2dc57d
revert lock files
dagda1 Jan 14, 2023
819908e
test for ajv8 ErrorObject optional fields
dagda1 Jan 14, 2023
65100ba
use array notation get
dagda1 Jan 14, 2023
7c8717d
update CHANGELOG
dagda1 Jan 14, 2023
3834858
Update CHANGELOG.md
dagda1 Jan 14, 2023
7f3ea5f
Update packages/validator-ajv8/package.json
dagda1 Jan 14, 2023
f932be8
Update packages/validator-ajv8/src/validator.ts
dagda1 Jan 14, 2023
34eb28e
Update packages/validator-ajv8/src/validator.ts
dagda1 Jan 14, 2023
254b0d5
move import
dagda1 Jan 14, 2023
53d0f36
Update packages/validator-ajv8/test/validator.test.ts
heath-freenome Jan 14, 2023
af975ac
Update CHANGELOG.md
heath-freenome Jan 14, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ should change the heading of the (upcoming) version to include a major version b

## @rjsf/validator-ajv8
- Remove alias for ajv -> ajv8 in package.json. This fixes [#3215](https://github.com/rjsf-team/react-jsonschema-form/issues/3215).
- Updated `AJV8Validator#transformRJSFValidationErrors` to return more human readable error messages. The ajv8 `ErrorObject` message is enhanced by replacing the error message field with either the `uiSchema`'s `ui:title` field if one exists or the `parentSchema` title if one exists. Fixes [#3246](https://github.com/rjsf-team/react-jsonschema-form/issues/3246)

# 5.0.0-beta-16

Expand Down
2 changes: 1 addition & 1 deletion packages/validator-ajv8/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"lodash-es": "^4.17.15"
},
"peerDependencies": {
"@rjsf/utils": "^5.0.0-beta.12"
"@rjsf/utils": "^5.0.0-beta.16"
},
"devDependencies": {
"@babel/core": "^7.20.12",
Expand Down
1 change: 1 addition & 0 deletions packages/validator-ajv8/src/createAjvInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const AJV_CONFIG: Options = {
allErrors: true,
multipleOfPrecision: 8,
strict: false,
verbose: true,
} as const;
export const COLOR_FORMAT_REGEX =
/^(#?([0-9A-Fa-f]{3}){1,2}\b|aqua|black|blue|fuchsia|gray|green|lime|maroon|navy|olive|orange|purple|red|silver|teal|white|yellow|(rgb\(\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*,\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*,\s*\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b\s*\))|(rgb\(\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*\)))$/;
Expand Down
58 changes: 52 additions & 6 deletions packages/validator-ajv8/src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import {
UiSchema,
ValidationData,
ValidatorType,
PROPERTIES_KEY,
getUiOptions,
} from "@rjsf/utils";
import get from "lodash/get";

import { CustomValidatorOptionsType, Localizer } from "./types";
import createAjvInstance from "./createAjvInstance";
Expand Down Expand Up @@ -199,20 +202,63 @@ export default class AJV8Validator<
* At some point, components should be updated to support ajv.
*
* @param errors - The list of AJV errors to convert to `RJSFValidationErrors`
* @private
* @protected
*/
private transformRJSFValidationErrors(
errors: ErrorObject[] = []
protected transformRJSFValidationErrors(
errors: ErrorObject[] = [],
uiSchema?: UiSchema<T, S, F>
): RJSFValidationError[] {
return errors.map((e: ErrorObject) => {
const { instancePath, keyword, message, params, schemaPath } = e;
const {
instancePath,
keyword,
params,
schemaPath,
parentSchema,
...rest
} = e;
let { message = "" } = rest;
let property = instancePath.replace(/\//g, ".");
let stack = `${property} ${message}`.trim();

if ("missingProperty" in params) {
property = property
? `${property}.${params.missingProperty}`
: params.missingProperty;
stack = message!;
const currentProperty: string = params.missingProperty;
const uiSchemaTitle = getUiOptions(
get(uiSchema, `${property.replace(/^\./, "")}`)
).title;

if (uiSchemaTitle) {
message = message.replace(currentProperty, uiSchemaTitle);
} else {
const parentSchemaTitle = get(parentSchema, [
PROPERTIES_KEY,
currentProperty,
"title",
]);

if (parentSchemaTitle) {
message = message.replace(currentProperty, parentSchemaTitle);
}
}

stack = message;
} else {
const uiSchemaTitle = getUiOptions(
get(uiSchema, `${property.replace(/^\./, "")}`)
).title;

if (uiSchemaTitle) {
stack = `'${uiSchemaTitle}' ${message}`.trim();
} else {
const parentSchemaTitle = parentSchema?.title;

if (parentSchemaTitle) {
stack = `'${parentSchemaTitle}' ${message}`.trim();
}
}
}

// put data in expected format
Expand Down Expand Up @@ -288,7 +334,7 @@ export default class AJV8Validator<
): ValidationData<T> {
const rawErrors = this.rawValidation<ErrorObject>(schema, formData);
const { validationError: invalidSchemaError } = rawErrors;
let errors = this.transformRJSFValidationErrors(rawErrors.errors);
let errors = this.transformRJSFValidationErrors(rawErrors.errors, uiSchema);

if (invalidSchemaError) {
errors = [...errors, { stack: invalidSchemaError!.message }];
Expand Down
Loading