Skip to content

Commit

Permalink
Compile in as (= ANY($singleListBinding))
Browse files Browse the repository at this point in the history
Change-type: patch
  • Loading branch information
otaviojacobi committed Jan 21, 2025
1 parent 33bb115 commit 2c4db28
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
"generate-types": "node ./bin/sbvr-compiler.js generate-types ./src/sbvr-api/user.sbvr ./src/sbvr-api/user.ts && node ./bin/sbvr-compiler.js generate-types ./src/migrator/migrations.sbvr ./src/migrator/migrations.ts && node ./bin/sbvr-compiler.js generate-types ./src/sbvr-api/dev.sbvr ./src/sbvr-api/dev.ts && node ./bin/sbvr-compiler.js generate-types ./src/tasks/tasks.sbvr ./src/tasks/tasks.ts && balena-lint -t tsconfig.dev.json --fix ./src/sbvr-api/user.ts ./src/migrator/migrations.ts ./src/sbvr-api/dev.ts"
},
"dependencies": {
"@balena/abstract-sql-compiler": "^10.1.0",
"@balena/abstract-sql-compiler": "10.2.0-build-compile-any-node-as-operand-77c25f0bfe74e1d1b8c5c46dddb5c14658c7144c-1",
"@balena/abstract-sql-to-typescript": "^5.1.0",
"@balena/env-parsing": "^1.2.0",
"@balena/lf-to-abstract-sql": "^5.0.3",
"@balena/odata-parser": "^3.1.2",
"@balena/odata-to-abstract-sql": "^7.0.1",
"@balena/odata-parser": "4.0.0-build-compile-in-as-eq-any-920795378e7f3a4c819d178be879570f48ebc3cb-1",
"@balena/odata-to-abstract-sql": "7.1.0-build-parse-in-as-eq-any-ede1d0fa541f53ca2ae55cd443490ac2cd0e1c11-1",
"@balena/sbvr-parser": "^1.4.6",
"@balena/sbvr-types": "^9.1.0",
"@types/body-parser": "^1.19.5",
Expand Down
33 changes: 24 additions & 9 deletions src/sbvr-api/abstract-sql.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import _ from 'lodash';

import type { Engines } from '@balena/abstract-sql-compiler';
import AbstractSQLCompiler from '@balena/abstract-sql-compiler';
import type { BindKey } from '@balena/odata-parser';
import {
Expand Down Expand Up @@ -78,7 +79,7 @@ export const getAndCheckBindValues = async (
return await Promise.all(
bindings.map(async (binding) => {
let fieldName = '';
let field: { dataType: string };
let dataType: string;
let value: any;
if (binding[0] === 'Bind') {
const bindValue = binding[1];
Expand All @@ -103,7 +104,7 @@ export const getAndCheckBindValues = async (
if (maybeField == null) {
throw new Error(`Could not find field '${fieldName}'`);
}
field = maybeField;
dataType = maybeField.dataType;
} else if (Number.isInteger(bindValue)) {
if (bindValue >= odataBinds.length) {
console.error(
Expand All @@ -112,9 +113,7 @@ export const getAndCheckBindValues = async (
);
throw new Error('Invalid binding');
}
let dataType;
[dataType, value] = odataBinds[bindValue];
field = { dataType };
} else if (typeof bindValue === 'string') {
if (!Object.hasOwn(odataBinds, bindValue)) {
console.error(
Expand All @@ -123,31 +122,47 @@ export const getAndCheckBindValues = async (
);
throw new Error('Invalid binding');
}
let dataType;
[dataType, value] = odataBinds[bindValue as BindKey];
field = { dataType };
} else {
throw new Error(`Unknown binding: ${binding}`);
}
} else {
let dataType;
[dataType, value] = binding;
field = { dataType };
}

if (value === undefined) {
throw new Error(`Bind value cannot be undefined: ${binding}`);
}

try {
return await AbstractSQLCompiler[engine].dataTypeValidate(value, field);
return await validateBindingType(engine, value, dataType);
} catch (err: any) {
throw new BadRequestError(`"${fieldName}" ${err.message}`);
}
}),
);
};

const validateBindingType = async (
engine: Engines,
$value: any,
$dataType: string,
): Promise<any> => {
if ($dataType === 'List') {
if (!Array.isArray($value)) {
throw new Error('List value binding must be an array');
}
return await Promise.all(
$value.map(async ([dataType, value]: [string, any]) => {
return await validateBindingType(engine, value, dataType);
}),
);
}
return await AbstractSQLCompiler[engine].dataTypeValidate($value, {
dataType: $dataType,
});
};

const checkModifiedFields = (
ruleReferencedFields: AbstractSQLCompiler.RuleReferencedFields,
modifiedFields: AbstractSQLCompiler.ModifiedFields,
Expand Down

0 comments on commit 2c4db28

Please sign in to comment.