diff --git a/src/registry/validation.ts b/src/registry/validation.ts index c1486892..7244d4a7 100644 --- a/src/registry/validation.ts +++ b/src/registry/validation.ts @@ -4,6 +4,7 @@ * TODO: check the zodErrorMap implementation & patterns in the SDK for a default error * map. */ +import {InferenceError} from '@open-formulieren/infernologic/lib/exceptions'; import {IntlShape, defineMessages} from 'react-intl'; import {z} from 'zod'; @@ -145,12 +146,19 @@ Related to jsonLogic export const itemsExpressionSchema = (builderContext: BuilderContextType) => jsonSchema.superRefine((val, ctx) => { - const result = builderContext.validateLogic(val, [['', '']]); - if (result !== '') { - // TODO adapt once the InferNoLogic API uses exceptions - ctx.addIssue({ - code: z.ZodIssueCode.custom, - message: result, - }); + try { + builderContext.validateLogic(val, [['', '']]); + } catch (error) { + if (error instanceof InferenceError) { + ctx.addIssue({ + code: z.ZodIssueCode.custom, + message: `Invalid JSON Logic: ${error.message}`, + }); + } else { + ctx.addIssue({ + code: z.ZodIssueCode.custom, + message: `Something went wrong when validating items expression: ${error.message}`, + }); + } } }); diff --git a/src/utils/jsonlogic.ts b/src/utils/jsonlogic.ts index dde6d88e..c3cdbad7 100644 --- a/src/utils/jsonlogic.ts +++ b/src/utils/jsonlogic.ts @@ -2,6 +2,7 @@ * JsonLogic type checking utility functions */ import {infer} from '@open-formulieren/infernologic/lib'; +import type {InferenceResult} from '@open-formulieren/infernologic/lib'; import type {JSONObject, JSONValue} from '@open-formulieren/types/lib/types'; import type {AnyComponentSchema} from '@/types'; @@ -11,7 +12,7 @@ import type {AnyComponentSchema} from '@/types'; * @param expected - example value from expected result type e.g. [["label", "value"]] * @returns error message or '' if type checks */ -export type JsonLogicTypeChecker = (logic: JSONValue, expected?: JSONValue) => string; +export type JsonLogicTypeChecker = (logic: JSONValue, expected?: JSONValue) => InferenceResult; type DataType = | 'string' @@ -88,8 +89,7 @@ export const createTypeCheck = ({ return (logic, expected = undefined) => { // We don't evaluate logic, we just look at types. // "===" forces the logic expression align with the expectancy if defined - const result = infer(expected !== undefined ? {'===': [logic, expected]} : logic, data); - return result.startsWith('result type:') ? '' : result; + return infer(expected !== undefined ? {'===': [logic, expected]} : logic, data); }; };