Skip to content

Commit

Permalink
[open-formulieren/open-forms#3597] Handle exceptions added in InferNo…
Browse files Browse the repository at this point in the history
…Logic
  • Loading branch information
Viicos committed Dec 27, 2023
1 parent 240ca31 commit 8343b96
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
22 changes: 15 additions & 7 deletions src/registry/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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}`,
});
}
}
});
6 changes: 3 additions & 3 deletions src/utils/jsonlogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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'
Expand Down Expand Up @@ -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);
};
};

Expand Down

0 comments on commit 8343b96

Please sign in to comment.