Skip to content

Commit

Permalink
Merge pull request #27 from formio/adding-processors
Browse files Browse the repository at this point in the history
Adding processors for server-side data processing.
  • Loading branch information
brendanbond authored Jan 30, 2024
2 parents c307551 + 8e201f6 commit d1d59fe
Show file tree
Hide file tree
Showing 142 changed files with 5,168 additions and 1,850 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@formio/core",
"version": "2.0.0-rc.2",
"version": "2.0.0-rc.9",
"description": "The core Form.io renderering framework.",
"main": "lib/index.js",
"exports": {
Expand Down Expand Up @@ -45,7 +45,6 @@
],
"homepage": "https://github.com/formio/core#readme",
"devDependencies": {
"@formio/js": "^5.0.0-rc.30",
"@types/chai": "^4.3.10",
"@types/chance": "^1.1.6",
"@types/dompurify": "^3.0.5",
Expand Down Expand Up @@ -90,6 +89,7 @@
"eventemitter3": "^5.0.0",
"fetch-ponyfill": "^7.1.0",
"json-logic-js": "^2.0.2",
"lodash": "^4.17.21"
"lodash": "^4.17.21",
"moment": "^2.29.4"
}
}
3 changes: 3 additions & 0 deletions src/error/FieldError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type FieldErrorContext = ValidationContext & {
// * context.hasLabel will determine how the error is displayed in the `alert` template
level?: string;
hasLabel?: boolean;
setting?: string | boolean | number;
// TODO: these are the custom error properties according to the inline docs in the builer
min?: string;
max?: string;
Expand Down Expand Up @@ -38,3 +39,5 @@ export class FieldError {
}
}
}

export type InterpolateErrorFn = (text: string, context: FieldErrorContext) => string;
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ export * from './utils';
export * from './components';
export * from './process/validation';
export * from './process';
export * from './proxy';
export * from './template';
39 changes: 39 additions & 0 deletions src/modules/jsonlogic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,45 @@ class JSONLogicEvaluator extends BaseEvaluator {
}
}

export type EvaluatorContext = {
evalContext?: (context: any) => any;
instance?: any;
[key: string]: any;
};

export type EvaluatorFn = (context: EvaluatorContext) => any;

export function evaluate(
context: EvaluatorContext,
evaluation: string,
ret: string = 'result',
evalContextFn?: EvaluatorFn,
fnName?: string,
options: any = {}
) {
const { evalContext, instance } = context;
const evalContextValue = evalContext ? evalContext(context) : context;
if (evalContextFn) {
evalContextFn(evalContextValue);
}
fnName = fnName || 'evaluate';
if (instance && (instance as any)[fnName]) {
evaluation = `var ${ret}; ${ret} = ${evaluation}; return ${ret}`;
return (instance as any)[fnName](evaluation, evalContextValue, options);
}
return (JSONLogicEvaluator as any)[fnName](evaluation, evalContextValue, ret);
}

export function interpolate(
context: EvaluatorContext,
evaluation: string,
evalContextFn?: EvaluatorFn
) : string {
return evaluate(context, evaluation, undefined, evalContextFn, 'interpolate', {
noeval: true
});
}

