@open-formulieren/formio-builder
Version:
An opinionated Formio webform builder for Open Forms
44 lines (43 loc) • 2.05 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const zod_1 = require("zod");
const validation_1 = require("../../registry/validation");
// z.object(...).or(z.object(...)) based on openForms.dataSrc doesn't seem to work,
// looks like the union validation only works if the discriminator is in the top level
// object :(
// so we mark each aspect as optional so that *when* it is provided, we can run the
// validation
const buildValuesSchema = (intl) => zod_1.z
.object({
values: (0, validation_1.optionSchema)(intl).array().min(1).optional(),
openForms: zod_1.z.object({
dataSrc: zod_1.z.union([zod_1.z.literal('manual'), zod_1.z.literal('variable'), zod_1.z.literal('referenceLists')]),
// TODO: wire up infernologic type checking
itemsExpression: validation_1.jsonSchema.optional(),
service: zod_1.z.string().optional(),
code: zod_1.z.string().optional(),
}),
})
.superRefine((component, ctx) => {
var _a;
// validate the both service and table are selected when using reference lists
if (((_a = component === null || component === void 0 ? void 0 : component.openForms) === null || _a === void 0 ? void 0 : _a.dataSrc) === 'referenceLists') {
const { service, code } = component.openForms;
if (!service) {
ctx.addIssue({
code: zod_1.z.ZodIssueCode.custom,
path: ['openForms', 'service'],
message: intl.formatMessage({ id: "7HlMn7", defaultMessage: [{ type: 0, value: "You must select a service." }] }),
});
}
if (!code) {
ctx.addIssue({
code: zod_1.z.ZodIssueCode.custom,
path: ['openForms', 'code'],
message: intl.formatMessage({ id: "i/P7yw", defaultMessage: [{ type: 0, value: "You must select a table." }] }),
});
}
}
});
const schema = ({ intl }) => (0, validation_1.buildCommonSchema)(intl).and(buildValuesSchema(intl));
exports.default = schema;