@open-formulieren/formio-builder
Version:
An opinionated Formio webform builder for Open Forms
33 lines (32 loc) • 1.58 kB
JavaScript
;
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 VALIDATION_MESSAGES = (0, react_intl_1.defineMessages)({ email: { id: "Q0SS8P", defaultMessage: [{ type: 1, value: "field" }, { type: 0, value: " must be a valid email." }] } });
const buildDefaultValueSchema = (intl) => {
const emailSchema = zod_1.z
.string({
errorMap: (0, validation_1.getErrorMap)(issue => {
if ((0, validation_1.isInvalidStringIssue)(issue) && issue.validation === 'email') {
const fieldLabel = intl.formatMessage(messages_1.LABELS.defaultValue);
return intl.formatMessage(VALIDATION_MESSAGES.email, { field: fieldLabel });
}
return;
}),
})
.email()
.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: emailSchema }));
// case for when component.multiple=true
const multipleValueSchema = zod_1.z
.object({ multiple: zod_1.z.literal(true) })
.and(zod_1.z.object({ defaultValue: emailSchema.array() }));
return singleValueSchema.or(multipleValueSchema);
};
const schema = ({ intl }) => (0, validation_1.buildCommonSchema)(intl).and(buildDefaultValueSchema(intl));
exports.default = schema;