@open-formulieren/formio-builder
Version:
An opinionated Formio webform builder for Open Forms
71 lines (70 loc) • 3.25 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const zod_1 = require("zod");
const validation_1 = require("../../registry/validation");
const dateSchema = zod_1.z.coerce.date().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: dateSchema }));
// case for when component.multiple=true
const multipleValueSchema = zod_1.z
.object({ multiple: zod_1.z.literal(true) })
.and(zod_1.z.object({ defaultValue: dateSchema.array() }));
const defaultValueSchema = singleValueSchema.or(multipleValueSchema);
// formik (deliberately) turns empty string into undefined
const noMode = zod_1.z.object({ mode: zod_1.z.union([zod_1.z.literal(undefined), zod_1.z.literal('')]) });
const future = zod_1.z.object({
mode: zod_1.z.literal('future'),
includeToday: zod_1.z.boolean(),
});
const past = zod_1.z.object({
mode: zod_1.z.literal('past'),
includeToday: zod_1.z.boolean(),
});
// XXX: requires superRefine to enforce datePicker.maxDate etc. to be set!
const fixedValue = zod_1.z.object({ mode: zod_1.z.literal('fixedValue') });
const date = zod_1.z.null().or(zod_1.z.coerce.date());
const buildRelativeToVariable = (intl) => zod_1.z.object({
mode: zod_1.z.literal('relativeToVariable'),
operator: zod_1.z.literal('add').or(zod_1.z.literal('subtract')),
variable: (0, validation_1.buildKeySchema)(intl),
delta: zod_1.z.object({
years: zod_1.z.null().or(zod_1.z.number().int().gte(0)).optional(),
months: zod_1.z.null().or(zod_1.z.number().int().gte(0)).optional(),
days: zod_1.z.null().or(zod_1.z.number().int().gte(0)).optional(),
}),
});
const buildDateSpecific = (intl) => zod_1.z
.object({
openForms: zod_1.z
.object({
minDate: zod_1.z.union([noMode, fixedValue, future, buildRelativeToVariable(intl)]).optional(),
maxDate: zod_1.z.union([noMode, fixedValue, past, buildRelativeToVariable(intl)]).optional(),
})
.optional(),
datePicker: zod_1.z
.object({
minDate: date.optional(),
maxDate: date.optional(),
})
.optional(),
})
.superRefine((component, ctx) => {
var _a, _b, _c;
for (const constraint of ['minDate', 'maxDate']) {
const isFixedMode = ((_b = (_a = component === null || component === void 0 ? void 0 : component.openForms) === null || _a === void 0 ? void 0 : _a[constraint]) === null || _b === void 0 ? void 0 : _b.mode) === 'fixedValue';
if (!isFixedMode)
continue;
const datePickerValue = (_c = component === null || component === void 0 ? void 0 : component.datePicker) === null || _c === void 0 ? void 0 : _c[constraint];
if (datePickerValue)
continue;
ctx.addIssue({
code: zod_1.z.ZodIssueCode.custom,
path: ['datePicker', constraint],
message: intl.formatMessage({ id: "RiFNuZ", defaultMessage: [{ type: 0, value: "You must specify a date." }] }),
});
}
});
const schema = ({ intl }) => (0, validation_1.buildCommonSchema)(intl).and(defaultValueSchema).and(buildDateSpecific(intl));
exports.default = schema;