Skip to content

Commit 91652c2

Browse files
committed
Merge branch 'master' of github.com:formio/core into fix-truncate-multiple-spaces
2 parents 5033358 + 93bcd9a commit 91652c2

24 files changed

+321
-198
lines changed

src/error/DereferenceError.ts

-1
This file was deleted.

src/error/ProcessorError.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { ProcessorContext } from "types";
2+
export class ProcessorError extends Error {
3+
context: Omit<ProcessorContext<any>, 'scope'>;
4+
constructor(message: string, context: ProcessorContext<any>, processor: string = 'unknown') {
5+
super(message);
6+
this.message = `${message}\nin ${processor} at ${context.path}`;
7+
const { component, path, data, row } = context;
8+
this.context = {component, path, data, row};
9+
}
10+
};

src/error/ValidatorError.ts

-1
This file was deleted.

src/error/index.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
export * from './FieldError';
2-
export * from './ValidatorError';
3-
export * from './DereferenceError';
2+
export * from './ProcessorError';

src/modules/jsonlogic/index.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ export type EvaluatorContext = {
2828
export type EvaluatorFn = (context: EvaluatorContext) => any;
2929

3030
export function evaluate(
31-
context: EvaluatorContext,
32-
evaluation: string,
33-
ret: string = 'result',
31+
context: EvaluatorContext,
32+
evaluation: string,
33+
ret: string = 'result',
3434
evalContextFn?: EvaluatorFn,
3535
fnName?: string,
3636
options: any = {}
@@ -49,8 +49,8 @@ export function evaluate(
4949
}
5050

5151
export function interpolate(
52-
context: EvaluatorContext,
53-
evaluation: string,
52+
context: EvaluatorContext,
53+
evaluation: string,
5454
evalContextFn?: EvaluatorFn
5555
) : string {
5656
return evaluate(context, evaluation, undefined, evalContextFn, 'interpolate', {

src/process/dereference/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DereferenceError } from "error";
1+
import { ProcessorError } from "error";
22
import {
33
ProcessorFn,
44
ProcessorScope,
@@ -37,7 +37,7 @@ export const dereferenceProcess: ProcessorFn<DereferenceScope> = async (context)
3737
return;
3838
}
3939
if (!config?.database) {
40-
throw new DereferenceError('Cannot dereference resource value without a database config object');
40+
throw new ProcessorError('Cannot dereference resource value without a database config object', context, 'dereference');
4141
}
4242

4343
try {
@@ -49,7 +49,7 @@ export const dereferenceProcess: ProcessorFn<DereferenceScope> = async (context)
4949
component.components = vmCompatibleComponents;
5050
}
5151
catch (err: any) {
52-
throw new DereferenceError(err.message || err);
52+
throw new ProcessorError(err.message || err, context, 'dereference');
5353
}
5454
}
5555

src/process/validation/rules/__tests__/validateRequired.test.ts

+52-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { expect } from 'chai';
22

33
import { FieldError } from 'error';
44
import { validateRequired } from '../validateRequired';
5-
import { conditionallyHiddenRequiredHiddenField, hiddenRequiredField, requiredNonInputField, simpleTextField } from './fixtures/components';
5+
6+
import { conditionallyHiddenRequiredHiddenField, hiddenRequiredField, requiredNonInputField, simpleTextField, simpleSelectBoxes, simpleRadioField } from './fixtures/components';
67
import { processOne } from 'processes/processOne';
78
import { generateProcessorContext } from './fixtures/util';
89
import { ProcessorsContext, ValidationScope } from 'types';
@@ -25,6 +26,56 @@ it('Validating a simple component that is required and present in the data will
2526
expect(result).to.equal(null);
2627
});
2728

29+
30+
it('Validating a simple radio component that is required and present in the data with value set to false will return null', async () => {
31+
const component = { ...simpleRadioField, validate: { required: true }, values: [
32+
{
33+
label: 'Yes',
34+
value: 'true',
35+
},
36+
{
37+
label: 'No',
38+
value: 'false',
39+
}] };
40+
const data = { component: false };
41+
const context = generateProcessorContext(component, data);
42+
const result = await validateRequired(context);
43+
expect(result).to.equal(null);
44+
});
45+
46+
47+
it('Validating a simple selectbox that is required and present in the data with value set to zero will return null', async () => {
48+
const component = { ...simpleSelectBoxes, validate: { required: true }, values: [
49+
{
50+
label: 'true',
51+
value: 'true',
52+
},
53+
{
54+
label: 'Null',
55+
value: '0',
56+
}] };
57+
const data = { component: 0 };
58+
const context = generateProcessorContext(component, data);
59+
const result = await validateRequired(context);
60+
expect(result).to.equal(null);
61+
});
62+
63+
it('Validating a simple selectbox that is required and present in the data with value set to false will return null', async () => {
64+
const component = { ...simpleSelectBoxes, validate: { required: true }, values: [
65+
{
66+
label: 'true',
67+
value: 'true',
68+
},
69+
{
70+
label: 'false',
71+
value: 'false',
72+
}] };
73+
const data = { component: false };
74+
const context = generateProcessorContext(component, data);
75+
const result = await validateRequired(context);
76+
expect(result).to.equal(null);
77+
});
78+
2879
it('Validating a simple component that is not required and present in the data will return null', async () => {
2980
const component = simpleTextField;
3081
const data = { component: 'a simple value' };

0 commit comments

Comments
 (0)