From eb18e6c63d431c68db3bbbc05ee98020f1572506 Mon Sep 17 00:00:00 2001 From: Steven Bal Date: Tue, 13 Aug 2024 10:17:51 +0200 Subject: [PATCH 1/3] :bug: [open-formulieren/open-forms#4420] Fix AddressNL validation type to allow Validate.useManageValidatorsTranslations typing to be correct in the formio builder --- src/formio/components/addressNL.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/formio/components/addressNL.ts b/src/formio/components/addressNL.ts index b9029b2..fb2032e 100644 --- a/src/formio/components/addressNL.ts +++ b/src/formio/components/addressNL.ts @@ -1,4 +1,5 @@ import {InputComponentSchema} from '..'; +import {HasValidation} from '../base'; import {ComponentTranslations, ErrorTranslations} from '../i18n'; type Validator = 'required'; @@ -15,7 +16,7 @@ export interface AddressData { } export interface ComponentValidation { - validate: {pattern: string}; + validate: HasValidation<'pattern'>; translatedErrors: ErrorTranslations; } From 44c42c7f06c9a42c3de7d02575773544df00db6f Mon Sep 17 00:00:00 2001 From: Sergei Maertens Date: Thu, 15 Aug 2024 11:30:09 +0200 Subject: [PATCH 2/3] :sparkles: [open-formulieren/open-forms#4420] Add ability to omit 'plugins' from validate object --- src/formio/base.ts | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/formio/base.ts b/src/formio/base.ts index 3204589..ba8e8cd 100644 --- a/src/formio/base.ts +++ b/src/formio/base.ts @@ -10,8 +10,40 @@ import { // Refinements of form.io 'builtin' types. -export interface HasValidation { - validate?: OFValidateOptions; +/** + * The `HasValidation` interfaces encapsulates properties involved in validation. + * + * - The `validate` property defines the types for each possible Formio validator, e.g. + * a `pattern` must be a string, while a `maxLength` must be a number. Open Forms + * also supports backend validators that are called async, which is specified as a + * list of strings for the validator names. + * - The `errors` property defines the (translated) error messages that may be returned + * by the backend, at runtime. The possible keys are coupled with the possible + * validator names in the `validate` property. The resulting strings are the strings + * that are ultimately presented to the end user. + * - The `translatedErrors` property is used to store the translated error messages. The + * keys are the supported language codes, the values have the same shape as the + * `errors` property. Effectively, at runtime, this object is assigned for the active + * language: `Object.assign(obj.errors, obj.translatedErrors[activeLanguage])`. + * + * There are some generics involed: + * + * - `VN`: the relevant validator names. Most components only use a small subset of + * validator options depending on their type. E.g. a `pattern` makes no sense for a + * number field, only for textfield/textarea etc. Likewise, `max` only has meaning for + * numbers, but not for strings. Typically you pass in a union: + * `'pattern' | 'maxLength'`. This generic is then used to populate the `errors` and + * `translatedErrors` objects with only the relevant keys. + * - `WithPlugins` - most components support plugin validation, but the error messages + * come from the server. The `plugins` key is never included in the `errors` and + * `translatedErrors` objects. Pass `false` if plugin validation is not available for + * the component. + */ +export interface HasValidation< + VN extends CuratedValidatorNames, + WithPlugins extends boolean = true +> { + validate?: OFValidateOptions; errors?: ComponentErrors>; translatedErrors?: ErrorTranslations>; } From 6357bd4fa04e4484a29c0bb185d4f82e5fff6597 Mon Sep 17 00:00:00 2001 From: Sergei Maertens Date: Thu, 15 Aug 2024 11:34:24 +0200 Subject: [PATCH 3/3] :sparkles: [open-formulieren/open-forms#4420] Simplify sub component type definition The validation structure/mechanism now makes use of the HasValidation helper type, but disables the 'plugins' validators which are not used or relevant in this context. --- src/formio/components/addressNL.ts | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/formio/components/addressNL.ts b/src/formio/components/addressNL.ts index fb2032e..b1598a1 100644 --- a/src/formio/components/addressNL.ts +++ b/src/formio/components/addressNL.ts @@ -1,6 +1,5 @@ -import {InputComponentSchema} from '..'; -import {HasValidation} from '../base'; -import {ComponentTranslations, ErrorTranslations} from '../i18n'; +import {HasValidation, InputComponentSchema} from '..'; +import {ComponentTranslations} from '../i18n'; type Validator = 'required'; type TranslatableKeys = 'label' | 'description' | 'tooltip'; @@ -15,14 +14,9 @@ export interface AddressData { secretStreetCity?: string; } -export interface ComponentValidation { - validate: HasValidation<'pattern'>; - translatedErrors: ErrorTranslations; -} - export interface AddressComponents { - postcode?: ComponentValidation; - city?: ComponentValidation; + postcode?: HasValidation<'pattern', false>; + city?: HasValidation<'pattern', false>; } export type AddressNLInputSchema = InputComponentSchema;