UNPKG

@open-formulieren/formio-builder

Version:

An opinionated Formio webform builder for Open Forms

35 lines (34 loc) 1.43 kB
import { TILE_LAYER_RD } from '@open-formulieren/leaflet-tools'; import { z } from 'zod'; import { buildCommonSchema } from '../../registry/validation'; const buildConfigurationSchema = (intl) => z.object({ defaultZoom: z.number().int().lte(TILE_LAYER_RD.maxZoom).gte(TILE_LAYER_RD.minZoom).optional(), initialCenter: z .object({ lat: z.number().lte(90).gte(-90).optional(), lng: z.number().lte(180).gte(-180).optional(), }) .superRefine((val, ctx) => { const bothMissing = val.lat == null && val.lng == null; const bothSet = val.lat != null && val.lng != null; if (bothMissing || bothSet) { return; } const missing = val.lat == null ? 'lat' : 'lng'; ctx.addIssue({ code: z.ZodIssueCode.custom, message: intl.formatMessage({ id: "R/XadM", defaultMessage: [{ type: 0, value: "You need to configure both longitude and latitude." }] }), path: [missing], }); }) .optional(), }); const buildMapSchema = (intl) => z .object({ useConfigDefaultMapSettings: z.literal(true), defaultZoom: z.undefined().or(z.null()), initialCenter: z.undefined(), }) .or(z.object({ useConfigDefaultMapSettings: z.literal(false) }).and(buildConfigurationSchema(intl))); const schema = ({ intl }) => buildCommonSchema(intl).and(buildMapSchema(intl)); export default schema;