@open-formulieren/formio-builder
Version:
An opinionated Formio webform builder for Open Forms
46 lines (45 loc) • 2.03 kB
JavaScript
import { z } from 'zod';
import { buildCommonSchema, jsonSchema, optionSchema } from '../../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) => z
.object({
data: z.object({
// *can* be empty if an itemsExpression is set, it's only added back at runtime in
// the backend
values: optionSchema(intl).array().min(1).optional(),
}),
openForms: z.object({
dataSrc: z.union([z.literal('manual'), z.literal('variable'), z.literal('referenceLists')]),
// TODO: wire up infernologic type checking
itemsExpression: jsonSchema.optional(),
service: z.string().optional(),
code: 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: 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: z.ZodIssueCode.custom,
path: ['openForms', 'code'],
message: intl.formatMessage({ id: "i/P7yw", defaultMessage: [{ type: 0, value: "You must select a table." }] }),
});
}
}
});
const schema = ({ intl }) => buildCommonSchema(intl).and(buildValuesSchema(intl));
export default schema;