UNPKG

@open-formulieren/formio-builder

Version:

An opinionated Formio webform builder for Open Forms

108 lines (107 loc) 4.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getErrorMap = exports.isInvalidStringIssue = exports.optionSchema = exports.jsonSchema = exports.buildCommonSchema = exports.buildKeySchema = exports.buildLabelSchema = void 0; /** * Zod schemas for builder form validation. * * TODO: check the zodErrorMap implementation & patterns in the SDK for a default error * map. */ const react_intl_1 = require("react-intl"); const zod_1 = require("zod"); /* Validation message definitions. */ const DEFAULT_MESSAGES = (0, react_intl_1.defineMessages)({ required: { id: "dazu9j", defaultMessage: [{ type: 0, value: "Label is a required field." }] }, invalidKeyPattern: { id: "h0B9Fr", defaultMessage: [{ type: 0, value: "The property name must only contain alphanumeric characters, underscores, dots and dashes and should not be ended by dash or dot." }] } }); /* Public API */ /** * Construct the localised validation schema for the component.label property. * * Component label is (typically) a required text for Form.io components. */ const buildLabelSchema = (intl) => zod_1.z.string({ errorMap: (0, exports.getErrorMap)((issue, ctx) => { const isRequiredError = issue.code === 'invalid_type' && ctx.data === undefined; if (isRequiredError) { return intl.formatMessage(DEFAULT_MESSAGES.required); } return; }), }); exports.buildLabelSchema = buildLabelSchema; /** * Construct the localised validation schema for the component.key property. * * The component key is used in the submission data and support lodash.get/set patterns. * It must allow slug-characters, amended with underscores and dots for nested data * lookups/setters. It may not end with a dash or dot for nested-data reasons. */ const buildKeySchema = (intl) => zod_1.z .string({ errorMap: (0, exports.getErrorMap)(issue => { if ((0, exports.isInvalidStringIssue)(issue) && issue.validation === 'regex') { return intl.formatMessage(DEFAULT_MESSAGES.invalidKeyPattern); } return; }), }) .regex(new RegExp('^(\\w|\\w[\\w-.]*\\w)$')); exports.buildKeySchema = buildKeySchema; /** * Construct the localised validation schema shared by (most) Form.io components. * * You can use the output of this base schema with the `.and(...)` chain for additional * component-type specific schema validations that are not/less generic in nature. */ const buildCommonSchema = (intl) => zod_1.z.object({ label: (0, exports.buildLabelSchema)(intl), key: (0, exports.buildKeySchema)(intl), }); exports.buildCommonSchema = buildCommonSchema; // From https://zod.dev/?id=json-type const literalSchema = zod_1.z.union([zod_1.z.string(), zod_1.z.number(), zod_1.z.boolean(), zod_1.z.null()]); exports.jsonSchema = zod_1.z.lazy(() => zod_1.z.union([literalSchema, zod_1.z.array(exports.jsonSchema), zod_1.z.record(exports.jsonSchema)])); // Maps to @open-formulieren/types common.ts Option type. const optionTranslationSchema = zod_1.z.object({ label: zod_1.z.string().optional(), }); const optionSchema = (intl) => zod_1.z.object({ value: zod_1.z .string({ required_error: intl.formatMessage({ id: "BKe/DT", defaultMessage: [{ type: 0, value: "The option value is a required field." }] }), }) .min(1), label: zod_1.z .string({ required_error: intl.formatMessage({ id: "arAMAF", defaultMessage: [{ type: 0, value: "The option label is a required field." }] }), }) .min(1), openForms: zod_1.z .object({ // zod doesn't seem to be able to use our supportedLanguageCodes for z.object keys, // they need to be defined statically. So, 'record' it is. translations: zod_1.z.record(zod_1.z.string(), optionTranslationSchema.optional()), }) .optional(), }); exports.optionSchema = optionSchema; /* Helpers */ const isInvalidStringIssue = (issue) => { return issue.code === 'invalid_string'; }; exports.isInvalidStringIssue = isInvalidStringIssue; const getErrorMap = (builder) => { const errorMap = (issue, ctx) => { const message = builder(issue, ctx); if (message) { return { message }; } return { message: ctx.defaultError }; }; return errorMap; }; exports.getErrorMap = getErrorMap;