UNPKG

@open-formulieren/formio-builder

Version:

An opinionated Formio webform builder for Open Forms

39 lines (38 loc) 1.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const zod_1 = require("zod"); const validation_1 = require("../../registry/validation"); const buildTime24hSchema = (intl) => zod_1.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) => zod_1.z.union([ buildTime24hSchema(intl), zod_1.z.literal(''), zod_1.z.undefined(), zod_1.z.null(), ]); // case for when component.multiple=false const buildSingleValueSchema = (intl) => zod_1.z .object({ multiple: zod_1.z.literal(false) }) .and(zod_1.z.object({ defaultValue: buildOptionalTimeSchema(intl) })); // case for when component.multiple=true const buildMultipleValueSchema = (intl) => zod_1.z .object({ multiple: zod_1.z.literal(true) }) .and(zod_1.z.object({ defaultValue: buildOptionalTimeSchema(intl).array() })); const buildTimeSpecific = (intl) => zod_1.z.object({ validate: zod_1.z .object({ minTime: buildOptionalTimeSchema(intl), maxTime: buildOptionalTimeSchema(intl), }) .optional(), }); const schema = ({ intl }) => { const commonSchema = (0, validation_1.buildCommonSchema)(intl); const defaultValueSchema = buildSingleValueSchema(intl).or(buildMultipleValueSchema(intl)); return commonSchema.and(defaultValueSchema).and(buildTimeSpecific(intl)); }; exports.default = schema;