Browse Source

fix: allow args with different types

pull/7446/head
Pranav C 10 months ago
parent
commit
5e6aacb0ca
  1. 58
      packages/nocodb-sdk/src/lib/formulaHelpers.ts

58
packages/nocodb-sdk/src/lib/formulaHelpers.ts

@ -267,7 +267,9 @@ interface FormulaMeta {
max?: number; max?: number;
rqd?: number; rqd?: number;
type?: FormulaDataTypes; // array of allowed types when args types are not same
// types should be in order of args
type?: FormulaDataTypes | FormulaDataTypes[];
}; };
custom?: (args: FormulaDataTypes[], parseTree: any) => void; custom?: (args: FormulaDataTypes[], parseTree: any) => void;
}; };
@ -956,38 +958,11 @@ export const formulas: Record<string, FormulaMeta> = {
args: { args: {
min: 2, min: 2,
max: 3, max: 3,
}, type: [
custom(argTypes: FormulaDataTypes[], parsedTree) { FormulaDataTypes.STRING,
if (argTypes[0] !== FormulaDataTypes.STRING) { FormulaDataTypes.NUMERIC,
throw new FormulaError( FormulaDataTypes.NUMERIC,
FormulaErrorType.INVALID_ARG, ],
{
key: 'msg.formula.stringTypeIsExpected',
calleeName: parsedTree.callee?.name?.toUpperCase(),
},
'String type is expected'
);
}
if (argTypes[1] !== FormulaDataTypes.NUMERIC) {
throw new FormulaError(
FormulaErrorType.INVALID_ARG,
{
key: 'msg.formula.numericTypeIsExpected',
calleeName: parsedTree.callee?.name?.toUpperCase(),
},
'Numeric type is expected'
);
}
if (argTypes[2] && argTypes[2] !== FormulaDataTypes.NUMERIC) {
throw new FormulaError(
FormulaErrorType.INVALID_ARG,
{
key: 'msg.formula.numericTypeIsExpected',
calleeName: parsedTree.callee?.name?.toUpperCase(),
},
'Numeric type is expected'
);
}
}, },
}, },
description: description:
@ -1007,6 +982,11 @@ export const formulas: Record<string, FormulaMeta> = {
validation: { validation: {
args: { args: {
rqd: 3, rqd: 3,
type: [
FormulaDataTypes.STRING,
FormulaDataTypes.NUMERIC,
FormulaDataTypes.NUMERIC,
],
}, },
custom(argTypes: FormulaDataTypes[], parsedTree) { custom(argTypes: FormulaDataTypes[], parsedTree) {
if (argTypes[0] !== FormulaDataTypes.STRING) { if (argTypes[0] !== FormulaDataTypes.STRING) {
@ -1758,9 +1738,16 @@ export async function validateFormulaAndExtractTreeWithType({
} }
// validate against expected arg types if present // validate against expected arg types if present
else if (formulas[calleeName].validation?.args?.type) { else if (formulas[calleeName].validation?.args?.type) {
const expectedArgType = formulas[calleeName].validation.args.type; for (let i = 0; i < validateResult.length; i++) {
const argPt = validateResult[i];
// if type
const expectedArgType = Array.isArray(
formulas[calleeName].validation.args.type
)
? formulas[calleeName].validation.args.type[i]
: formulas[calleeName].validation.args.type;
for (const argPt of validateResult) {
if ( if (
argPt.dataType !== expectedArgType && argPt.dataType !== expectedArgType &&
argPt.dataType !== FormulaDataTypes.NULL && argPt.dataType !== FormulaDataTypes.NULL &&
@ -1786,7 +1773,6 @@ export async function validateFormulaAndExtractTreeWithType({
} else { } else {
let key = '', let key = '',
message = 'Invalid argument type'; message = 'Invalid argument type';
if (expectedArgType === FormulaDataTypes.NUMERIC) { if (expectedArgType === FormulaDataTypes.NUMERIC) {
key = 'msg.formula.numericTypeIsExpected'; key = 'msg.formula.numericTypeIsExpected';
message = 'Numeric type is expected'; message = 'Numeric type is expected';

Loading…
Cancel
Save