export default {
evaluator: JSONLogicEvaluator,
jsonLogic: jsonLogic
Expand Down
230 changes: 230 additions & 0 deletions src/process/__tests__/fixtures/data1a.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
{
"_id": "65777f801c5d0de6f55e2344",
"form": "657085d01c5d0de6f55de729",
"owner": "650a5bfdb9ac8160c0968e59",
"roles": [],
"access": [],
"metadata": {
"selectData": {
"state": {
"name": "Texas"
}
},
"timezone": "America/Chicago",
"offset": -360,
"origin": "https://form.local",
"referrer": "",
"browserName": "Netscape",
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36",
"pathName": "/",
"onLine": true,
"headers": {
"host": "form.local",
"x-real-ip": "172.18.0.1",
"x-forwarded-for": "172.18.0.1",
"x-forwarded-proto": "https",
"connection": "close",
"content-length": "4723",
"sec-ch-ua": "\"Brave\";v=\"119\", \"Chromium\";v=\"119\", \"Not?A_Brand\";v=\"24\"",
"accept": "application/json",
"content-type": "application/json",
"sec-ch-ua-mobile": "?0",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36",
"sec-ch-ua-platform": "\"macOS\"",
"sec-gpc": "1",
"accept-language": "en-US,en;q=0.5",
"origin": "https://form.local",
"sec-fetch-site": "same-origin",
"sec-fetch-mode": "cors",
"sec-fetch-dest": "empty",
"referer": "https://form.local/",
"accept-encoding": "gzip, deflate, br"
}
},
"data": {
"taxRate": 0.08,
"customer": {
"firstName": "Joe",
"lastName": "Smith",
"email": "[email protected]"
},
"cc": "4444444444444",
"expires": "02/00/2030",
"state": "TX",
"zip": 75123,
"cart": [
{
"product": {
"_id": "6570852c1c5d0de6f55de6ae",
"form": "657085141c5d0de6f55de4b7",
"owner": "650a5bfdb9ac8160c0968e59",
"roles": [],
"access": [],
"metadata": {
"timezone": "America/Chicago",
"offset": -360,
"origin": "https://form.local",
"referrer": "",
"browserName": "Netscape",
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36",
"pathName": "/",
"onLine": true,
"headers": {
"host": "form.local",
"x-real-ip": "172.18.0.1",
"x-forwarded-for": "172.18.0.1",
"x-forwarded-proto": "https",
"connection": "close",
"content-length": "374",
"sec-ch-ua": "\"Brave\";v=\"117\", \"Not;A=Brand\";v=\"8\", \"Chromium\";v=\"117\"",
"accept": "application/json",
"content-type": "application/json",
"sec-ch-ua-mobile": "?0",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36",
"sec-ch-ua-platform": "\"macOS\"",
"sec-gpc": "1",
"accept-language": "en-US,en;q=0.5",
"origin": "https://form.local",
"sec-fetch-site": "same-origin",
"sec-fetch-mode": "cors",
"sec-fetch-dest": "empty",
"referer": "https://form.local/",
"accept-encoding": "gzip, deflate, br"
}
},
"data": {
"name": "C",
"sku": "C",
"price": 30
},
"_fvid": 0,
"project": "651185882e5eae71be60b314",
"state": "submitted",
"externalIds": [],
"created": "2023-12-06T14:29:00.345Z",
"modified": "2023-12-06T14:29:00.345Z"
},
"quantity": 1,
"price": 30
},
{
"product": {
"_id": "657085261c5d0de6f55de612",
"form": "657085141c5d0de6f55de4b7",
"owner": "650a5bfdb9ac8160c0968e59",
"roles": [],
"access": [],
"metadata": {
"timezone": "America/Chicago",
"offset": -360,
"origin": "https://form.local",
"referrer": "",
"browserName": "Netscape",
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36",
"pathName": "/",
"onLine": true,
"headers": {
"host": "form.local",
"x-real-ip": "172.18.0.1",
"x-forwarded-for": "172.18.0.1",
"x-forwarded-proto": "https",
"connection": "close",
"content-length": "374",
"sec-ch-ua": "\"Brave\";v=\"117\", \"Not;A=Brand\";v=\"8\", \"Chromium\";v=\"117\"",
"accept": "application/json",
"content-type": "application/json",
"sec-ch-ua-mobile": "?0",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36",
"sec-ch-ua-platform": "\"macOS\"",
"sec-gpc": "1",
"accept-language": "en-US,en;q=0.5",
"origin": "https://form.local",
"sec-fetch-site": "same-origin",
"sec-fetch-mode": "cors",
"sec-fetch-dest": "empty",
"referer": "https://form.local/",
"accept-encoding": "gzip, deflate, br"
}
},
"data": {
"name": "B",
"sku": "B",
"price": 20
},
"_fvid": 0,
"project": "651185882e5eae71be60b314",
"state": "submitted",
"externalIds": [],
"created": "2023-12-06T14:28:54.747Z",
"modified": "2023-12-06T14:28:54.748Z"
},
"quantity": 2,
"price": 20
},
{
"product": {
"_id": "6570851e1c5d0de6f55de576",
"form": "657085141c5d0de6f55de4b7",
"owner": "650a5bfdb9ac8160c0968e59",
"roles": [],
"access": [],
"metadata": {
"timezone": "America/Chicago",
"offset": -360,
"origin": "https://form.local",
"referrer": "",
"browserName": "Netscape",
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36",
"pathName": "/",
"onLine": true,
"headers": {
"host": "form.local",
"x-real-ip": "172.18.0.1",
"x-forwarded-for": "172.18.0.1",
"x-forwarded-proto": "https",
"connection": "close",
"content-length": "374",
"sec-ch-ua": "\"Brave\";v=\"117\", \"Not;A=Brand\";v=\"8\", \"Chromium\";v=\"117\"",
"accept": "application/json",
"content-type": "application/json",
"sec-ch-ua-mobile": "?0",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36",
"sec-ch-ua-platform": "\"macOS\"",
"sec-gpc": "1",
"accept-language": "en-US,en;q=0.5",
"origin": "https://form.local",
"sec-fetch-site": "same-origin",
"sec-fetch-mode": "cors",
"sec-fetch-dest": "empty",
"referer": "https://form.local/",
"accept-encoding": "gzip, deflate, br"
}
},
"data": {
"name": "A",
"sku": "A",
"price": 10
},
"_fvid": 0,
"project": "651185882e5eae71be60b314",
"state": "submitted",
"externalIds": [],
"created": "2023-12-06T14:28:46.759Z",
"modified": "2023-12-06T14:28:46.760Z"
},
"quantity": 3,
"price": 10
}
],
"subtotal": 100,
"hasTaxes": true,
"taxes": 8,
"total": 108
},
"_fvid": 0,
"project": "651185882e5eae71be60b314",
"state": "submitted",
"externalIds": [],
"created": "2023-12-11T21:30:40.162Z",
"modified": "2023-12-11T21:30:40.162Z"
}
Loading

0 comments on commit d1d59fe

Please sign in to comment.