UNPKG

@open-formulieren/formio-builder

Version:

An opinionated Formio webform builder for Open Forms

31 lines (30 loc) 1.4 kB
import { defineMessages } from 'react-intl'; import { z } from 'zod'; import { LABELS } from '../../components/builder/messages'; import { buildCommonSchema, getErrorMap, isInvalidStringIssue } from '../../registry/validation'; const VALIDATION_MESSAGES = defineMessages({ email: { id: "Q0SS8P", defaultMessage: [{ type: 1, value: "field" }, { type: 0, value: " must be a valid email." }] } }); const buildDefaultValueSchema = (intl) => { const emailSchema = z .string({ errorMap: getErrorMap(issue => { if (isInvalidStringIssue(issue) && issue.validation === 'email') { const fieldLabel = intl.formatMessage(LABELS.defaultValue); return intl.formatMessage(VALIDATION_MESSAGES.email, { field: fieldLabel }); } return; }), }) .email() .optional(); // case for when component.multiple=false const singleValueSchema = z .object({ multiple: z.literal(false) }) .and(z.object({ defaultValue: emailSchema })); // case for when component.multiple=true const multipleValueSchema = z .object({ multiple: z.literal(true) }) .and(z.object({ defaultValue: emailSchema.array() })); return singleValueSchema.or(multipleValueSchema); }; const schema = ({ intl }) => buildCommonSchema(intl).and(buildDefaultValueSchema(intl)); export default schema;