@lowdefy/ajv 6.0.0 → 7.0.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/compile.js +10 -0
- package/dist/formatSchemaError.js +31 -0
- package/dist/getConfigSchemaErrors.js +147 -0
- package/dist/index.js +3 -1
- package/dist/validate.js +22 -4
- package/package.json +3 -2
package/dist/compile.js
CHANGED
|
@@ -13,8 +13,18 @@
|
|
|
13
13
|
See the License for the specific language governing permissions and
|
|
14
14
|
limitations under the License.
|
|
15
15
|
*/ import ajv from './ajvInstance.js';
|
|
16
|
+
// The shared instance keeps every schema object it compiles, keyed by the
|
|
17
|
+
// object and by its $id. Callers compile fresh copies of the same schema (a
|
|
18
|
+
// config file read again after the file cache evicts it, a step property
|
|
19
|
+
// evaluated per call), which would grow that cache for the life of the server
|
|
20
|
+
// and fail the second copy of a schema with an $id as a duplicate. The caller
|
|
21
|
+
// holds the validator; the instance keeps nothing. Boolean schemas are cached
|
|
22
|
+
// by value, so they never grow it.
|
|
16
23
|
function compile({ schema }) {
|
|
17
24
|
const validator = ajv.compile(schema);
|
|
25
|
+
if (typeof schema === 'object') {
|
|
26
|
+
ajv.removeSchema(schema);
|
|
27
|
+
}
|
|
18
28
|
return (data)=>{
|
|
19
29
|
const valid = validator(data);
|
|
20
30
|
return {
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ function formatSchemaError({ ajvError, pluginLabel, typeName, fieldLabel }) {
|
|
16
|
+
const path = ajvError.instancePath ? ajvError.instancePath.substring(1).replace(/\//g, '.') : null;
|
|
17
|
+
const fieldName = path || ajvError.params?.missingProperty || 'unknown';
|
|
18
|
+
switch(ajvError.keyword){
|
|
19
|
+
case 'type':
|
|
20
|
+
return `${pluginLabel} "${typeName}" ${fieldLabel} "${fieldName}" must be type "${ajvError.params.type}".`;
|
|
21
|
+
case 'enum':
|
|
22
|
+
return `${pluginLabel} "${typeName}" ${fieldLabel} "${fieldName}" must be one of ${JSON.stringify(ajvError.params.allowedValues)}.`;
|
|
23
|
+
case 'additionalProperties':
|
|
24
|
+
return `${pluginLabel} "${typeName}" ${fieldLabel} "${ajvError.params.additionalProperty}" is not allowed.`;
|
|
25
|
+
case 'required':
|
|
26
|
+
return `${pluginLabel} "${typeName}" required ${fieldLabel} "${ajvError.params.missingProperty}" is missing.`;
|
|
27
|
+
default:
|
|
28
|
+
return `${pluginLabel} "${typeName}" ${fieldLabel} "${fieldName}" ${ajvError.message}.`;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export default formatSchemaError;
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020-2026 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import { getOperatorType, type } from '@lowdefy/helpers';
|
|
16
|
+
import validate from './validate.js';
|
|
17
|
+
// Keywords that judge a value as a whole. An operator anywhere under the value
|
|
18
|
+
// can change which branch matches or what the value equals, so these cannot be
|
|
19
|
+
// judged before the operator evaluates.
|
|
20
|
+
const WHOLE_VALUE_KEYWORDS = new Set([
|
|
21
|
+
'anyOf',
|
|
22
|
+
'oneOf',
|
|
23
|
+
'not',
|
|
24
|
+
'if',
|
|
25
|
+
'contains',
|
|
26
|
+
'enum',
|
|
27
|
+
'const',
|
|
28
|
+
'uniqueItems'
|
|
29
|
+
]);
|
|
30
|
+
function escapePointerSegment(segment) {
|
|
31
|
+
return String(segment).replace(/~/g, '~0').replace(/\//g, '~1');
|
|
32
|
+
}
|
|
33
|
+
// A copy of the data without ~ keys (~k, ~r, ~l, ~ignoreBuildChecks), with
|
|
34
|
+
// each operator node replaced by null, and the JSON pointer of each operator
|
|
35
|
+
// node. null keeps the operator's key, so additionalProperties still reports a
|
|
36
|
+
// misspelt key whose value is an operator. A key whose config value is null
|
|
37
|
+
// is left out: Lowdefy reads a null property as unset, and an unset module var
|
|
38
|
+
// or _build.env gives null where the schema wants a value.
|
|
39
|
+
function prepareData(value, path, operatorPaths) {
|
|
40
|
+
if (type.isArray(value)) {
|
|
41
|
+
return value.map((item, index)=>prepareData(item, `${path}/${index}`, operatorPaths));
|
|
42
|
+
}
|
|
43
|
+
if (!type.isObject(value)) {
|
|
44
|
+
return value;
|
|
45
|
+
}
|
|
46
|
+
if (getOperatorType(value) !== null) {
|
|
47
|
+
operatorPaths.push(path);
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
const prepared = {};
|
|
51
|
+
for (const key of Object.keys(value)){
|
|
52
|
+
if (key.startsWith('~') || value[key] === null) continue;
|
|
53
|
+
prepared[key] = prepareData(value[key], `${path}/${escapePointerSegment(key)}`, operatorPaths);
|
|
54
|
+
}
|
|
55
|
+
return prepared;
|
|
56
|
+
}
|
|
57
|
+
function isAtOrUnder(instancePath, path) {
|
|
58
|
+
return instancePath === path || instancePath.startsWith(`${path}/`);
|
|
59
|
+
}
|
|
60
|
+
function hasOperatorUnder(error, operatorPaths) {
|
|
61
|
+
return operatorPaths.some((path)=>isAtOrUnder(path, error.instancePath));
|
|
62
|
+
}
|
|
63
|
+
// Every error, with the errors an errorMessage rewords.
|
|
64
|
+
function flattenErrors(errors) {
|
|
65
|
+
return errors.flatMap((error)=>error.keyword === 'errorMessage' ? [
|
|
66
|
+
error,
|
|
67
|
+
...flattenErrors(error.params?.errors ?? [])
|
|
68
|
+
] : [
|
|
69
|
+
error
|
|
70
|
+
]);
|
|
71
|
+
}
|
|
72
|
+
// The branches of each whole-value keyword left out: a failed anyOf, oneOf or
|
|
73
|
+
// if keeps the errors of the branches it tried, under its own schema path (an
|
|
74
|
+
// if's under its sibling then and else). Plugin block, action and operator
|
|
75
|
+
// schemas use no $ref, so a branch error's schema path sits under its keyword.
|
|
76
|
+
function findUndecidedBranches(errors, operatorPaths) {
|
|
77
|
+
return errors.filter((error)=>WHOLE_VALUE_KEYWORDS.has(error.keyword)).filter((error)=>hasOperatorUnder(error, operatorPaths)).map((error)=>{
|
|
78
|
+
if (error.keyword === 'if') {
|
|
79
|
+
const parent = error.schemaPath.slice(0, -'/if'.length);
|
|
80
|
+
return {
|
|
81
|
+
instancePath: error.instancePath,
|
|
82
|
+
schemaPaths: [
|
|
83
|
+
`${parent}/then/`,
|
|
84
|
+
`${parent}/else/`
|
|
85
|
+
]
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
instancePath: error.instancePath,
|
|
90
|
+
schemaPaths: [
|
|
91
|
+
`${error.schemaPath}/`
|
|
92
|
+
]
|
|
93
|
+
};
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
function isInBranch(error, branches) {
|
|
97
|
+
return branches.some((branch)=>isAtOrUnder(error.instancePath, branch.instancePath) && branch.schemaPaths.some((schemaPath)=>error.schemaPath.startsWith(schemaPath)));
|
|
98
|
+
}
|
|
99
|
+
function isDecidedByOperator(error, { operatorPaths, branches }) {
|
|
100
|
+
if (error.keyword === 'errorMessage') {
|
|
101
|
+
// ajv-errors replaces the errors it rewords; the message stands only if
|
|
102
|
+
// one of them does.
|
|
103
|
+
const originals = error.params?.errors ?? [];
|
|
104
|
+
return originals.every((original)=>isDecidedByOperator(original, {
|
|
105
|
+
operatorPaths,
|
|
106
|
+
branches
|
|
107
|
+
}));
|
|
108
|
+
}
|
|
109
|
+
if (operatorPaths.some((path)=>isAtOrUnder(error.instancePath, path))) {
|
|
110
|
+
return true;
|
|
111
|
+
}
|
|
112
|
+
if (WHOLE_VALUE_KEYWORDS.has(error.keyword) && hasOperatorUnder(error, operatorPaths)) {
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
return isInBranch(error, branches);
|
|
116
|
+
}
|
|
117
|
+
// The schema errors of config data that hold before its operators evaluate.
|
|
118
|
+
// Config data can hold operators anywhere, and an operator may evaluate to any
|
|
119
|
+
// value, so { _state: columns } may sit where the schema wants an array. Errors
|
|
120
|
+
// at or under an operator, and whole-value keywords above one with the errors
|
|
121
|
+
// of their branches, are left out.
|
|
122
|
+
// required and additionalProperties on a literal object still stand.
|
|
123
|
+
// Data that is itself one operator cannot be judged at all.
|
|
124
|
+
// The shared ajv instance keeps at most 20 errors per validation, and errors
|
|
125
|
+
// left out here count toward it, so data with many operators where the schema
|
|
126
|
+
// wants another type can hide a real error past the cap; it never adds one.
|
|
127
|
+
function getConfigSchemaErrors({ schema, data }) {
|
|
128
|
+
if (getOperatorType(data) !== null) {
|
|
129
|
+
return [];
|
|
130
|
+
}
|
|
131
|
+
const operatorPaths = [];
|
|
132
|
+
const prepared = prepareData(data, '', operatorPaths);
|
|
133
|
+
const result = validate({
|
|
134
|
+
schema,
|
|
135
|
+
data: prepared,
|
|
136
|
+
returnErrors: true
|
|
137
|
+
});
|
|
138
|
+
if (result.valid) {
|
|
139
|
+
return [];
|
|
140
|
+
}
|
|
141
|
+
const branches = findUndecidedBranches(flattenErrors(result.errors), operatorPaths);
|
|
142
|
+
return result.errors.filter((error)=>!isDecidedByOperator(error, {
|
|
143
|
+
operatorPaths,
|
|
144
|
+
branches
|
|
145
|
+
}));
|
|
146
|
+
}
|
|
147
|
+
export default getConfigSchemaErrors;
|
package/dist/index.js
CHANGED
|
@@ -14,4 +14,6 @@
|
|
|
14
14
|
limitations under the License.
|
|
15
15
|
*/ import validate from './validate.js';
|
|
16
16
|
import compile from './compile.js';
|
|
17
|
-
|
|
17
|
+
import formatSchemaError from './formatSchemaError.js';
|
|
18
|
+
import getConfigSchemaErrors from './getConfigSchemaErrors.js';
|
|
19
|
+
export { validate, compile, formatSchemaError, getConfigSchemaErrors };
|
package/dist/validate.js
CHANGED
|
@@ -12,18 +12,36 @@
|
|
|
12
12
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
13
|
See the License for the specific language governing permissions and
|
|
14
14
|
limitations under the License.
|
|
15
|
-
*/ import
|
|
15
|
+
*/ import compile from './compile.js';
|
|
16
16
|
import createErrorMessage from './createErrorMessage.js';
|
|
17
|
+
// A schema object that stays the same (a plugin schema, a cached artifact)
|
|
18
|
+
// compiles once; a fresh object compiles again and is released with it.
|
|
19
|
+
const validators = new WeakMap();
|
|
20
|
+
function getValidator(schema) {
|
|
21
|
+
if (typeof schema !== 'object') {
|
|
22
|
+
return compile({
|
|
23
|
+
schema
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
let validator = validators.get(schema);
|
|
27
|
+
if (!validator) {
|
|
28
|
+
validator = compile({
|
|
29
|
+
schema
|
|
30
|
+
});
|
|
31
|
+
validators.set(schema, validator);
|
|
32
|
+
}
|
|
33
|
+
return validator;
|
|
34
|
+
}
|
|
17
35
|
function validate({ schema, data, returnErrors = false }) {
|
|
18
|
-
const valid =
|
|
36
|
+
const { valid, errors } = getValidator(schema)(data);
|
|
19
37
|
if (!valid) {
|
|
20
38
|
if (returnErrors) {
|
|
21
39
|
return {
|
|
22
40
|
valid: false,
|
|
23
|
-
errors
|
|
41
|
+
errors
|
|
24
42
|
};
|
|
25
43
|
}
|
|
26
|
-
throw new Error(createErrorMessage(
|
|
44
|
+
throw new Error(createErrorMessage(errors));
|
|
27
45
|
}
|
|
28
46
|
return {
|
|
29
47
|
valid: true
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lowdefy/ajv",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "7.0.0",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"description": "",
|
|
6
6
|
"homepage": "https://lowdefy.com",
|
|
@@ -33,7 +33,8 @@
|
|
|
33
33
|
"dist/*"
|
|
34
34
|
],
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@lowdefy/
|
|
36
|
+
"@lowdefy/helpers": "7.0.0",
|
|
37
|
+
"@lowdefy/nunjucks": "7.0.0",
|
|
37
38
|
"ajv": "8.20.0",
|
|
38
39
|
"ajv-errors": "3.0.0",
|
|
39
40
|
"ajv-formats": "2.1.1",
|