UNPKG

@open-formulieren/formio-builder

Version:

An opinionated Formio webform builder for Open Forms

34 lines (33 loc) 1.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const react_intl_1 = require("react-intl"); const zod_1 = require("zod"); const messages_1 = require("../../components/builder/messages"); const validation_1 = require("../../registry/validation"); const constants_1 = require("./constants"); const VALIDATION_MESSAGES = (0, react_intl_1.defineMessages)({ postcode: { id: "4CuHYk", defaultMessage: [{ type: 1, value: "field" }, { type: 0, value: " must be a valid postcode." }] } }); const buildDefaultValueSchema = (intl) => { const postcodeSchema = zod_1.z .string({ errorMap: (0, validation_1.getErrorMap)(issue => { if ((0, validation_1.isInvalidStringIssue)(issue) && issue.validation === 'regex') { const fieldLabel = intl.formatMessage(messages_1.LABELS.defaultValue); return intl.formatMessage(VALIDATION_MESSAGES.postcode, { field: fieldLabel }); } return; }), }) .regex(new RegExp(constants_1.POSTCODE_REGEX)) .optional(); // case for when component.multiple=false const singleValueSchema = zod_1.z .object({ multiple: zod_1.z.literal(false) }) .and(zod_1.z.object({ defaultValue: postcodeSchema })); // case for when component.multiple=true const multipleValueSchema = zod_1.z .object({ multiple: zod_1.z.literal(true) }) .and(zod_1.z.object({ defaultValue: postcodeSchema.array() })); return singleValueSchema.or(multipleValueSchema); }; const schema = ({ intl }) => (0, validation_1.buildCommonSchema)(intl).and(buildDefaultValueSchema(intl)); exports.default = schema;