From e457ce73bb4f9cf968bf8feadb970f9f8f5e864c Mon Sep 17 00:00:00 2001 From: Thijs Louisse Date: Wed, 16 Mar 2022 10:03:05 +0100 Subject: [PATCH] chore: type and document Validator --- .../form-core/src/validate/types/index.d.ts | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 packages/form-core/src/validate/types/index.d.ts diff --git a/packages/form-core/src/validate/types/index.d.ts b/packages/form-core/src/validate/types/index.d.ts new file mode 100644 index 000000000..36741865a --- /dev/null +++ b/packages/form-core/src/validate/types/index.d.ts @@ -0,0 +1,60 @@ +import { FormControlHost } from '../../FormControlMixin'; +export { ValidationType } from '../../types/ValidateMixinTypes'; + +/** + * The name under which validation results get registered. For convience and predictability, this + * should always be the same as the constructor name (since it will be obfuscated in js builds, + * we need to provide it separately). + * @example + * ```js + * class MyValidator extends Validator { + * static validatorName = 'MyValidator'; + * // etc... + * } + * ``` + */ +export type ValidatorName = string; + +/** + * The first argument of the constructor, for instance 3 in `new MinLength(3)`. Will + * be stored on Validator instance and passed to `execute` function + */ +export type ValidatorParam = any; + +/** + * The second argument of the constructor, for instance + * `new MinLength(3, {getFeedbackMessage: async () => 'too long'})`. + * Will be stored on Validator instance and passed to `execute` function + */ +export type ValidatorConfig = { + getMessage?: (data: Partial) => Promise; + type?: ValidationType; + node?: FormControlHost; + fieldName?: string | Promise; +}; + +/** + * Output of the `execute` function that returns a validity outcome. When we need to shpw feedback, + * it should return true, otherwise false. So when an error\info|warning|success message + * needs to be shown, return true. + * It's also possible to return an enum. Let's say that a phone number can have multiple + * states: 'invalid-country-code' | 'too-long' | 'too-short' + * Those states can be retrieved in the getMessage function. + */ +export type ValidatorOutcome = string | boolean; + +/** + * Data object that will be provided to `getMessage()` method + */ +export type FeedbackMessageData = { + modelValue: any; + /** Value configured in FormControl.fieldName. Contributes to accessible error messages */ + fieldName: string; + name: ValidatorName; + formControl: FormControlHost; + type: ValidationType; + /* Outcome of Validator.execute function. Can be a boolean (true) or enum */ + outcome: ValidatorOutcome; + config: ValidatorConfig; + params: ValidatorParam; +};