@crewhaus/tool-schema 0.7.0 → 0.8.1
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/LICENSE +201 -0
- package/NOTICE +9 -0
- package/README.md +50 -1
- package/dist/index.d.ts +9 -1
- package/dist/index.js +92 -13
- package/dist/lib/assert.d.ts +36 -1
- package/dist/lib/assert.js +95 -14
- package/dist/lib/compare.js +30 -9
- package/dist/lib/formats.d.ts +13 -0
- package/dist/lib/formats.js +14 -1
- package/dist/lib/jsonschema.d.ts +170 -0
- package/dist/lib/jsonschema.js +530 -68
- package/dist/lib/records.d.ts +16 -0
- package/dist/lib/records.js +17 -3
- package/dist/lib/regex-answers.d.ts +142 -0
- package/dist/lib/regex-answers.js +289 -0
- package/dist/lib/schemadiff.d.ts +19 -0
- package/dist/lib/schemadiff.js +98 -17
- package/dist/lib/shape-index.d.ts +22 -0
- package/dist/lib/shape-index.js +157 -0
- package/dist/lib/summarize.js +28 -9
- package/dist/lib/value.d.ts +16 -2
- package/dist/lib/value.js +143 -13
- package/package.json +11 -6
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
CrewHaus Factory
|
|
2
|
+
Copyright 2026 Max Meier
|
|
3
|
+
|
|
4
|
+
This product includes software developed by Max Meier
|
|
5
|
+
(operating as StudioMax — https://studiomax.io) and contributors.
|
|
6
|
+
|
|
7
|
+
The CrewHaus name and logo are trademarks of Max Meier. See
|
|
8
|
+
https://github.com/crewhaus/factory/blob/main/TRADEMARK.md for the
|
|
9
|
+
use-of-marks policy.
|
package/README.md
CHANGED
|
@@ -12,7 +12,7 @@ answer twice — and say exactly which field, at which path, was wrong.
|
|
|
12
12
|
```yaml
|
|
13
13
|
tools:
|
|
14
14
|
- all-schema # every tool below
|
|
15
|
-
- -
|
|
15
|
+
- -jsonSchemaInfer # ...except this one
|
|
16
16
|
```
|
|
17
17
|
|
|
18
18
|
| Tool | What it does |
|
|
@@ -64,6 +64,55 @@ sitting in a branch this particular value never reached is not in it. A
|
|
|
64
64
|
|
|
65
65
|
`format` follows Draft-07 and is an annotation until you pass `assertFormat`.
|
|
66
66
|
|
|
67
|
+
A recursive schema may recurse through any keyword, `anyOf`, `oneOf`, `not`,
|
|
68
|
+
`if` and `contains` included; only the same `$ref` reached twice at the same
|
|
69
|
+
place in the value is reported as a cycle. Depth is counted where the value
|
|
70
|
+
nests, so a recursive schema follows a value 512 levels deep whatever
|
|
71
|
+
keywords it recurses through; a deeper value, or a schema nested so deep the
|
|
72
|
+
walk would overflow the stack, is `undetermined` rather than a verdict.
|
|
73
|
+
|
|
74
|
+
**The work is bounded.** `anyOf`, `oneOf` and `allOf` over `$ref`s that share
|
|
75
|
+
a target multiply, so a small schema can ask for billions of evaluations.
|
|
76
|
+
Each call gets a budget of subschema evaluations, sized to the value and the
|
|
77
|
+
schema: each node of the value (a long string or key counting as a node per
|
|
78
|
+
256 characters) is allowed as many evaluations as the schema has subschemas
|
|
79
|
+
written out, at least 64 and at most 1,024, and a call gets at least 500,000
|
|
80
|
+
and at most 20 million (a few seconds). A schema written as a tree checks each
|
|
81
|
+
of its subschemas at most once per value node, so a wide or nested union over
|
|
82
|
+
thousands of valid rows is answered; only a `$ref` reached again at the same
|
|
83
|
+
place goes past it. A check whose cost follows the value's size — a string's
|
|
84
|
+
length, pattern or format, `uniqueItems`, listing an object's keys, each
|
|
85
|
+
`enum` candidate (an enum is indexed once, so a value is one lookup), each
|
|
86
|
+
key tested against each `patternProperties` pattern (compiled once per
|
|
87
|
+
schema), a `const` comparison — is charged for that size, and an error
|
|
88
|
+
message is built only when it is kept, from a preview that reads just the
|
|
89
|
+
part it shows. When a schema needs more, `JsonSchemaValidate` answers
|
|
90
|
+
`valid: null` with `undetermined: true` and the reason, never a verdict
|
|
91
|
+
either way; `ValidateRecords` shares one budget across its rows and counts
|
|
92
|
+
the rows it could not decide as `undetermined`, neither passed nor failed,
|
|
93
|
+
with `ok: false`. A failing union's message is at most 1,000 characters:
|
|
94
|
+
when its branches' reasons do not fit, each is cut to an equal share and a
|
|
95
|
+
short one keeps all of it, so a nested union's decisive reason survives.
|
|
96
|
+
|
|
97
|
+
**Patterns never run on the caller's thread.** A schema's `pattern` and
|
|
98
|
+
`patternProperties`, and `Assert`'s `matches`/`notMatches`, are regexes the
|
|
99
|
+
caller wrote, run over text the caller chose; a synchronous `RegExp` cannot be
|
|
100
|
+
interrupted, and when JavaScriptCore gives up on a backtracking match it says
|
|
101
|
+
"no match". So each tool asks every pattern question first, answers them all
|
|
102
|
+
in one `@crewhaus/tool-safety` regex worker under a five-second deadline, and
|
|
103
|
+
then evaluates with the answers (a schema whose later patterns depend on
|
|
104
|
+
earlier answers, through `if` or a union, takes a few rounds). A pattern the
|
|
105
|
+
screen refuses, because it backtracks exponentially (`(a+)+`, `(\w+\s?)*`) or
|
|
106
|
+
is past 10,000 characters, makes the schema malformed (`schemaValid: false`),
|
|
107
|
+
and fails an `Assert` check as an invalid regex does. One that cannot be run
|
|
108
|
+
to an answer (the deadline, the engine giving up, which includes a no-match
|
|
109
|
+
slower than 100 ms) leaves the value `undetermined`, never invalid or valid,
|
|
110
|
+
and an `Assert` check `undetermined` and not ok. `Branch`, `DecisionTable`,
|
|
111
|
+
`RuleScore`, `LeadAssign`, `SequenceRun` and `ErrorClassify` in
|
|
112
|
+
`@crewhaus/tool-flow` read the same answers. A library caller of `runChecks` or `validateValue` with no
|
|
113
|
+
answers gets a bounded fallback on its own thread: the same screen, at most
|
|
114
|
+
64 KiB of input per pattern, and a give-up read as undetermined.
|
|
115
|
+
|
|
67
116
|
## The formats
|
|
68
117
|
|
|
69
118
|
Each format is a stated subset, not a full grammar, and each rejects the grey
|
package/dist/index.d.ts
CHANGED
|
@@ -11,6 +11,8 @@ export declare const validateEnum: RegisteredTool;
|
|
|
11
11
|
export declare const validateFormat: RegisteredTool;
|
|
12
12
|
export declare const validateUniqueKeys: RegisteredTool;
|
|
13
13
|
export declare const validateReferences: RegisteredTool;
|
|
14
|
+
/** The most characters the paths of the changes SchemaDiff lists may add up to. */
|
|
15
|
+
export declare const MAX_LISTED_PATH_CHARS = 1000000;
|
|
14
16
|
export declare const schemaDiff: RegisteredTool;
|
|
15
17
|
export declare const schemaSummarize: RegisteredTool;
|
|
16
18
|
/** Every tool this package registers, in the order a catalog should list them. */
|
|
@@ -24,4 +26,10 @@ export declare const SCHEMA_TOOLS: ReadonlyArray<RegisteredTool>;
|
|
|
24
26
|
* find it missing, or subtly different, in the other. There is one
|
|
25
27
|
* vocabulary and one evaluator, and this is where it lives.
|
|
26
28
|
*/
|
|
27
|
-
export { ASSERT_OPS, type AssertOp, type AssertReport, type Check, type CheckResult, checkRequired, runChecks, } from "./lib/assert";
|
|
29
|
+
export { ASSERT_OPS, type AssertOp, type AssertReport, type Check, type CheckResult, askCheckPatterns, checkRequired, checksVerdict, runChecks, } from "./lib/assert";
|
|
30
|
+
/**
|
|
31
|
+
* Caller patterns answered in the worker, for the other packages whose
|
|
32
|
+
* synchronous evaluators run a regex someone else wrote: ask, resolve once,
|
|
33
|
+
* then read (see `./lib/regex-answers`).
|
|
34
|
+
*/
|
|
35
|
+
export { _setRegexAnswersSessionForTest, CALLER_PATTERN_DEADLINE_MS, MAX_PATTERN_QUESTIONS, type RegexAnswer, RegexAnswers, type RegexRunContext, regexRunContext, testPatternSync, withRegexAnswers, } from "./lib/regex-answers";
|
package/dist/index.js
CHANGED
|
@@ -18,12 +18,13 @@
|
|
|
18
18
|
*/
|
|
19
19
|
import { buildTool } from "@crewhaus/tool-builder";
|
|
20
20
|
import { z } from "zod";
|
|
21
|
-
import { ASSERT_OPS, checkRequired, runChecks } from "./lib/assert";
|
|
21
|
+
import { ASSERT_OPS, askCheckPatterns, checkRequired, runChecks } from "./lib/assert";
|
|
22
22
|
import { compareValues, firstDifference } from "./lib/compare";
|
|
23
23
|
import { FORMAT_NAMES, checkFormat, matchingFormats } from "./lib/formats";
|
|
24
24
|
import { inferSchema } from "./lib/infer";
|
|
25
25
|
import { checkSchemaShape, validateValue } from "./lib/jsonschema";
|
|
26
26
|
import { checkReferences, collectFieldValues, findDuplicates, validateRecords as validateRecordsFn, } from "./lib/records";
|
|
27
|
+
import { RegexAnswers, regexRunContext, withRegexAnswers } from "./lib/regex-answers";
|
|
27
28
|
import { diffSchemas } from "./lib/schemadiff";
|
|
28
29
|
import { closestMatch } from "./lib/suggest";
|
|
29
30
|
import { renderFieldTable, summarizeSchema } from "./lib/summarize";
|
|
@@ -83,14 +84,43 @@ export const jsonSchemaValidate = buildTool({
|
|
|
83
84
|
.refine(present("value"), { message: "value is required, even when it is null" }),
|
|
84
85
|
readOnly: true,
|
|
85
86
|
concurrencySafe: true,
|
|
86
|
-
execute: async (input) => {
|
|
87
|
+
execute: async (input, ctx) => {
|
|
87
88
|
const problems = schemaProblemReport(input.schema);
|
|
88
89
|
if (problems !== null)
|
|
89
90
|
return problems;
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
91
|
+
// The schema's patterns run in the worker, never on this thread; the
|
|
92
|
+
// walk runs once to learn what they are asked, and again with answers.
|
|
93
|
+
const run = await withRegexAnswers((regex) => {
|
|
94
|
+
const pass = validateValue(input.value, input.schema, {
|
|
95
|
+
assertFormat: input.assertFormat ?? false,
|
|
96
|
+
maxErrors: input.maxErrors ?? 100,
|
|
97
|
+
regex,
|
|
98
|
+
});
|
|
99
|
+
if (pass.workExhausted)
|
|
100
|
+
regex.discardPending();
|
|
101
|
+
return pass;
|
|
102
|
+
}, regexRunContext(ctx));
|
|
103
|
+
if ("undetermined" in run) {
|
|
104
|
+
return json({
|
|
105
|
+
valid: null,
|
|
106
|
+
undetermined: true,
|
|
107
|
+
reason: run.undetermined,
|
|
108
|
+
errorsBeforeStopping: [],
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
const result = run.value;
|
|
112
|
+
if (result.undetermined !== null) {
|
|
113
|
+
// Not a verdict either way: `valid` is null, never false or true.
|
|
114
|
+
return json({
|
|
115
|
+
valid: null,
|
|
116
|
+
undetermined: true,
|
|
117
|
+
reason: result.undetermined,
|
|
118
|
+
errorsBeforeStopping: result.errors,
|
|
119
|
+
...(result.unsupportedKeywords.length > 0
|
|
120
|
+
? { unsupportedKeywords: result.unsupportedKeywords }
|
|
121
|
+
: {}),
|
|
122
|
+
});
|
|
123
|
+
}
|
|
94
124
|
return json({
|
|
95
125
|
valid: result.valid,
|
|
96
126
|
errorCount: result.errors.length,
|
|
@@ -161,21 +191,34 @@ export const validateRecords = buildTool({
|
|
|
161
191
|
}),
|
|
162
192
|
readOnly: true,
|
|
163
193
|
concurrencySafe: true,
|
|
164
|
-
execute: async (input) => {
|
|
194
|
+
execute: async (input, ctx) => {
|
|
165
195
|
const problems = schemaProblemReport(input.schema);
|
|
166
196
|
if (problems !== null)
|
|
167
197
|
return problems;
|
|
168
|
-
const
|
|
198
|
+
const run = await withRegexAnswers((regex) => validateRecordsFn(input.records, input.schema, {
|
|
169
199
|
assertFormat: input.assertFormat ?? false,
|
|
170
200
|
maxErrorsPerRow: input.maxErrorsPerRow ?? 10,
|
|
171
201
|
maxFailedRows: input.maxFailedRows ?? 50,
|
|
172
202
|
idField: input.idField ?? null,
|
|
173
|
-
|
|
203
|
+
regex,
|
|
204
|
+
}), regexRunContext(ctx));
|
|
205
|
+
if ("undetermined" in run) {
|
|
206
|
+
return json({
|
|
207
|
+
ok: false,
|
|
208
|
+
total: input.records.length,
|
|
209
|
+
undetermined: input.records.length,
|
|
210
|
+
reason: run.undetermined,
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
const report = run.value;
|
|
174
214
|
const head = {
|
|
175
215
|
ok: report.ok,
|
|
176
216
|
total: report.total,
|
|
177
217
|
passed: report.passed,
|
|
178
218
|
failed: report.failed,
|
|
219
|
+
...(report.undetermined > 0
|
|
220
|
+
? { undetermined: report.undetermined, undeterminedFrom: report.undeterminedFrom }
|
|
221
|
+
: {}),
|
|
179
222
|
topIssues: report.topIssues.slice(0, 20),
|
|
180
223
|
...(report.unsupportedKeywords.length > 0
|
|
181
224
|
? { unsupportedKeywords: report.unsupportedKeywords }
|
|
@@ -213,13 +256,20 @@ export const assert = buildTool({
|
|
|
213
256
|
.refine(present("value"), { message: "value is required, even when it is null" }),
|
|
214
257
|
readOnly: true,
|
|
215
258
|
concurrencySafe: true,
|
|
216
|
-
execute: async (input) => {
|
|
217
|
-
const
|
|
259
|
+
execute: async (input, ctx) => {
|
|
260
|
+
const checks = input.checks;
|
|
261
|
+
// `matches` patterns run in the worker, before the checks are read.
|
|
262
|
+
const regex = new RegexAnswers();
|
|
263
|
+
askCheckPatterns(input.value, checks, regex);
|
|
264
|
+
await regex.resolve(regexRunContext(ctx));
|
|
265
|
+
const report = runChecks(input.value, checks, { regex });
|
|
266
|
+
const undetermined = report.undetermined > 0 ? { undetermined: report.undetermined } : {};
|
|
218
267
|
if (input.failuresOnly === true) {
|
|
219
268
|
return json({
|
|
220
269
|
ok: report.ok,
|
|
221
270
|
passed: report.passed,
|
|
222
271
|
failed: report.failed,
|
|
272
|
+
...undetermined,
|
|
223
273
|
failures: report.failures,
|
|
224
274
|
});
|
|
225
275
|
}
|
|
@@ -227,6 +277,7 @@ export const assert = buildTool({
|
|
|
227
277
|
ok: report.ok,
|
|
228
278
|
passed: report.passed,
|
|
229
279
|
failed: report.failed,
|
|
280
|
+
...undetermined,
|
|
230
281
|
results: report.results,
|
|
231
282
|
});
|
|
232
283
|
},
|
|
@@ -586,6 +637,8 @@ export const validateReferences = buildTool({
|
|
|
586
637
|
});
|
|
587
638
|
},
|
|
588
639
|
});
|
|
640
|
+
/** The most characters the paths of the changes SchemaDiff lists may add up to. */
|
|
641
|
+
export const MAX_LISTED_PATH_CHARS = 1_000_000;
|
|
589
642
|
export const schemaDiff = buildTool({
|
|
590
643
|
name: "SchemaDiff",
|
|
591
644
|
description: "Diff two JSON Schemas and classify every change as compatible, breaking or undecidable for a reader of the data. Use before shipping a contract change, to answer whether existing payloads still validate — an undecidable change is reported as such, never rounded to safe.",
|
|
@@ -604,12 +657,32 @@ export const schemaDiff = buildTool({
|
|
|
604
657
|
const changes = input.breakingOnly === true
|
|
605
658
|
? result.changes.filter((c) => c.compat !== "compatible")
|
|
606
659
|
: result.changes;
|
|
660
|
+
// Every change carries its full path, so one long name high in a schema
|
|
661
|
+
// is repeated in every change below it: a result the square of the
|
|
662
|
+
// input. The changes are listed until their paths reach the cap, and
|
|
663
|
+
// the rest are counted; the verdict and the counts cover them all.
|
|
664
|
+
const listed = [];
|
|
665
|
+
let pathChars = 0;
|
|
666
|
+
for (const change of changes) {
|
|
667
|
+
pathChars += change.path.length;
|
|
668
|
+
if (pathChars > MAX_LISTED_PATH_CHARS)
|
|
669
|
+
break;
|
|
670
|
+
listed.push(change);
|
|
671
|
+
}
|
|
672
|
+
const omitted = changes.length - listed.length;
|
|
607
673
|
return json({
|
|
608
674
|
identical: result.identical,
|
|
609
675
|
verdict: result.verdict,
|
|
610
676
|
backwardCompatible: result.backwardCompatible,
|
|
611
677
|
counts: result.counts,
|
|
612
|
-
changes,
|
|
678
|
+
changes: listed,
|
|
679
|
+
...(omitted > 0
|
|
680
|
+
? {
|
|
681
|
+
truncated: true,
|
|
682
|
+
omitted,
|
|
683
|
+
detail: `${omitted} of the ${changes.length} changes are not listed: their paths would pass ${MAX_LISTED_PATH_CHARS} characters. The verdict and counts cover every change; diff a nested part on its own to list the rest`,
|
|
684
|
+
}
|
|
685
|
+
: {}),
|
|
613
686
|
note: "backward compatible means every value valid under `before` is still valid under `after`",
|
|
614
687
|
});
|
|
615
688
|
},
|
|
@@ -675,4 +748,10 @@ export const SCHEMA_TOOLS = Object.freeze([
|
|
|
675
748
|
* find it missing, or subtly different, in the other. There is one
|
|
676
749
|
* vocabulary and one evaluator, and this is where it lives.
|
|
677
750
|
*/
|
|
678
|
-
export { ASSERT_OPS, checkRequired, runChecks, } from "./lib/assert";
|
|
751
|
+
export { ASSERT_OPS, askCheckPatterns, checkRequired, checksVerdict, runChecks, } from "./lib/assert";
|
|
752
|
+
/**
|
|
753
|
+
* Caller patterns answered in the worker, for the other packages whose
|
|
754
|
+
* synchronous evaluators run a regex someone else wrote: ask, resolve once,
|
|
755
|
+
* then read (see `./lib/regex-answers`).
|
|
756
|
+
*/
|
|
757
|
+
export { _setRegexAnswersSessionForTest, CALLER_PATTERN_DEADLINE_MS, MAX_PATTERN_QUESTIONS, RegexAnswers, regexRunContext, testPatternSync, withRegexAnswers, } from "./lib/regex-answers";
|
package/dist/lib/assert.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type RegexAnswers } from "./regex-answers";
|
|
1
2
|
/** Every comparison {@link runChecks} understands. */
|
|
2
3
|
export declare const ASSERT_OPS: readonly ["equals", "notEquals", "contains", "notContains", "matches", "notMatches", "startsWith", "endsWith", "greaterThan", "greaterThanOrEqual", "lessThan", "lessThanOrEqual", "exists", "notExists", "isEmpty", "isNotEmpty", "isType", "hasLength", "minLength", "maxLength", "oneOf", "notOneOf", "hasFormat"];
|
|
3
4
|
export type AssertOp = (typeof ASSERT_OPS)[number];
|
|
@@ -22,17 +23,34 @@ export type CheckResult = {
|
|
|
22
23
|
expected: string | null;
|
|
23
24
|
/** Empty when the check passed. */
|
|
24
25
|
reason: string;
|
|
26
|
+
/**
|
|
27
|
+
* Set when the check could not be evaluated: its pattern could not be run
|
|
28
|
+
* to an answer. It is not ok (a gate fails closed), but it did not fail
|
|
29
|
+
* either, and `reason` says why. Absent on every other check.
|
|
30
|
+
*/
|
|
31
|
+
undetermined?: true;
|
|
25
32
|
};
|
|
26
33
|
export type AssertReport = {
|
|
27
34
|
ok: boolean;
|
|
28
35
|
passed: number;
|
|
36
|
+
/** Checks that did not pass, the undetermined ones included. */
|
|
29
37
|
failed: number;
|
|
38
|
+
/** Of `failed`, the checks that could not be evaluated. */
|
|
39
|
+
undetermined: number;
|
|
30
40
|
results: CheckResult[];
|
|
31
41
|
/** Just the failures, for a caller that only wants to print those. */
|
|
32
42
|
failures: CheckResult[];
|
|
33
43
|
};
|
|
34
44
|
/** Empty means: absent, null, the empty string, an empty array or an empty object. */
|
|
35
45
|
export declare function isEmptyValue(value: unknown): boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Ask `answers` about every pattern `checks` will test against `value`, so
|
|
48
|
+
* one {@link RegexAnswers.resolve} settles them all before
|
|
49
|
+
* {@link runChecks} reads them. A caller evaluating several check lists
|
|
50
|
+
* against one value (a table's rows, a router's arms) asks for every list
|
|
51
|
+
* first, then resolves once.
|
|
52
|
+
*/
|
|
53
|
+
export declare function askCheckPatterns(value: unknown, checks: ReadonlyArray<Check>, answers: RegexAnswers): void;
|
|
36
54
|
/**
|
|
37
55
|
* Run every check against `value` and report which held.
|
|
38
56
|
*
|
|
@@ -44,8 +62,25 @@ export declare function isEmptyValue(value: unknown): boolean;
|
|
|
44
62
|
* A malformed check (a bad regex, a non-numeric bound) fails that check with
|
|
45
63
|
* an explanatory reason rather than throwing, so one typo cannot take down a
|
|
46
64
|
* whole gate.
|
|
65
|
+
*
|
|
66
|
+
* `matches` and `notMatches` read their answers from `options.regex`, which
|
|
67
|
+
* the calling tool resolved in the worker (see {@link askCheckPatterns});
|
|
68
|
+
* without it they fall back to a bounded run on this thread. A check whose
|
|
69
|
+
* pattern could not be run to an answer is not ok and carries
|
|
70
|
+
* `undetermined: true`; its reason says so whatever `message` the check set.
|
|
71
|
+
*/
|
|
72
|
+
export declare function runChecks(value: unknown, checks: ReadonlyArray<Check>, options?: {
|
|
73
|
+
readonly regex?: RegexAnswers;
|
|
74
|
+
}): AssertReport;
|
|
75
|
+
/**
|
|
76
|
+
* What a check list says as a whole, three ways. Under `all`, a check that
|
|
77
|
+
* definitely failed decides it (`fail`); otherwise an undetermined one leaves
|
|
78
|
+
* it `undetermined`. Under `any`, a check that passed decides it (`pass`);
|
|
79
|
+
* otherwise an undetermined one leaves it `undetermined`. A router or a
|
|
80
|
+
* scorer that reads `report.ok` alone would take an undetermined check for a
|
|
81
|
+
* miss and fall through to the next arm or the default.
|
|
47
82
|
*/
|
|
48
|
-
export declare function
|
|
83
|
+
export declare function checksVerdict(report: AssertReport, mode?: "all" | "any"): "pass" | "fail" | "undetermined";
|
|
49
84
|
/**
|
|
50
85
|
* Which of `paths` are present and non-empty in `value`, and which are not.
|
|
51
86
|
* "Present" means the path resolves; "non-empty" applies
|