UNPKG

@open-formulieren/formio-builder

Version:

An opinionated Formio webform builder for Open Forms

37 lines (36 loc) 1.44 kB
import { z } from 'zod'; import { buildCommonSchema } from '../../registry/validation'; const buildTime24hSchema = (intl) => z.string().refine(value => { const time24hFormat = /^([01]?[0-9]|2[0-3]):[0-5][0-9]$/; return time24hFormat.test(value); }, { message: intl.formatMessage({ id: "klYCow", defaultMessage: [{ type: 0, value: "The time must a valid time in the HH:mm format." }] }), }); const buildOptionalTimeSchema = (intl) => z.union([ buildTime24hSchema(intl), z.literal(''), z.undefined(), z.null(), ]); // case for when component.multiple=false const buildSingleValueSchema = (intl) => z .object({ multiple: z.literal(false) }) .and(z.object({ defaultValue: buildOptionalTimeSchema(intl) })); // case for when component.multiple=true const buildMultipleValueSchema = (intl) => z .object({ multiple: z.literal(true) }) .and(z.object({ defaultValue: buildOptionalTimeSchema(intl).array() })); const buildTimeSpecific = (intl) => z.object({ validate: z .object({ minTime: buildOptionalTimeSchema(intl), maxTime: buildOptionalTimeSchema(intl), }) .optional(), }); const schema = ({ intl }) => { const commonSchema = buildCommonSchema(intl); const defaultValueSchema = buildSingleValueSchema(intl).or(buildMultipleValueSchema(intl)); return commonSchema.and(defaultValueSchema).and(buildTimeSpecific(intl)); }; export default schema;