Browse Source

fix: formula if condition validation correction

pull/7268/head
Pranav C 1 year ago
parent
commit
a4df91819f
  1. 27
      packages/nc-gui/components/smartsheet/column/FormulaOptions.vue
  2. 9
      packages/nocodb-sdk/src/lib/formulaHelpers.ts
  3. 2
      packages/nocodb/src/db/functionMappings/commonFns.ts

27
packages/nc-gui/components/smartsheet/column/FormulaOptions.vue

@ -10,7 +10,7 @@ import {
isSystemColumn, isSystemColumn,
jsepCurlyHook, jsepCurlyHook,
substituteColumnIdWithAliasInFormula, substituteColumnIdWithAliasInFormula,
validateDateWithUnknownFormat, validateFormulaAndExtractTreeWithType
} from 'nocodb-sdk' } from 'nocodb-sdk'
import { import {
MetaInj, MetaInj,
@ -99,10 +99,15 @@ const validators = {
validator: (_: any, formula: any) => { validator: (_: any, formula: any) => {
return new Promise<void>((resolve, reject) => { return new Promise<void>((resolve, reject) => {
if (!formula?.trim()) return reject(new Error('Required')) if (!formula?.trim()) return reject(new Error('Required'))
const res = parseAndValidateFormula(formula)
if (res !== true) { try {
return reject(new Error(res)) validateFormulaAndExtractTreeWithType(formula, supportedColumns.value)
} catch (e: any) {
return reject(new Error(e.message))
} }
// if (res !== true) {
// return reject(new Error(res))
// }
resolve() resolve()
}) })
}, },
@ -226,7 +231,7 @@ function validateAgainstMeta(parsedTree: any, errors = new Set(), typeErrors = n
return validateAgainstMeta(arg, errors, typeErrors) return validateAgainstMeta(arg, errors, typeErrors)
}) })
const argsTypes = validateResult.map((v: any) => v.returnType); const argsTypes = validateResult.map((v: any) => v.returnType)
if (typeof validateResult[0].returnType === 'function') { if (typeof validateResult[0].returnType === 'function') {
returnType = formulas[calleeName].returnType(argsTypes) returnType = formulas[calleeName].returnType(argsTypes)
@ -266,7 +271,9 @@ function validateAgainstMeta(parsedTree: any, errors = new Set(), typeErrors = n
typeErrors, typeErrors,
) )
} else { } else {
parsedTree.arguments.map((arg: Record<string, any>) => validateAgainstType(arg, expectedType, null, typeErrors, argsTypes)) parsedTree.arguments.map((arg: Record<string, any>) =>
validateAgainstType(arg, expectedType, null, typeErrors, argsTypes),
)
} }
} else if (expectedType === formulaTypes.DATE) { } else if (expectedType === formulaTypes.DATE) {
if (calleeName === 'DATEADD') { if (calleeName === 'DATEADD') {
@ -488,7 +495,13 @@ function validateAgainstMeta(parsedTree: any, errors = new Set(), typeErrors = n
return { errors, returnType } return { errors, returnType }
} }
function validateAgainstType(parsedTree: any, expectedType: string, func: any, typeErrors = new Set(), argTypes: formulaTypes = []) { function validateAgainstType(
parsedTree: any,
expectedType: string,
func: any,
typeErrors = new Set(),
argTypes: formulaTypes = [],
) {
let type let type
if (parsedTree === false || typeof parsedTree === 'undefined') { if (parsedTree === false || typeof parsedTree === 'undefined') {
return typeErrors return typeErrors

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

@ -693,7 +693,7 @@ const formulas: Record<string, FormulaMeta> = {
validation: { validation: {
args: { args: {
min: 3, min: 3,
} },
}, },
description: 'Switch case value based on expr output', description: 'Switch case value based on expr output',
syntax: 'SWITCH(expr, [pattern, value, ..., default])', syntax: 'SWITCH(expr, [pattern, value, ..., default])',
@ -1169,7 +1169,7 @@ export function validateFormulaAndExtractTreeWithType(
case UITypes.Collaborator: case UITypes.Collaborator:
case UITypes.QrCode: case UITypes.QrCode:
default: default:
throw new FormulaError(FormulaErrorType.NOT_SUPPORTED, ''); throw new FormulaError(FormulaErrorType.NOT_SUPPORTED, {});
} }
} }
} else if (parsedTree.type === JSEPNode.LITERAL) { } else if (parsedTree.type === JSEPNode.LITERAL) {
@ -1188,7 +1188,10 @@ export function validateFormulaAndExtractTreeWithType(
) { ) {
res.left = validateAndExtract(parsedTree.left); res.left = validateAndExtract(parsedTree.left);
res.right = validateAndExtract(parsedTree.right); res.right = validateAndExtract(parsedTree.right);
res.dataType = FormulaDataTypes.NUMERIC;
if (['==', '<', '>', '<=', '>=', '!='].includes(parsedTree.operator)) {
res.dataType = FormulaDataTypes.COND_EXP;
} else res.dataType = FormulaDataTypes.NUMERIC;
} }
return res; return res;

2
packages/nocodb/src/db/functionMappings/commonFns.ts

@ -142,7 +142,7 @@ export default {
).builder; ).builder;
} else { } else {
thenArg = (await args.fn(args.pt.arguments[1])).builder.toQuery(); thenArg = (await args.fn(args.pt.arguments[1])).builder.toQuery();
elseArg = (await args.fn(args.pt.arguments[1])).builder.toQuery(); elseArg = (await args.fn(args.pt.arguments[2])).builder.toQuery();
} }
let query = args.knex.raw(`\n\tWHEN ${cond} THEN ${thenArg}`).toQuery(); let query = args.knex.raw(`\n\tWHEN ${cond} THEN ${thenArg}`).toQuery();

Loading…
Cancel
Save