@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.
- package/dist/arguments/argument-spec.d.ts +7 -0
- package/dist/index.js +63 -2
- package/dist/index.js.map +4 -4
- package/dist/worker-cf-entry.js +70 -2
- package/dist/worker-cf-entry.js.map +4 -4
- package/package.json +1 -1
- package/src/arguments/argument-spec.ts +68 -0
- package/src/functions/scalar.ts +31 -2
|
@@ -58,6 +58,13 @@ export declare function formatRange(ge?: number, le?: number, gt?: number, lt?:
|
|
|
58
58
|
* no field). Mirrors Python's `_constraint_kwargs`.
|
|
59
59
|
*/
|
|
60
60
|
export declare function constraintSpecFields(constraints: ArgumentConstraints | undefined): Pick<ArgumentSpec, "defaultJson" | "choicesJson" | "rangeNotation" | "pattern">;
|
|
61
|
+
/**
|
|
62
|
+
* Enforce a bind-time const value against its declared {@link ArgumentConstraints},
|
|
63
|
+
* throwing {@link ArgumentValidationError} on violation. Mirrors the Python SDK's
|
|
64
|
+
* `Arg._validate`: numeric range (`ge`/`le`/`gt`/`lt`), closed choice set, and
|
|
65
|
+
* regex `pattern`. A `null`/`undefined` value skips its value constraints.
|
|
66
|
+
*/
|
|
67
|
+
export declare function validateConstConstraints(name: string, constraints: ArgumentConstraints, value: unknown): void;
|
|
61
68
|
/**
|
|
62
69
|
* Convert ArgumentSpecs to an Arrow Schema with VGI metadata keys.
|
|
63
70
|
* Positional arguments come first (in order), named arguments follow.
|
package/dist/index.js
CHANGED
|
@@ -2717,6 +2717,55 @@ function constraintSpecFields(constraints) {
|
|
|
2717
2717
|
}
|
|
2718
2718
|
return fields;
|
|
2719
2719
|
}
|
|
2720
|
+
function toNumber(value) {
|
|
2721
|
+
if (typeof value === "number")
|
|
2722
|
+
return value;
|
|
2723
|
+
if (typeof value === "bigint")
|
|
2724
|
+
return Number(value);
|
|
2725
|
+
return;
|
|
2726
|
+
}
|
|
2727
|
+
function choiceEquals(choice, value) {
|
|
2728
|
+
const cn = toNumber(choice);
|
|
2729
|
+
const vn = toNumber(value);
|
|
2730
|
+
if (cn !== undefined && vn !== undefined)
|
|
2731
|
+
return cn === vn;
|
|
2732
|
+
return choice === value;
|
|
2733
|
+
}
|
|
2734
|
+
function validateConstConstraints(name, constraints, value) {
|
|
2735
|
+
if (value === null || value === undefined)
|
|
2736
|
+
return;
|
|
2737
|
+
const num = toNumber(value);
|
|
2738
|
+
if (num !== undefined) {
|
|
2739
|
+
if (constraints.ge !== undefined && num < constraints.ge) {
|
|
2740
|
+
throw new ArgumentValidationError(`argument '${name}' must be >= ${constraints.ge}`);
|
|
2741
|
+
}
|
|
2742
|
+
if (constraints.le !== undefined && num > constraints.le) {
|
|
2743
|
+
throw new ArgumentValidationError(`argument '${name}' must be <= ${constraints.le}`);
|
|
2744
|
+
}
|
|
2745
|
+
if (constraints.gt !== undefined && num <= constraints.gt) {
|
|
2746
|
+
throw new ArgumentValidationError(`argument '${name}' must be > ${constraints.gt}`);
|
|
2747
|
+
}
|
|
2748
|
+
if (constraints.lt !== undefined && num >= constraints.lt) {
|
|
2749
|
+
throw new ArgumentValidationError(`argument '${name}' must be < ${constraints.lt}`);
|
|
2750
|
+
}
|
|
2751
|
+
}
|
|
2752
|
+
if (constraints.choices !== undefined && constraints.choices.length > 0) {
|
|
2753
|
+
if (!constraints.choices.some((c) => choiceEquals(c, value))) {
|
|
2754
|
+
throw new ArgumentValidationError(`argument '${name}' must be one of ${JSON.stringify([...constraints.choices])}`);
|
|
2755
|
+
}
|
|
2756
|
+
}
|
|
2757
|
+
if (constraints.pattern !== undefined && typeof value === "string") {
|
|
2758
|
+
let re;
|
|
2759
|
+
try {
|
|
2760
|
+
re = new RegExp(constraints.pattern);
|
|
2761
|
+
} catch {
|
|
2762
|
+
re = undefined;
|
|
2763
|
+
}
|
|
2764
|
+
if (re !== undefined && !re.test(value)) {
|
|
2765
|
+
throw new ArgumentValidationError(`argument '${name}' must match pattern ${JSON.stringify(constraints.pattern)}`);
|
|
2766
|
+
}
|
|
2767
|
+
}
|
|
2768
|
+
}
|
|
2720
2769
|
function argumentSpecSortKey(spec) {
|
|
2721
2770
|
if (typeof spec.position === "number") {
|
|
2722
2771
|
return [0, spec.position];
|
|
@@ -2884,6 +2933,14 @@ function defineScalarFunction(config) {
|
|
|
2884
2933
|
}
|
|
2885
2934
|
}
|
|
2886
2935
|
}
|
|
2936
|
+
const constConstraints = new Map;
|
|
2937
|
+
if (config.parameters) {
|
|
2938
|
+
for (const p of config.parameters) {
|
|
2939
|
+
if (p.const && (p.choices !== undefined || p.ge !== undefined || p.le !== undefined || p.gt !== undefined || p.lt !== undefined || p.pattern !== undefined)) {
|
|
2940
|
+
constConstraints.set(p.name, p);
|
|
2941
|
+
}
|
|
2942
|
+
}
|
|
2943
|
+
}
|
|
2887
2944
|
const meta = {
|
|
2888
2945
|
name: config.name,
|
|
2889
2946
|
description: config.description,
|
|
@@ -2910,7 +2967,11 @@ function defineScalarFunction(config) {
|
|
|
2910
2967
|
let constIdx = 0;
|
|
2911
2968
|
for (const spec of specs) {
|
|
2912
2969
|
if (spec.isConst) {
|
|
2913
|
-
|
|
2970
|
+
const value = request.arguments.get(constIdx, undefined);
|
|
2971
|
+
constArgs[spec.name] = value;
|
|
2972
|
+
const constraints = constConstraints.get(spec.name);
|
|
2973
|
+
if (constraints)
|
|
2974
|
+
validateConstConstraints(spec.name, constraints, value);
|
|
2914
2975
|
constIdx++;
|
|
2915
2976
|
}
|
|
2916
2977
|
}
|
|
@@ -10996,4 +11057,4 @@ export {
|
|
|
10996
11057
|
ArgumentValidationError
|
|
10997
11058
|
};
|
|
10998
11059
|
|
|
10999
|
-
//# debugId=
|
|
11060
|
+
//# debugId=4ECD85EFC57F269A64756E2164756E21
|