-
Notifications
You must be signed in to change notification settings - Fork 11
feat(input_schema): Custom error messages #567
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
Conversation
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.
Pull Request Overview
This PR implements custom error messages for input schema validation, allowing developers to define field-specific error messages for validation keywords. This provides better user experience by enabling more contextual and user-friendly error messages.
Key Changes
- Added
errorMessageproperty definition to the JSON schema with support for all standard validation keywords (type, pattern, min/max constraints, etc.) - Implemented
getCustomErrorMessage()function to retrieve custom messages from the schema based on validation paths - Integrated custom error message lookup into both AJV-based validation (
parseAjvError) and pattern validation in utilities - Added comprehensive test coverage for all supported validation keywords and field types
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| packages/json_schemas/schemas/input.schema.json | Added errorMessage definition and referenced it in all property type definitions (string, array, object, integer, number, boolean, resource, any, and their sub-schema variants) |
| packages/input_schema/src/input_schema.ts | Implemented getCustomErrorMessage() function and integrated it into parseAjvError() to prioritize custom messages over default ones |
| packages/input_schema/src/utilities.ts | Integrated custom error message support for pattern validation (patternKey and patternValue) in various field types |
| test/input_schema.test.ts | Added tests to validate schema accepts custom error messages and rejects unknown keywords |
| test/utilities.client.test.ts | Added comprehensive test coverage for custom error messages across all validation keywords and field types |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
valekjo
left a comment
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.
👏
| const pathParts = schemaPath | ||
| .replace(/^#\//, '') | ||
| .split('/') | ||
| .filter(Boolean); |
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.
Note: I was wondering how AJV deals with / in schemaPath and it seems it escapes it as ~1
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.
Yep, I tried that too :)
# Conflicts: # packages/json_schemas/schemas/input.schema.json
jancurn
left a comment
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.
Looks good (based on the description, haven't reviewed the code). Thanks Martin!
Solves https://github.com/apify/apify-core/issues/24045 - implements option to define custom error messages within the input schema based on the specification here: #556
Few notes to the implementation:
It supports only property level validation keywords like
type,minLength,pattern,.. but no schema/object level keywords (required,additionalProperties)It supports our custom validation keywords too (
patternKeyandpatternValue)This allows to specify any validation keyword in any property. There is no check whether the property actually supports or moreover has defined the keyword. This makes the implementation much more simpler using one
errorMessagedefinition and reusing it in all the properties.This would start working out-of-the box in the UI, but we use there custom function
beautifyValidationMessageto modify the default error message for UI usecases and the custom message would just bubble through without any modification in most cases (there is regex check) - IMHO expected behavior, but mentioning it here to be sure.