@query-farm/vgi 0.9.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3827,6 +3827,13 @@ class CatalogReadOnlyError extends VgiError {
3827
3827
  this.name = "CatalogReadOnlyError";
3828
3828
  }
3829
3829
  }
3830
+ class ArgumentValidationError extends VgiError {
3831
+ constructor(message) {
3832
+ super(message);
3833
+ this.name = "ArgumentValidationError";
3834
+ }
3835
+ }
3836
+
3830
3837
  class NoCatalogError extends VgiError {
3831
3838
  constructor() {
3832
3839
  super("No catalog is configured for this worker");
@@ -7044,6 +7051,55 @@ function constraintSpecFields(constraints) {
7044
7051
  }
7045
7052
  return fields;
7046
7053
  }
7054
+ function toNumber(value) {
7055
+ if (typeof value === "number")
7056
+ return value;
7057
+ if (typeof value === "bigint")
7058
+ return Number(value);
7059
+ return;
7060
+ }
7061
+ function choiceEquals(choice, value) {
7062
+ const cn = toNumber(choice);
7063
+ const vn = toNumber(value);
7064
+ if (cn !== undefined && vn !== undefined)
7065
+ return cn === vn;
7066
+ return choice === value;
7067
+ }
7068
+ function validateConstConstraints(name, constraints, value) {
7069
+ if (value === null || value === undefined)
7070
+ return;
7071
+ const num = toNumber(value);
7072
+ if (num !== undefined) {
7073
+ if (constraints.ge !== undefined && num < constraints.ge) {
7074
+ throw new ArgumentValidationError(`argument '${name}' must be >= ${constraints.ge}`);
7075
+ }
7076
+ if (constraints.le !== undefined && num > constraints.le) {
7077
+ throw new ArgumentValidationError(`argument '${name}' must be <= ${constraints.le}`);
7078
+ }
7079
+ if (constraints.gt !== undefined && num <= constraints.gt) {
7080
+ throw new ArgumentValidationError(`argument '${name}' must be > ${constraints.gt}`);
7081
+ }
7082
+ if (constraints.lt !== undefined && num >= constraints.lt) {
7083
+ throw new ArgumentValidationError(`argument '${name}' must be < ${constraints.lt}`);
7084
+ }
7085
+ }
7086
+ if (constraints.choices !== undefined && constraints.choices.length > 0) {
7087
+ if (!constraints.choices.some((c) => choiceEquals(c, value))) {
7088
+ throw new ArgumentValidationError(`argument '${name}' must be one of ${JSON.stringify([...constraints.choices])}`);
7089
+ }
7090
+ }
7091
+ if (constraints.pattern !== undefined && typeof value === "string") {
7092
+ let re;
7093
+ try {
7094
+ re = new RegExp(constraints.pattern);
7095
+ } catch {
7096
+ re = undefined;
7097
+ }
7098
+ if (re !== undefined && !re.test(value)) {
7099
+ throw new ArgumentValidationError(`argument '${name}' must match pattern ${JSON.stringify(constraints.pattern)}`);
7100
+ }
7101
+ }
7102
+ }
7047
7103
  function argumentSpecSortKey(spec) {
7048
7104
  if (typeof spec.position === "number") {
7049
7105
  return [0, spec.position];
@@ -7164,6 +7220,14 @@ function defineScalarFunction(config) {
7164
7220
  }
7165
7221
  }
7166
7222
  }
7223
+ const constConstraints = new Map;
7224
+ if (config.parameters) {
7225
+ for (const p of config.parameters) {
7226
+ if (p.const && (p.choices !== undefined || p.ge !== undefined || p.le !== undefined || p.gt !== undefined || p.lt !== undefined || p.pattern !== undefined)) {
7227
+ constConstraints.set(p.name, p);
7228
+ }
7229
+ }
7230
+ }
7167
7231
  const meta = {
7168
7232
  name: config.name,
7169
7233
  description: config.description,
@@ -7190,7 +7254,11 @@ function defineScalarFunction(config) {
7190
7254
  let constIdx = 0;
7191
7255
  for (const spec of specs) {
7192
7256
  if (spec.isConst) {
7193
- constArgs[spec.name] = request.arguments.get(constIdx, undefined);
7257
+ const value = request.arguments.get(constIdx, undefined);
7258
+ constArgs[spec.name] = value;
7259
+ const constraints = constConstraints.get(spec.name);
7260
+ if (constraints)
7261
+ validateConstConstraints(spec.name, constraints, value);
7194
7262
  constIdx++;
7195
7263
  }
7196
7264
  }
@@ -8863,4 +8931,4 @@ export {
8863
8931
  Arguments
8864
8932
  };
8865
8933
 
8866
- //# debugId=5A27AF075D6D5AD464756E2164756E21
8934
+ //# debugId=1755038A7EDE8C3064756E2164756E21