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

Simplify the Validator.validate() return type #8552

Closed
vlukashov opened this issue Jun 11, 2020 · 0 comments · Fixed by #8602
Closed

Simplify the Validator.validate() return type #8552

vlukashov opened this issue Jun 11, 2020 · 0 comments · Fixed by #8602
Assignees
Labels
DX hilla Issues related to Hilla

Comments

@vlukashov
Copy link

The return type of the validate() method in record-level validators should be more intuitive to use (should not require including boilerplate / unnecessary properties).

DoD:

  • it is possible to mark a single model property as 'invalid': return { property: model.password }
  • it is possible to mark several model properties as 'invalid': return [{ property: model.password }, { property: model.repeatPassword }] (in this case the validator's default message it linked to the first property in the array)
  • it is possible to provide different messages for each invalid model property: [{ property: model.password }, { property: model.repeatPassword, message: 'please check that you've repeated the password exactly as it's typed in the field above ^' }]
  • it is possible to mark the entire record as invalid without marking any specific field as invalid: return false
  • the validate method has to always return something, an empty return is not OK: return true or return [] both are treated as 'validation passed without errors' (the recommended way in all our examples is return true).

Extra:
It’s not easy to access the model inside the validator function => add binder as the second parameter to the validator callback: validate: async (value, binder) => { ... }

@vlukashov vlukashov added hilla Issues related to Hilla DX labels Jun 11, 2020
@Haprog Haprog self-assigned this Jun 15, 2020
Haprog added a commit that referenced this issue Jun 22, 2020
- ValidationCallback may now return a single ValidationResult or an array of ValidationResult objects which don't need to specify the "value" or "validator" properties which were needed previously when ValueError was used in the return type.
- ValidationResult may contain an optional "message" property for overriding the default error message (coming from validator) for a specific property.
- ValueError now also directly has a "message" property so it's slightly simpler to get the error messages when working with a list of errors.
- Update flow-client tsconfig to allow using Array.flat() etc.

Fixes #8552
platosha pushed a commit that referenced this issue Jun 23, 2020
* Simplify ValidationCallback return type

- ValidationCallback may now return a single ValidationResult or an array of ValidationResult objects which don't need to specify the "value" or "validator" properties which were needed previously when ValueError was used in the return type.
- ValidationResult may contain an optional "message" property for overriding the default error message (coming from validator) for a specific property.
- ValueError now also directly has a "message" property so it's slightly simpler to get the error messages when working with a list of errors.
- Update flow-client tsconfig to allow using Array.flat() etc.

Fixes #8552

* Add Binder as a parameter to ValidationCallback

* Fix linter errors
vlukashov pushed a commit to vaadin/flow-and-components-documentation that referenced this issue Jun 29, 2020
Highlight server-side validation as the primary way to validate the user input.
Present client-side validation as a UX optimization.
Provide a complete example for adding a custom validator (both server-side and client-side).
Introduce JSR-380 instead of assuming readers know what it is.
Make the example more realistic.

Also update the docs after fixing vaadin/flow#8551 and vaadin/flow#8552

Fixes #1291
Fixes #1292
platosha added a commit to vaadin/flow-and-components-documentation that referenced this issue Jun 30, 2020
* fix: improve client-side validation docs

Highlight server-side validation as the primary way to validate the user input.
Present client-side validation as a UX optimization.
Provide a complete example for adding a custom validator (both server-side and client-side).
Introduce JSR-380 instead of assuming readers know what it is.
Make the example more realistic.

Also update the docs after fixing vaadin/flow#8551 and vaadin/flow#8552

Fixes #1291
Fixes #1292

* reveiew comments

 - move the 'note' block from the beginning to the point when custom client-side validators are introduced
 - move the 'required field' section to under the custom validators section

Co-authored-by: Anton Platonov <anton@vaadin.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DX hilla Issues related to Hilla
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants