@zukhruf/evals 0.0.0-stage → 0.8.3
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 +21 -0
- package/NOTICE +82 -0
- package/README.md +528 -2
- package/dist/aggregation/counts.d.ts +11 -0
- package/dist/aggregation/counts.d.ts.map +1 -0
- package/dist/aggregation/counts.js +14 -0
- package/dist/aggregation/counts.js.map +1 -0
- package/dist/aggregation/f1-ratio.d.ts +8 -0
- package/dist/aggregation/f1-ratio.d.ts.map +1 -0
- package/dist/aggregation/f1-ratio.js +24 -0
- package/dist/aggregation/f1-ratio.js.map +1 -0
- package/dist/aggregation/ratio-of-sums.d.ts +8 -0
- package/dist/aggregation/ratio-of-sums.d.ts.map +1 -0
- package/dist/aggregation/ratio-of-sums.js +21 -0
- package/dist/aggregation/ratio-of-sums.js.map +1 -0
- package/dist/aggregation/ratio.d.ts +8 -0
- package/dist/aggregation/ratio.d.ts.map +1 -0
- package/dist/aggregation/ratio.js +20 -0
- package/dist/aggregation/ratio.js.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -0
- package/dist/promptfoo/assertion-module.d.ts +23 -0
- package/dist/promptfoo/assertion-module.d.ts.map +1 -0
- package/dist/promptfoo/assertion-module.js +45 -0
- package/dist/promptfoo/assertion-module.js.map +1 -0
- package/dist/promptfoo/index.d.ts +3 -0
- package/dist/promptfoo/index.d.ts.map +1 -0
- package/dist/promptfoo/index.js +3 -0
- package/dist/promptfoo/index.js.map +1 -0
- package/dist/promptfoo/promptfoo-assertion.d.ts +18 -0
- package/dist/promptfoo/promptfoo-assertion.d.ts.map +1 -0
- package/dist/promptfoo/promptfoo-assertion.js +31 -0
- package/dist/promptfoo/promptfoo-assertion.js.map +1 -0
- package/dist/scorers/at-least.d.ts +9 -0
- package/dist/scorers/at-least.d.ts.map +1 -0
- package/dist/scorers/at-least.js +18 -0
- package/dist/scorers/at-least.js.map +1 -0
- package/dist/scorers/f1.d.ts +9 -0
- package/dist/scorers/f1.d.ts.map +1 -0
- package/dist/scorers/f1.js +12 -0
- package/dist/scorers/f1.js.map +1 -0
- package/dist/scorers/gate.d.ts +7 -0
- package/dist/scorers/gate.d.ts.map +1 -0
- package/dist/scorers/gate.js +10 -0
- package/dist/scorers/gate.js.map +1 -0
- package/dist/scorers/matches.d.ts +5 -0
- package/dist/scorers/matches.d.ts.map +1 -0
- package/dist/scorers/matches.js +2 -0
- package/dist/scorers/matches.js.map +1 -0
- package/dist/scorers/mean-of.d.ts +8 -0
- package/dist/scorers/mean-of.d.ts.map +1 -0
- package/dist/scorers/mean-of.js +15 -0
- package/dist/scorers/mean-of.js.map +1 -0
- package/dist/scorers/penalized.d.ts +9 -0
- package/dist/scorers/penalized.d.ts.map +1 -0
- package/dist/scorers/penalized.js +28 -0
- package/dist/scorers/penalized.js.map +1 -0
- package/dist/scorers/penalty.d.ts +7 -0
- package/dist/scorers/penalty.d.ts.map +1 -0
- package/dist/scorers/penalty.js +11 -0
- package/dist/scorers/penalty.js.map +1 -0
- package/dist/scorers/precision.d.ts +9 -0
- package/dist/scorers/precision.d.ts.map +1 -0
- package/dist/scorers/precision.js +13 -0
- package/dist/scorers/precision.js.map +1 -0
- package/dist/scorers/recall.d.ts +9 -0
- package/dist/scorers/recall.d.ts.map +1 -0
- package/dist/scorers/recall.js +13 -0
- package/dist/scorers/recall.js.map +1 -0
- package/dist/scorers/sample-averaged.d.ts +10 -0
- package/dist/scorers/sample-averaged.d.ts.map +1 -0
- package/dist/scorers/sample-averaged.js +17 -0
- package/dist/scorers/sample-averaged.js.map +1 -0
- package/dist/scorers/scorer.d.ts +5 -0
- package/dist/scorers/scorer.d.ts.map +1 -0
- package/dist/scorers/scorer.js +2 -0
- package/dist/scorers/scorer.js.map +1 -0
- package/dist/scorers/shortfall-penalty.d.ts +11 -0
- package/dist/scorers/shortfall-penalty.d.ts.map +1 -0
- package/dist/scorers/shortfall-penalty.js +25 -0
- package/dist/scorers/shortfall-penalty.js.map +1 -0
- package/dist/scoring/bootstrap.d.ts +16 -0
- package/dist/scoring/bootstrap.d.ts.map +1 -0
- package/dist/scoring/bootstrap.js +31 -0
- package/dist/scoring/bootstrap.js.map +1 -0
- package/dist/scoring/scorecard-scorer.d.ts +11 -0
- package/dist/scoring/scorecard-scorer.d.ts.map +1 -0
- package/dist/scoring/scorecard-scorer.js +2 -0
- package/dist/scoring/scorecard-scorer.js.map +1 -0
- package/dist/scoring/scorecard.d.ts +15 -0
- package/dist/scoring/scorecard.d.ts.map +1 -0
- package/dist/scoring/scorecard.js +2 -0
- package/dist/scoring/scorecard.js.map +1 -0
- package/dist/scoring/totals.d.ts +9 -0
- package/dist/scoring/totals.d.ts.map +1 -0
- package/dist/scoring/totals.js +23 -0
- package/dist/scoring/totals.js.map +1 -0
- package/dist/structured/comparison/alignment.d.ts +9 -0
- package/dist/structured/comparison/alignment.d.ts.map +1 -0
- package/dist/structured/comparison/alignment.js +2 -0
- package/dist/structured/comparison/alignment.js.map +1 -0
- package/dist/structured/comparison/alignments/by-best-match.d.ts +9 -0
- package/dist/structured/comparison/alignments/by-best-match.d.ts.map +1 -0
- package/dist/structured/comparison/alignments/by-best-match.js +25 -0
- package/dist/structured/comparison/alignments/by-best-match.js.map +1 -0
- package/dist/structured/comparison/alignments/by-position.d.ts +6 -0
- package/dist/structured/comparison/alignments/by-position.d.ts.map +1 -0
- package/dist/structured/comparison/alignments/by-position.js +9 -0
- package/dist/structured/comparison/alignments/by-position.js.map +1 -0
- package/dist/structured/comparison/bag-of-words.d.ts +10 -0
- package/dist/structured/comparison/bag-of-words.d.ts.map +1 -0
- package/dist/structured/comparison/bag-of-words.js +48 -0
- package/dist/structured/comparison/bag-of-words.js.map +1 -0
- package/dist/structured/comparison/comparator.d.ts +5 -0
- package/dist/structured/comparison/comparator.d.ts.map +1 -0
- package/dist/structured/comparison/comparator.js +2 -0
- package/dist/structured/comparison/comparator.js.map +1 -0
- package/dist/structured/comparison/comparators/json-equality.d.ts +7 -0
- package/dist/structured/comparison/comparators/json-equality.d.ts.map +1 -0
- package/dist/structured/comparison/comparators/json-equality.js +18 -0
- package/dist/structured/comparison/comparators/json-equality.js.map +1 -0
- package/dist/structured/comparison/comparators/normalized-text.d.ts +7 -0
- package/dist/structured/comparison/comparators/normalized-text.d.ts.map +1 -0
- package/dist/structured/comparison/comparators/normalized-text.js +13 -0
- package/dist/structured/comparison/comparators/normalized-text.js.map +1 -0
- package/dist/structured/comparison/comparators/normalized.d.ts +2 -0
- package/dist/structured/comparison/comparators/normalized.d.ts.map +1 -0
- package/dist/structured/comparison/comparators/normalized.js +3 -0
- package/dist/structured/comparison/comparators/normalized.js.map +1 -0
- package/dist/structured/comparison/comparators/number-within.d.ts +9 -0
- package/dist/structured/comparison/comparators/number-within.d.ts.map +1 -0
- package/dist/structured/comparison/comparators/number-within.js +18 -0
- package/dist/structured/comparison/comparators/number-within.js.map +1 -0
- package/dist/structured/comparison/comparators/python-equality.d.ts +7 -0
- package/dist/structured/comparison/comparators/python-equality.d.ts.map +1 -0
- package/dist/structured/comparison/comparators/python-equality.js +15 -0
- package/dist/structured/comparison/comparators/python-equality.js.map +1 -0
- package/dist/structured/comparison/comparators/token-overlap-at-least.d.ts +9 -0
- package/dist/structured/comparison/comparators/token-overlap-at-least.d.ts.map +1 -0
- package/dist/structured/comparison/comparators/token-overlap-at-least.js +20 -0
- package/dist/structured/comparison/comparators/token-overlap-at-least.js.map +1 -0
- package/dist/structured/comparison/comparators/typed-comparator.d.ts +8 -0
- package/dist/structured/comparison/comparators/typed-comparator.d.ts.map +1 -0
- package/dist/structured/comparison/comparators/typed-comparator.js +10 -0
- package/dist/structured/comparison/comparators/typed-comparator.js.map +1 -0
- package/dist/structured/comparison/comparison-spec.d.ts +17 -0
- package/dist/structured/comparison/comparison-spec.d.ts.map +1 -0
- package/dist/structured/comparison/comparison-spec.js +36 -0
- package/dist/structured/comparison/comparison-spec.js.map +1 -0
- package/dist/structured/comparison/comparison.d.ts +25 -0
- package/dist/structured/comparison/comparison.d.ts.map +1 -0
- package/dist/structured/comparison/comparison.js +94 -0
- package/dist/structured/comparison/comparison.js.map +1 -0
- package/dist/structured/comparison/field.d.ts +10 -0
- package/dist/structured/comparison/field.d.ts.map +1 -0
- package/dist/structured/comparison/field.js +29 -0
- package/dist/structured/comparison/field.js.map +1 -0
- package/dist/structured/comparison/item-pairs.d.ts +8 -0
- package/dist/structured/comparison/item-pairs.d.ts.map +1 -0
- package/dist/structured/comparison/item-pairs.js +2 -0
- package/dist/structured/comparison/item-pairs.js.map +1 -0
- package/dist/structured/comparison/json-path-query.d.ts +7 -0
- package/dist/structured/comparison/json-path-query.d.ts.map +1 -0
- package/dist/structured/comparison/json-path-query.js +133 -0
- package/dist/structured/comparison/json-path-query.js.map +1 -0
- package/dist/structured/comparison/json-value.d.ts +10 -0
- package/dist/structured/comparison/json-value.d.ts.map +1 -0
- package/dist/structured/comparison/json-value.js +47 -0
- package/dist/structured/comparison/json-value.js.map +1 -0
- package/dist/structured/comparison/leaf-pair.d.ts +24 -0
- package/dist/structured/comparison/leaf-pair.d.ts.map +1 -0
- package/dist/structured/comparison/leaf-pair.js +2 -0
- package/dist/structured/comparison/leaf-pair.js.map +1 -0
- package/dist/structured/comparison/match.d.ts +5 -0
- package/dist/structured/comparison/match.d.ts.map +1 -0
- package/dist/structured/comparison/match.js +2 -0
- package/dist/structured/comparison/match.js.map +1 -0
- package/dist/structured/comparison/name-selector.d.ts +6 -0
- package/dist/structured/comparison/name-selector.d.ts.map +1 -0
- package/dist/structured/comparison/name-selector.js +41 -0
- package/dist/structured/comparison/name-selector.js.map +1 -0
- package/dist/structured/comparison/path.d.ts +10 -0
- package/dist/structured/comparison/path.d.ts.map +1 -0
- package/dist/structured/comparison/path.js +27 -0
- package/dist/structured/comparison/path.js.map +1 -0
- package/dist/structured/comparison/tree-walk.d.ts +10 -0
- package/dist/structured/comparison/tree-walk.d.ts.map +1 -0
- package/dist/structured/comparison/tree-walk.js +136 -0
- package/dist/structured/comparison/tree-walk.js.map +1 -0
- package/dist/structured/cpython/python-text.d.ts +5 -0
- package/dist/structured/cpython/python-text.d.ts.map +1 -0
- package/dist/structured/cpython/python-text.js +79 -0
- package/dist/structured/cpython/python-text.js.map +1 -0
- package/dist/structured/index.d.ts +39 -0
- package/dist/structured/index.d.ts.map +1 -0
- package/dist/structured/index.js +29 -0
- package/dist/structured/index.js.map +1 -0
- package/dist/structured/promptfoo/scorer-assertion.d.ts +22 -0
- package/dist/structured/promptfoo/scorer-assertion.d.ts.map +1 -0
- package/dist/structured/promptfoo/scorer-assertion.js +34 -0
- package/dist/structured/promptfoo/scorer-assertion.js.map +1 -0
- package/dist/structured/schema/draft-07-schema.d.ts +3 -0
- package/dist/structured/schema/draft-07-schema.d.ts.map +1 -0
- package/dist/structured/schema/draft-07-schema.js +243 -0
- package/dist/structured/schema/draft-07-schema.js.map +1 -0
- package/dist/structured/schema/draft-2020-12-schema.d.ts +4 -0
- package/dist/structured/schema/draft-2020-12-schema.d.ts.map +1 -0
- package/dist/structured/schema/draft-2020-12-schema.js +280 -0
- package/dist/structured/schema/draft-2020-12-schema.js.map +1 -0
- package/dist/structured/schema/is-record.d.ts +2 -0
- package/dist/structured/schema/is-record.d.ts.map +1 -0
- package/dist/structured/schema/is-record.js +3 -0
- package/dist/structured/schema/is-record.js.map +1 -0
- package/dist/structured/schema/json-schema.d.ts +20 -0
- package/dist/structured/schema/json-schema.d.ts.map +1 -0
- package/dist/structured/schema/json-schema.js +244 -0
- package/dist/structured/schema/json-schema.js.map +1 -0
- package/dist/structured/schema/schema-type-walk.d.ts +11 -0
- package/dist/structured/schema/schema-type-walk.d.ts.map +1 -0
- package/dist/structured/schema/schema-type-walk.js +196 -0
- package/dist/structured/schema/schema-type-walk.js.map +1 -0
- package/dist/structured/scorers/all-matched.d.ts +10 -0
- package/dist/structured/scorers/all-matched.d.ts.map +1 -0
- package/dist/structured/scorers/all-matched.js +36 -0
- package/dist/structured/scorers/all-matched.js.map +1 -0
- package/dist/structured/scorers/exact-match.d.ts +7 -0
- package/dist/structured/scorers/exact-match.d.ts.map +1 -0
- package/dist/structured/scorers/exact-match.js +13 -0
- package/dist/structured/scorers/exact-match.js.map +1 -0
- package/dist/structured/scorers/item-matches.d.ts +7 -0
- package/dist/structured/scorers/item-matches.d.ts.map +1 -0
- package/dist/structured/scorers/item-matches.js +17 -0
- package/dist/structured/scorers/item-matches.js.map +1 -0
- package/dist/structured/scorers/leaf-matches.d.ts +14 -0
- package/dist/structured/scorers/leaf-matches.d.ts.map +1 -0
- package/dist/structured/scorers/leaf-matches.js +28 -0
- package/dist/structured/scorers/leaf-matches.js.map +1 -0
- package/dist/structured/scorers/parse-success.d.ts +7 -0
- package/dist/structured/scorers/parse-success.d.ts.map +1 -0
- package/dist/structured/scorers/parse-success.js +11 -0
- package/dist/structured/scorers/parse-success.js.map +1 -0
- package/dist/structured/scorers/path-matches.d.ts +5 -0
- package/dist/structured/scorers/path-matches.d.ts.map +1 -0
- package/dist/structured/scorers/path-matches.js +9 -0
- package/dist/structured/scorers/path-matches.js.map +1 -0
- package/dist/structured/scorers/required-key-recall.d.ts +8 -0
- package/dist/structured/scorers/required-key-recall.d.ts.map +1 -0
- package/dist/structured/scorers/required-key-recall.js +50 -0
- package/dist/structured/scorers/required-key-recall.js.map +1 -0
- package/dist/structured/scorers/schema-compliance.d.ts +8 -0
- package/dist/structured/scorers/schema-compliance.d.ts.map +1 -0
- package/dist/structured/scorers/schema-compliance.js +29 -0
- package/dist/structured/scorers/schema-compliance.js.map +1 -0
- package/dist/structured/scorers/token-f1.d.ts +14 -0
- package/dist/structured/scorers/token-f1.d.ts.map +1 -0
- package/dist/structured/scorers/token-f1.js +44 -0
- package/dist/structured/scorers/token-f1.js.map +1 -0
- package/dist/structured/scorers/type-precision.d.ts +10 -0
- package/dist/structured/scorers/type-precision.d.ts.map +1 -0
- package/dist/structured/scorers/type-precision.js +30 -0
- package/dist/structured/scorers/type-precision.js.map +1 -0
- package/dist/structured/scorers/valid-schema.d.ts +7 -0
- package/dist/structured/scorers/valid-schema.d.ts.map +1 -0
- package/dist/structured/scorers/valid-schema.js +11 -0
- package/dist/structured/scorers/valid-schema.js.map +1 -0
- package/dist/structured/scorers/value-matches.d.ts +6 -0
- package/dist/structured/scorers/value-matches.d.ts.map +1 -0
- package/dist/structured/scorers/value-matches.js +9 -0
- package/dist/structured/scorers/value-matches.js.map +1 -0
- package/dist/structured/scoring/case.d.ts +14 -0
- package/dist/structured/scoring/case.d.ts.map +1 -0
- package/dist/structured/scoring/case.js +14 -0
- package/dist/structured/scoring/case.js.map +1 -0
- package/dist/structured/scoring/config-case.d.ts +11 -0
- package/dist/structured/scoring/config-case.d.ts.map +1 -0
- package/dist/structured/scoring/config-case.js +22 -0
- package/dist/structured/scoring/config-case.js.map +1 -0
- package/dist/structured/scoring/per-field.d.ts +9 -0
- package/dist/structured/scoring/per-field.d.ts.map +1 -0
- package/dist/structured/scoring/per-field.js +23 -0
- package/dist/structured/scoring/per-field.js.map +1 -0
- package/dist/structured/scoring/scorer-set.d.ts +15 -0
- package/dist/structured/scoring/scorer-set.d.ts.map +1 -0
- package/dist/structured/scoring/scorer-set.js +60 -0
- package/dist/structured/scoring/scorer-set.js.map +1 -0
- package/dist/structured/scoring/text-vars-case.d.ts +12 -0
- package/dist/structured/scoring/text-vars-case.d.ts.map +1 -0
- package/dist/structured/scoring/text-vars-case.js +31 -0
- package/dist/structured/scoring/text-vars-case.js.map +1 -0
- package/package.json +78 -4
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import { Validator, dereference, validate, } from '@cfworker/json-schema';
|
|
2
|
+
import { toJsonParseValue, } from '@zukhruf/python';
|
|
3
|
+
import { draft07 } from "./draft-07-schema.js";
|
|
4
|
+
import { draft202012, draft202012Vocabularies, } from "./draft-2020-12-schema.js";
|
|
5
|
+
import { isRecord } from "./is-record.js";
|
|
6
|
+
import { SchemaTypeWalk } from "./schema-type-walk.js";
|
|
7
|
+
// A JSON Schema, read with one draft (Facade over cfworker's validator). The
|
|
8
|
+
// draft is the one that the schema's own $schema names, else 2020-12, unless
|
|
9
|
+
// the caller sets it: jsonschema's Draft7Validator reads every schema as
|
|
10
|
+
// draft 7, whatever its $schema says. The validator and the walk of the types
|
|
11
|
+
// read the schema with that one draft, and each $ref through one lookup, so
|
|
12
|
+
// one schema has one meaning (ADR 0011). A format is an annotation: it fails
|
|
13
|
+
// no value, as the 2020-12 format-annotation vocabulary says and as
|
|
14
|
+
// Draft7Validator, which has no format checker, does.
|
|
15
|
+
export class JsonSchema {
|
|
16
|
+
static #drafts = new Map([
|
|
17
|
+
['http://json-schema.org/draft-07/schema', '7'],
|
|
18
|
+
['https://json-schema.org/draft/2020-12/schema', '2020-12'],
|
|
19
|
+
]);
|
|
20
|
+
// The keywords that cfworker applies in any draft but that the draft does
|
|
21
|
+
// not define, so a validator of that draft ignores them (backlog #3775).
|
|
22
|
+
static #otherKeywords = {
|
|
23
|
+
'7': [
|
|
24
|
+
'prefixItems',
|
|
25
|
+
'dependentRequired',
|
|
26
|
+
'dependentSchemas',
|
|
27
|
+
'unevaluatedProperties',
|
|
28
|
+
'unevaluatedItems',
|
|
29
|
+
'minContains',
|
|
30
|
+
'maxContains',
|
|
31
|
+
'$recursiveRef',
|
|
32
|
+
'$recursiveAnchor',
|
|
33
|
+
'$dynamicRef',
|
|
34
|
+
'$dynamicAnchor',
|
|
35
|
+
],
|
|
36
|
+
'2020-12': [
|
|
37
|
+
'additionalItems',
|
|
38
|
+
'dependencies',
|
|
39
|
+
'$recursiveRef',
|
|
40
|
+
'$recursiveAnchor',
|
|
41
|
+
],
|
|
42
|
+
};
|
|
43
|
+
// The keywords whose value is a schema, a list of schemas, or a map of
|
|
44
|
+
// schemas, in draft 7 and 2020-12. json-schema-traverse 1.0.0, its latest
|
|
45
|
+
// version, has no prefixItems, dependentSchemas or unevaluated keywords, so
|
|
46
|
+
// the package walks the schemas itself.
|
|
47
|
+
static #schemaKeywords = new Set([
|
|
48
|
+
'additionalItems',
|
|
49
|
+
'items',
|
|
50
|
+
'contains',
|
|
51
|
+
'additionalProperties',
|
|
52
|
+
'propertyNames',
|
|
53
|
+
'not',
|
|
54
|
+
'if',
|
|
55
|
+
'then',
|
|
56
|
+
'else',
|
|
57
|
+
'unevaluatedItems',
|
|
58
|
+
'unevaluatedProperties',
|
|
59
|
+
]);
|
|
60
|
+
static #listKeywords = new Set([
|
|
61
|
+
'items',
|
|
62
|
+
'prefixItems',
|
|
63
|
+
'allOf',
|
|
64
|
+
'anyOf',
|
|
65
|
+
'oneOf',
|
|
66
|
+
]);
|
|
67
|
+
static #mapKeywords = new Set([
|
|
68
|
+
'$defs',
|
|
69
|
+
'definitions',
|
|
70
|
+
'properties',
|
|
71
|
+
'patternProperties',
|
|
72
|
+
'dependencies',
|
|
73
|
+
'dependentSchemas',
|
|
74
|
+
]);
|
|
75
|
+
// cfworker reports each failed keyword, also the keywords that only hold a
|
|
76
|
+
// failed child (properties, items, $ref …). The errors name the others.
|
|
77
|
+
static #containers = new Set([
|
|
78
|
+
'properties',
|
|
79
|
+
'patternProperties',
|
|
80
|
+
'additionalProperties',
|
|
81
|
+
'items',
|
|
82
|
+
'prefixItems',
|
|
83
|
+
'additionalItems',
|
|
84
|
+
'contains',
|
|
85
|
+
'allOf',
|
|
86
|
+
'anyOf',
|
|
87
|
+
'oneOf',
|
|
88
|
+
'$ref',
|
|
89
|
+
'dependentSchemas',
|
|
90
|
+
'unevaluatedProperties',
|
|
91
|
+
'unevaluatedItems',
|
|
92
|
+
]);
|
|
93
|
+
// The meta-schema of each draft, as jsonschema's check_schema reads it: it
|
|
94
|
+
// asserts the regex format only, because jsonschema checks uri and
|
|
95
|
+
// uri-reference only with its format extras, which evaluate.py does not
|
|
96
|
+
// install.
|
|
97
|
+
static #metaSchemas = {
|
|
98
|
+
'7': new Validator(JsonSchema.#prepared(structuredClone(draft07), '7', ['regex']), '7', true),
|
|
99
|
+
'2020-12': JsonSchema.#metaSchema2020(['regex']),
|
|
100
|
+
};
|
|
101
|
+
// The meta-schemas that a $ref of a schema reaches through the registry of
|
|
102
|
+
// jsonschema, as a validator of the draft reads them.
|
|
103
|
+
static #referencedMetaSchemas = {
|
|
104
|
+
'7': [JsonSchema.#prepared(structuredClone(draft07), '7', [])],
|
|
105
|
+
'2020-12': [draft202012, ...draft202012Vocabularies].map((schema) => JsonSchema.#prepared(JsonSchema.#static(schema), '2020-12', [])),
|
|
106
|
+
};
|
|
107
|
+
value;
|
|
108
|
+
draft;
|
|
109
|
+
// The types that the schema allows at a path (ADR 0008).
|
|
110
|
+
types;
|
|
111
|
+
// The schema when it is valid, as the validator reads it.
|
|
112
|
+
#validSchema;
|
|
113
|
+
#lookup;
|
|
114
|
+
constructor(value, { draft } = {}) {
|
|
115
|
+
this.value = value.value;
|
|
116
|
+
this.draft = draft ?? JsonSchema.#draftOf(this.value);
|
|
117
|
+
const schema = JsonSchema.#prepared(toJsonParseValue(this.value), this.draft, []);
|
|
118
|
+
this.#lookup = this.#lookupOf(schema);
|
|
119
|
+
this.types = new SchemaTypeWalk(schema, (node) => this.#referenced(node), this.draft);
|
|
120
|
+
this.#validSchema =
|
|
121
|
+
this.value instanceof Map &&
|
|
122
|
+
isRecord(schema) &&
|
|
123
|
+
JsonSchema.#metaSchemas[this.draft].validate(toJsonParseValue(this.value))
|
|
124
|
+
.valid
|
|
125
|
+
? schema
|
|
126
|
+
: undefined;
|
|
127
|
+
}
|
|
128
|
+
// is_valid_schema of evaluate.py: a dict that the meta-schema of the draft
|
|
129
|
+
// accepts.
|
|
130
|
+
get valid() {
|
|
131
|
+
return this.#validSchema !== undefined;
|
|
132
|
+
}
|
|
133
|
+
// Checks a value against the schema, which must be valid. The validator
|
|
134
|
+
// reads the JSON.parse value, and it compares 1 and 1.0 as jsonschema does:
|
|
135
|
+
// an integral float is an integer.
|
|
136
|
+
validate(value) {
|
|
137
|
+
if (this.#validSchema === undefined)
|
|
138
|
+
throw new Error('A value is checked only against a valid schema');
|
|
139
|
+
const result = validate(toJsonParseValue(value), this.#validSchema, this.draft, this.#lookup, false);
|
|
140
|
+
return {
|
|
141
|
+
valid: result.valid,
|
|
142
|
+
errors: [
|
|
143
|
+
...new Set(result.errors
|
|
144
|
+
.filter((error) => !JsonSchema.#containers.has(error.keyword))
|
|
145
|
+
.map((error) => JsonSchema.#line(error))),
|
|
146
|
+
],
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
// The schema that the $ref of a schema names, as the validator resolves it,
|
|
150
|
+
// or undefined: dereference writes the absolute URI of each $ref, and the
|
|
151
|
+
// lookup holds each schema by its URI.
|
|
152
|
+
#referenced({ __absolute_ref__: uri }) {
|
|
153
|
+
return uri === undefined ? undefined : this.#lookup[uri];
|
|
154
|
+
}
|
|
155
|
+
// The schemas that a $ref of the schema can name, by URI: those of the
|
|
156
|
+
// schema, and the meta-schemas of its draft, which the registry of
|
|
157
|
+
// jsonschema holds at their URIs. A schema whose own $id is such a URI
|
|
158
|
+
// takes its place, as in jsonschema. cfworker has no registry, and
|
|
159
|
+
// Validator.addSchema throws for such a schema, so the lookup is built with
|
|
160
|
+
// dereference, which Validator calls.
|
|
161
|
+
#lookupOf(schema) {
|
|
162
|
+
const lookup = isRecord(schema)
|
|
163
|
+
? dereference(schema)
|
|
164
|
+
: {};
|
|
165
|
+
for (const meta of JsonSchema.#referencedMetaSchemas[this.draft])
|
|
166
|
+
if (meta.$id !== undefined &&
|
|
167
|
+
lookup[meta.$id.replace(/#$/, '')] === undefined)
|
|
168
|
+
dereference(meta, lookup);
|
|
169
|
+
return lookup;
|
|
170
|
+
}
|
|
171
|
+
// The $schema keyword names the draft; a schema without it, or with a
|
|
172
|
+
// $schema that is not text, is read as 2020-12.
|
|
173
|
+
static #draftOf(value) {
|
|
174
|
+
const uri = value instanceof Map ? value.get('$schema') : undefined;
|
|
175
|
+
if (typeof uri !== 'string')
|
|
176
|
+
return '2020-12';
|
|
177
|
+
const draft = JsonSchema.#drafts.get(uri.replace(/#$/, ''));
|
|
178
|
+
if (draft === undefined)
|
|
179
|
+
throw new Error(`The $schema ${uri} is not a draft that the package reads: 7 or 2020-12`);
|
|
180
|
+
return draft;
|
|
181
|
+
}
|
|
182
|
+
// The 2020-12 meta-schema with its vocabularies.
|
|
183
|
+
static #metaSchema2020(assertedFormats) {
|
|
184
|
+
const validator = new Validator(JsonSchema.#prepared(JsonSchema.#static(draft202012), '2020-12', assertedFormats), '2020-12', true);
|
|
185
|
+
for (const vocabulary of draft202012Vocabularies)
|
|
186
|
+
validator.addSchema(JsonSchema.#prepared(JsonSchema.#static(vocabulary), '2020-12', assertedFormats));
|
|
187
|
+
return validator;
|
|
188
|
+
}
|
|
189
|
+
// A copy of a 2020-12 meta-schema whose `$dynamicRef: "#meta"` is a $ref to
|
|
190
|
+
// the meta-schema. cfworker 4.1.4 has no $dynamicRef, and with no other
|
|
191
|
+
// `$dynamicAnchor: "meta"` in reach, the dynamic reference of each
|
|
192
|
+
// vocabulary resolves to the 2020-12 meta-schema (core section 8.2.3.2).
|
|
193
|
+
static #static(schema) {
|
|
194
|
+
const copy = structuredClone(schema);
|
|
195
|
+
const visit = (value) => {
|
|
196
|
+
if (Array.isArray(value))
|
|
197
|
+
value.forEach(visit);
|
|
198
|
+
if (!isRecord(value))
|
|
199
|
+
return;
|
|
200
|
+
if (value['$dynamicRef'] === '#meta') {
|
|
201
|
+
Reflect.deleteProperty(value, '$dynamicRef');
|
|
202
|
+
Reflect.set(value, '$ref', 'https://json-schema.org/draft/2020-12/schema');
|
|
203
|
+
}
|
|
204
|
+
Object.values(value).forEach(visit);
|
|
205
|
+
};
|
|
206
|
+
visit(copy);
|
|
207
|
+
return copy;
|
|
208
|
+
}
|
|
209
|
+
// A schema as a validator of the draft reads it, without the formats that
|
|
210
|
+
// it does not assert: cfworker asserts every format that it knows, and it
|
|
211
|
+
// applies the keywords of every draft. So the walk deletes the other
|
|
212
|
+
// formats and the keywords of other drafts from the copy that it is given,
|
|
213
|
+
// in each schema before it steps into its keywords, so a deleted keyword is
|
|
214
|
+
// not visited.
|
|
215
|
+
static #prepared(copy, draft, assertedFormats) {
|
|
216
|
+
const visit = (node) => {
|
|
217
|
+
if (!isRecord(node))
|
|
218
|
+
return;
|
|
219
|
+
const schema = node;
|
|
220
|
+
if (!assertedFormats.some((format) => schema['format'] === format))
|
|
221
|
+
Reflect.deleteProperty(schema, 'format');
|
|
222
|
+
for (const keyword of JsonSchema.#otherKeywords[draft])
|
|
223
|
+
Reflect.deleteProperty(schema, keyword);
|
|
224
|
+
for (const [keyword, child] of Object.entries(schema)) {
|
|
225
|
+
if (Array.isArray(child)) {
|
|
226
|
+
if (JsonSchema.#listKeywords.has(keyword))
|
|
227
|
+
child.forEach(visit);
|
|
228
|
+
}
|
|
229
|
+
else if (JsonSchema.#mapKeywords.has(keyword)) {
|
|
230
|
+
if (isRecord(child))
|
|
231
|
+
Object.values(child).forEach(visit);
|
|
232
|
+
}
|
|
233
|
+
else if (JsonSchema.#schemaKeywords.has(keyword))
|
|
234
|
+
visit(child);
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
visit(copy);
|
|
238
|
+
return copy;
|
|
239
|
+
}
|
|
240
|
+
static #line({ instanceLocation, error }) {
|
|
241
|
+
return `${instanceLocation.replace(/^#/, '') || '/'} ${error}`;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
//# sourceMappingURL=json-schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json-schema.js","sourceRoot":"","sources":["../../../src/structured/schema/json-schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,SAAS,EACT,WAAW,EACX,QAAQ,GACT,MAAM,uBAAuB,CAAC;AAE/B,OAAO,EAGL,gBAAgB,GACjB,MAAM,iBAAiB,CAAC;AAGzB,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAC/C,OAAO,EACL,WAAW,EACX,uB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CAAC,KAAK,CAAC;wBAAE,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;gBAC3D,CAAC;qBAAM,IAAI,UAAU,CAAC,eAAe,CAAC,GAAG,CAAC,OAAO,CAAC;oBAAE,KAAK,CAAC,KAAK,CAAC,CAAC;YACnE,CAAC;QACH,CAAC,CAAC;QACF,KAAK,CAAC,IAAI,CAAC,CAAC;QACZ,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,CAAC,KAAK,CAAC,EAAE,gBAAgB,EAAE,KAAK,EAAc;QAClD,OAAO,GAAG,gBAAgB,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,GAAG,IAAI,KAAK,EAAE,CAAC;IACjE,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { JsonData } from '@zukhruf/python';
|
|
2
|
+
import type { Path } from '../comparison/path.ts';
|
|
3
|
+
import type { Draft } from './json-schema.ts';
|
|
4
|
+
type SchemaObject = Readonly<Record<string, unknown>>;
|
|
5
|
+
export declare class SchemaTypeWalk {
|
|
6
|
+
#private;
|
|
7
|
+
constructor(root: unknown, referenced: (schema: SchemaObject) => unknown, draft: Draft);
|
|
8
|
+
allows(path: Path, value: JsonData): boolean;
|
|
9
|
+
}
|
|
10
|
+
export {};
|
|
11
|
+
//# sourceMappingURL=schema-type-walk.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema-type-walk.d.ts","sourceRoot":"","sources":["../../../src/structured/schema/schema-type-walk.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAEhD,OAAO,KAAK,EAAE,IAAI,EAAQ,MAAM,uBAAuB,CAAC;AAExD,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAM9C,KAAK,YAAY,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;AAatD,qBAAa,cAAc;;gBAwBvB,IAAI,EAAE,OAAO,EACb,UAAU,EAAE,CAAC,MAAM,EAAE,YAAY,KAAK,OAAO,EAC7C,KAAK,EAAE,KAAK;IASd,MAAM,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,QAAQ,GAAG,OAAO;CA2M7C"}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { isRecord } from "./is-record.js";
|
|
2
|
+
// The types that a JSON Schema allows for the value at a path of a stated
|
|
3
|
+
// value, such as offers, 0, value, amount, by the rules of the draft that
|
|
4
|
+
// reads the schema. The walk reads the schema as JSON.parse gives it:
|
|
5
|
+
// properties, patternProperties and additionalProperties for a name; for an
|
|
6
|
+
// index, items and additionalItems in draft 7, and prefixItems then items in
|
|
7
|
+
// 2020-12; the union of anyOf and oneOf branches, the intersection of allOf,
|
|
8
|
+
// $refs, and type, const and enum. Draft 7 ignores the other keywords of a
|
|
9
|
+
// schema that has a $ref, and 2020-12 applies them. It does not read if, then,
|
|
10
|
+
// else, not, dependencies, dependentSchemas or $dynamicRef, so they restrict
|
|
11
|
+
// nothing here. An empty set means that the schema allows no value at the
|
|
12
|
+
// path (ADR 0008, ADR 0011).
|
|
13
|
+
export class SchemaTypeWalk {
|
|
14
|
+
static #anyType = new Set([
|
|
15
|
+
'string',
|
|
16
|
+
'integer',
|
|
17
|
+
'number',
|
|
18
|
+
'boolean',
|
|
19
|
+
'object',
|
|
20
|
+
'array',
|
|
21
|
+
'null',
|
|
22
|
+
]);
|
|
23
|
+
static #noType = new Set();
|
|
24
|
+
// The type names, for reading the type keyword of a schema.
|
|
25
|
+
static #names = SchemaTypeWalk.#anyType;
|
|
26
|
+
#root;
|
|
27
|
+
// The schema that the $ref of a schema names, or undefined. Its caller
|
|
28
|
+
// resolves a $ref as its validator does, so that the walk and the validator
|
|
29
|
+
// read the same schema.
|
|
30
|
+
#referenced;
|
|
31
|
+
#draft;
|
|
32
|
+
constructor(root, referenced, draft) {
|
|
33
|
+
this.#root = root;
|
|
34
|
+
this.#referenced = referenced;
|
|
35
|
+
this.#draft = draft;
|
|
36
|
+
}
|
|
37
|
+
// Whether the schema allows the type of the value at the path. An integer
|
|
38
|
+
// is also a number (draft 7, validation section 6.1.1).
|
|
39
|
+
allows(path, value) {
|
|
40
|
+
const allowed = this.#allowed(this.#root, path.steps, new Set());
|
|
41
|
+
const type = SchemaTypeWalk.#typeOf(value);
|
|
42
|
+
return allowed.has(type) || (type === 'integer' && allowed.has('number'));
|
|
43
|
+
}
|
|
44
|
+
// followed holds the schemas whose $ref the walk followed since the last
|
|
45
|
+
// step, so that a $ref loop ends.
|
|
46
|
+
#allowed(schema, steps, followed) {
|
|
47
|
+
if (schema === true)
|
|
48
|
+
return SchemaTypeWalk.#anyType;
|
|
49
|
+
if (!isRecord(schema))
|
|
50
|
+
return SchemaTypeWalk.#noType;
|
|
51
|
+
// Draft 7 ignores the other keywords of a schema that has a $ref
|
|
52
|
+
// (draft-handrews-json-schema-01, section 8.3); 2020-12 applies them with
|
|
53
|
+
// the $ref (core section 8.2.3.1). A $ref that names no schema allows no
|
|
54
|
+
// type.
|
|
55
|
+
if (typeof schema['$ref'] === 'string') {
|
|
56
|
+
if (followed.has(schema))
|
|
57
|
+
return SchemaTypeWalk.#noType;
|
|
58
|
+
const referenced = this.#allowed(this.#referenced(schema), steps, new Set([...followed, schema]));
|
|
59
|
+
return this.#draft === '7'
|
|
60
|
+
? referenced
|
|
61
|
+
: referenced.intersection(this.#applied(schema, steps, followed));
|
|
62
|
+
}
|
|
63
|
+
return this.#applied(schema, steps, followed);
|
|
64
|
+
}
|
|
65
|
+
// The types that the keywords of a schema other than $ref allow.
|
|
66
|
+
#applied(schema, steps, followed) {
|
|
67
|
+
const [step, ...rest] = steps;
|
|
68
|
+
let types = step === undefined
|
|
69
|
+
? SchemaTypeWalk.#ownTypes(schema)
|
|
70
|
+
: this.#childTypes(schema, step, rest);
|
|
71
|
+
for (const keyword of ['anyOf', 'oneOf']) {
|
|
72
|
+
const branches = schema[keyword];
|
|
73
|
+
if (Array.isArray(branches))
|
|
74
|
+
types = types.intersection(SchemaTypeWalk.#union(branches.map((branch) => this.#allowed(branch, steps, followed))));
|
|
75
|
+
}
|
|
76
|
+
const members = schema['allOf'];
|
|
77
|
+
if (!Array.isArray(members))
|
|
78
|
+
return types;
|
|
79
|
+
return members.reduce((all, member) => all.intersection(this.#allowed(member, steps, followed)), types);
|
|
80
|
+
}
|
|
81
|
+
// The types that the value under the next step can have. A step starts a
|
|
82
|
+
// new list of followed $refs.
|
|
83
|
+
#childTypes(schema, step, rest) {
|
|
84
|
+
const declared = SchemaTypeWalk.#declaredTypes(schema);
|
|
85
|
+
if (typeof step === 'number') {
|
|
86
|
+
if (declared !== undefined && !declared.has('array'))
|
|
87
|
+
return SchemaTypeWalk.#noType;
|
|
88
|
+
return this.#allowed(this.#itemSchema(schema, step), rest, new Set());
|
|
89
|
+
}
|
|
90
|
+
if (declared !== undefined && !declared.has('object'))
|
|
91
|
+
return SchemaTypeWalk.#noType;
|
|
92
|
+
return SchemaTypeWalk.#propertySchemas(schema, step).reduce((types, child) => types.intersection(this.#allowed(child, rest, new Set())), SchemaTypeWalk.#anyType);
|
|
93
|
+
}
|
|
94
|
+
// The types that a schema allows for its own value: those of type, and of
|
|
95
|
+
// the values of const and enum. A schema without them allows any type.
|
|
96
|
+
static #ownTypes(schema) {
|
|
97
|
+
let types = SchemaTypeWalk.#declaredTypes(schema) ?? SchemaTypeWalk.#anyType;
|
|
98
|
+
const constant = schema['const'];
|
|
99
|
+
if (constant !== undefined)
|
|
100
|
+
types = types.intersection(SchemaTypeWalk.#typesOfValue(constant));
|
|
101
|
+
const values = schema['enum'];
|
|
102
|
+
if (Array.isArray(values))
|
|
103
|
+
types = types.intersection(SchemaTypeWalk.#union(values.map((value) => SchemaTypeWalk.#typesOfValue(value))));
|
|
104
|
+
return types;
|
|
105
|
+
}
|
|
106
|
+
// The types of a const or enum value: an integer is also a number.
|
|
107
|
+
static #typesOfValue(value) {
|
|
108
|
+
const type = SchemaTypeWalk.#typeOf(value);
|
|
109
|
+
return new Set(type === 'integer' ? ['integer', 'number'] : [type]);
|
|
110
|
+
}
|
|
111
|
+
// The schema of the item at an index. In 2020-12: the schema at its
|
|
112
|
+
// position in prefixItems, then items. In draft 7: items, or the schema at
|
|
113
|
+
// its position when items is a list, then additionalItems. Absent means any
|
|
114
|
+
// item.
|
|
115
|
+
#itemSchema(schema, index) {
|
|
116
|
+
const items = schema['items'];
|
|
117
|
+
if (this.#draft === '2020-12') {
|
|
118
|
+
const prefix = schema['prefixItems'];
|
|
119
|
+
const item = Array.isArray(prefix) ? prefix[index] : undefined;
|
|
120
|
+
if (item !== undefined)
|
|
121
|
+
return item;
|
|
122
|
+
return items === undefined ? true : items;
|
|
123
|
+
}
|
|
124
|
+
if (!Array.isArray(items))
|
|
125
|
+
return items === undefined ? true : items;
|
|
126
|
+
const item = items[index];
|
|
127
|
+
if (item !== undefined)
|
|
128
|
+
return item;
|
|
129
|
+
const additional = schema['additionalItems'];
|
|
130
|
+
return additional === undefined ? true : additional;
|
|
131
|
+
}
|
|
132
|
+
// The schemas that apply to a property: its own and those of each matching
|
|
133
|
+
// pattern, or else additionalProperties. Absent means any value. A name
|
|
134
|
+
// such as "constructor" is read only as an own property.
|
|
135
|
+
static #propertySchemas(schema, name) {
|
|
136
|
+
const schemas = [];
|
|
137
|
+
const properties = schema['properties'];
|
|
138
|
+
if (isRecord(properties) && Object.hasOwn(properties, name))
|
|
139
|
+
schemas.push(properties[name]);
|
|
140
|
+
const patterns = schema['patternProperties'];
|
|
141
|
+
if (isRecord(patterns))
|
|
142
|
+
for (const [pattern, child] of Object.entries(patterns))
|
|
143
|
+
if (SchemaTypeWalk.#matches(pattern, name))
|
|
144
|
+
schemas.push(child);
|
|
145
|
+
if (schemas.length > 0)
|
|
146
|
+
return schemas;
|
|
147
|
+
const additional = schema['additionalProperties'];
|
|
148
|
+
return [additional === undefined ? true : additional];
|
|
149
|
+
}
|
|
150
|
+
// Whether a patternProperties pattern matches a name. A pattern that is not
|
|
151
|
+
// a regular expression matches no name: a schema with one is not valid.
|
|
152
|
+
static #matches(pattern, name) {
|
|
153
|
+
try {
|
|
154
|
+
return new RegExp(pattern, 'u').test(name);
|
|
155
|
+
}
|
|
156
|
+
catch (error) {
|
|
157
|
+
if (error instanceof SyntaxError)
|
|
158
|
+
return false;
|
|
159
|
+
throw error;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
// The types of a schema's type keyword: a name or a list of names.
|
|
163
|
+
static #declaredTypes(schema) {
|
|
164
|
+
const type = schema['type'];
|
|
165
|
+
if (type === undefined)
|
|
166
|
+
return undefined;
|
|
167
|
+
const names = Array.isArray(type) ? type : [type];
|
|
168
|
+
return new Set(names.filter((name) => typeof name === 'string' && SchemaTypeWalk.#names.has(name)));
|
|
169
|
+
}
|
|
170
|
+
static #union(sets) {
|
|
171
|
+
return sets.reduce((all, set) => all.union(set), SchemaTypeWalk.#noType);
|
|
172
|
+
}
|
|
173
|
+
// The draft 7 type of a stated value or of a value in the schema: an int,
|
|
174
|
+
// or a float with a zero fraction such as 1.0, is an integer (validation
|
|
175
|
+
// section 6.1.1), though Python reads 1.0 as a float (ADR 0008). An object
|
|
176
|
+
// is a Map of a stated value, or an object of the schema.
|
|
177
|
+
static #typeOf(value) {
|
|
178
|
+
if (value === null)
|
|
179
|
+
return 'null';
|
|
180
|
+
if (Array.isArray(value))
|
|
181
|
+
return 'array';
|
|
182
|
+
switch (typeof value) {
|
|
183
|
+
case 'object':
|
|
184
|
+
return 'object';
|
|
185
|
+
case 'boolean':
|
|
186
|
+
return 'boolean';
|
|
187
|
+
case 'bigint':
|
|
188
|
+
return 'integer';
|
|
189
|
+
case 'number':
|
|
190
|
+
return Number.isInteger(value) ? 'integer' : 'number';
|
|
191
|
+
default:
|
|
192
|
+
return 'string';
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
//# sourceMappingURL=schema-type-walk.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema-type-walk.js","sourceRoot":"","sources":["../../../src/structured/schema/schema-type-walk.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAS1C,0EAA0E;AAC1E,0EAA0E;AAC1E,sEAAsE;AACtE,4EAA4E;AAC5E,6EAA6E;AAC7E,6EAA6E;AAC7E,2EAA2E;AAC3E,+EAA+E;AAC/E,6EAA6E;AAC7E,0EAA0E;AAC1E,6BAA6B;AAC7B,MAAM,OAAO,cAAc;IACzB,MAAM,CAAU,QAAQ,GAA0B,IAAI,GAAG,CAAW;QAClE,QAAQ;QACR,SAAS;QACT,QAAQ;QACR,SAAS;QACT,QAAQ;QACR,OAAO;QACP,MAAM;KACP,CAAC,CAAC;IAEH,MAAM,CAAU,OAAO,GAA0B,IAAI,GAAG,EAAY,CAAC;IAErE,4DAA4D;IAC5D,MAAM,CAAU,MAAM,GAAwB,cAAc,CAAC,QAAQ,CAAC;IAE7D,KAAK,CAAU;IACxB,uEAAuE;IACvE,4EAA4E;IAC5E,wBAAwB;IACf,WAAW,CAAoC;IAC/C,MAAM,CAAQ;IAEvB,YACE,IAAa,EACb,UAA6C,EAC7C,KAAY;QAEZ,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;QAClB,IAAI,CAAC,WAAW,GAAG,UAAU,CAAC;QAC9B,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IACtB,CAAC;IAED,0EAA0E;IAC1E,wDAAwD;IACxD,MAAM,CAAC,IAAU,EAAE,KAAe;QAChC,MAAM,OAAO,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,IAAI,GAAG,EAAE,CAAC,CAAC;QACjE,MAAM,IAAI,GAAG,cAAc,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;QA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+
import type { LeafOptions } from './leaf-matches.ts';
|
|
4
|
+
export declare class AllMatched extends Gate<Comparison> {
|
|
5
|
+
#private;
|
|
6
|
+
constructor({ nullIsMissing }?: LeafOptions);
|
|
7
|
+
holds(comparison: Comparison): boolean;
|
|
8
|
+
explain(comparison: Comparison): string;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=all-matched.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"all-matched.d.ts","sourceRoot":"","sources":["../../../src/structured/scorers/all-matched.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAC;AAC7C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,6BAA6B,CAAC;AAG9D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAOrD,qBAAa,UAAW,SAAQ,IAAI,CAAC,UAAU,CAAC;;gBAGlC,EAAE,aAAqB,EAAE,GAAE,WAAgB;IAKvD,KAAK,CAAC,UAAU,EAAE,UAAU,GAAG,OAAO;IAItC,OAAO,CAAC,UAAU,EAAE,UAAU,GAAG,MAAM;CA6BxC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Gate } from "../../scorers/gate.js";
|
|
2
|
+
import { canonicalJson } from "../cpython/python-text.js";
|
|
3
|
+
// 1 when every leaf pair of the comparison agrees and every item of every
|
|
4
|
+
// array has a pair, else 0. With nullIsMissing, a null leaf is no value, on
|
|
5
|
+
// both sides, as the matches of leaves read it. Its explanation names each
|
|
6
|
+
// array whose items do not all have a pair, and each leaf that does not
|
|
7
|
+
// agree.
|
|
8
|
+
export class AllMatched extends Gate {
|
|
9
|
+
#nullIsMissing;
|
|
10
|
+
constructor({ nullIsMissing = false } = {}) {
|
|
11
|
+
super();
|
|
12
|
+
this.#nullIsMissing = nullIsMissing;
|
|
13
|
+
}
|
|
14
|
+
holds(comparison) {
|
|
15
|
+
return this.#differences(comparison).length === 0;
|
|
16
|
+
}
|
|
17
|
+
explain(comparison) {
|
|
18
|
+
return this.#differences(comparison).join('; ');
|
|
19
|
+
}
|
|
20
|
+
#differences(comparison) {
|
|
21
|
+
return [
|
|
22
|
+
...comparison
|
|
23
|
+
.arrays()
|
|
24
|
+
.filter((items) => items.paired !== items.expected || items.paired !== items.stated)
|
|
25
|
+
.map((items) => `${items.field}: ${items.paired} pairs of ${items.expected} expected and ${items.stated} stated items`),
|
|
26
|
+
...comparison
|
|
27
|
+
.leaves({ nullIsMissing: this.#nullIsMissing })
|
|
28
|
+
.filter((pair) => pair.agrees !== true)
|
|
29
|
+
.map((pair) => `${(pair.expected ?? pair.stated).path}: expected ${AllMatched.#shown(pair.expected)}, stated ${AllMatched.#shown(pair.stated)}`),
|
|
30
|
+
];
|
|
31
|
+
}
|
|
32
|
+
static #shown(leaf) {
|
|
33
|
+
return leaf === undefined ? 'nothing' : canonicalJson(leaf.value);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=all-matched.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"all-matched.js","sourceRoot":"","sources":["../../../src/structured/scorers/all-matched.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAC;AAG7C,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAG1D,0EAA0E;AAC1E,4EAA4E;AAC5E,2EAA2E;AAC3E,wEAAwE;AACxE,SAAS;AACT,MAAM,OAAO,UAAW,SAAQ,IAAgB;IACrC,cAAc,CAAU;IAEjC,YAAY,EAAE,aAAa,GAAG,KAAK,KAAkB,EAAE;QACrD,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,cAAc,GAAG,aAAa,CAAC;IACtC,CAAC;IAED,KAAK,CAAC,UAAsB;QAC1B,OAAO,IAAI,CAAC,YAAY,CAAC,UAAU,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC;IACpD,CAAC;IAED,OAAO,CAAC,UAAsB;QAC5B,OAAO,IAAI,CAAC,YAAY,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAClD,CAAC;IAED,YAAY,CAAC,UAAsB;QACjC,OAAO;YACL,GAAG,UAAU;iBACV,MAAM,EAAE;iBACR,MAAM,CACL,CAAC,KAAK,EAAE,EAAE,CACR,KAAK,CAAC,MAAM,KAAK,KAAK,CAAC,QAAQ,IAAI,KAAK,CAAC,MAAM,KAAK,KAAK,CAAC,MAAM,CACnE;iBACA,GAAG,CACF,CAAC,KAAK,EAAE,EAAE,CACR,GAAG,KAAK,CAAC,KAAK,KAAK,KAAK,CAAC,MAAM,aAAa,KAAK,CAAC,QAAQ,iBAAiB,KAAK,CAAC,MAAM,eAAe,CACzG;YACH,GAAG,UAAU;iBACV,MAAM,CAAC,EAAE,aAAa,EAAE,IAAI,CAAC,cAAc,EAAE,CAAC;iBAC9C,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,MAAM,KAAK,IAAI,CAAC;iBACtC,GAAG,CACF,CAAC,IAAI,EAAE,EAAE,CACP,GAAG,CAAC,IAAI,CAAC,QAAQ,IAAI,IAAI,CAAC,MAAM,CAAC,CAAC,IAAI,cAAc,UAAU,CAAC,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,YAAY,UAAU,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CACnI;SACJ,CAAC;IACJ,CAAC;IAED,MAAM,CAAC,MAAM,CAAC,IAAsB;QAClC,OAAO,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,aAAa,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACpE,CAAC;CACF"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Gate } from '../../scorers/gate.ts';
|
|
2
|
+
import type { Comparison } from '../comparison/comparison.ts';
|
|
3
|
+
export declare class ExactMatch extends Gate<Comparison> {
|
|
4
|
+
holds({ stated, case: testCase }: Comparison): boolean;
|
|
5
|
+
explain(): string;
|
|
6
|
+
}
|
|
7
|
+
//# sourceMappingURL=exact-match.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"exact-match.d.ts","sourceRoot":"","sources":["../../../src/structured/scorers/exact-match.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAC;AAC7C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,6BAA6B,CAAC;AAK9D,qBAAa,UAAW,SAAQ,IAAI,CAAC,UAAU,CAAC;IAC9C,KAAK,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE,UAAU,GAAG,OAAO;IAItD,OAAO,IAAI,MAAM;CAGlB"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Gate } from "../../scorers/gate.js";
|
|
2
|
+
import { canonicalJson } from "../cpython/python-text.js";
|
|
3
|
+
// 1 when the canonical JSON of the stated value (sorted keys, no spaces)
|
|
4
|
+
// equals that of the expected value, else 0. SOB puts no gate on it.
|
|
5
|
+
export class ExactMatch extends Gate {
|
|
6
|
+
holds({ stated, case: testCase }) {
|
|
7
|
+
return canonicalJson(stated) === canonicalJson(testCase.expected);
|
|
8
|
+
}
|
|
9
|
+
explain() {
|
|
10
|
+
return 'the canonical JSON of the stated value is not that of the expected value';
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=exact-match.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"exact-match.js","sourceRoot":"","sources":["../../../src/structured/scorers/exact-match.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAC;AAE7C,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAE1D,yEAAyE;AACzE,qEAAqE;AACrE,MAAM,OAAO,UAAW,SAAQ,IAAgB;IAC9C,KAAK,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAc;QAC1C,OAAO,aAAa,CAAC,MAAM,CAAC,KAAK,aAAa,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;IACpE,CAAC;IAED,OAAO;QACL,OAAO,0EAA0E,CAAC;IACpF,CAAC;CACF"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Counts } from '../../aggregation/counts.ts';
|
|
2
|
+
import type { Matches } from '../../scorers/matches.ts';
|
|
3
|
+
import type { Comparison } from '../comparison/comparison.ts';
|
|
4
|
+
export declare class ItemMatches implements Matches<Comparison> {
|
|
5
|
+
counts(comparison: Comparison): Counts;
|
|
6
|
+
}
|
|
7
|
+
//# sourceMappingURL=item-matches.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"item-matches.d.ts","sourceRoot":"","sources":["../../../src/structured/scorers/item-matches.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,6BAA6B,CAAC;AACrD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,6BAA6B,CAAC;AAI9D,qBAAa,WAAY,YAAW,OAAO,CAAC,UAAU,CAAC;IACrD,MAAM,CAAC,UAAU,EAAE,UAAU,GAAG,MAAM;CAWvC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Counts } from "../../aggregation/counts.js";
|
|
2
|
+
// The items of every array of one comparison: a hit is a pair that the
|
|
3
|
+
// alignment of its array made, of the stated items and the expected items.
|
|
4
|
+
export class ItemMatches {
|
|
5
|
+
counts(comparison) {
|
|
6
|
+
let hits = 0;
|
|
7
|
+
let stated = 0;
|
|
8
|
+
let expected = 0;
|
|
9
|
+
for (const items of comparison.arrays()) {
|
|
10
|
+
hits += items.paired;
|
|
11
|
+
stated += items.stated;
|
|
12
|
+
expected += items.expected;
|
|
13
|
+
}
|
|
14
|
+
return new Counts({ hits, stated, expected });
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=item-matches.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"item-matches.js","sourceRoot":"","sources":["../../../src/structured/scorers/item-matches.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,6BAA6B,CAAC;AAIrD,uEAAuE;AACvE,2EAA2E;AAC3E,MAAM,OAAO,WAAW;IACtB,MAAM,CAAC,UAAsB;QAC3B,IAAI,IAAI,GAAG,CAAC,CAAC;QACb,IAAI,MAAM,GAAG,CAAC,CAAC;QACf,IAAI,QAAQ,GAAG,CAAC,CAAC;QACjB,KAAK,MAAM,KAAK,IAAI,UAAU,CAAC,MAAM,EAAE,EAAE,CAAC;YACxC,IAAI,IAAI,KAAK,CAAC,MAAM,CAAC;YACrB,MAAM,IAAI,KAAK,CAAC,MAAM,CAAC;YACvB,QAAQ,IAAI,KAAK,CAAC,QAAQ,CAAC;QAC7B,CAAC;QACD,OAAO,IAAI,MAAM,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,CAAC;IAChD,CAAC;CACF"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Counts } from '../../aggregation/counts.ts';
|
|
2
|
+
import type { Matches } from '../../scorers/matches.ts';
|
|
3
|
+
import type { Comparison } from '../comparison/comparison.ts';
|
|
4
|
+
import type { BothSides } from '../comparison/leaf-pair.ts';
|
|
5
|
+
export type LeafOptions = {
|
|
6
|
+
readonly nullIsMissing?: boolean;
|
|
7
|
+
};
|
|
8
|
+
export declare abstract class LeafMatches implements Matches<Comparison> {
|
|
9
|
+
#private;
|
|
10
|
+
constructor({ nullIsMissing }?: LeafOptions);
|
|
11
|
+
protected abstract hit(pair: BothSides): boolean;
|
|
12
|
+
counts(comparison: Comparison): Counts;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=leaf-matches.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"leaf-matches.d.ts","sourceRoot":"","sources":["../../../src/structured/scorers/leaf-matches.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,6BAA6B,CAAC;AACrD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,6BAA6B,CAAC;AAC9D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,4BAA4B,CAAC;AAI5D,MAAM,MAAM,WAAW,GAAG;IAAE,QAAQ,CAAC,aAAa,CAAC,EAAE,OAAO,CAAA;CAAE,CAAC;AAK/D,8BAAsB,WAAY,YAAW,OAAO,CAAC,UAAU,CAAC;;gBAGlD,EAAE,aAAqB,EAAE,GAAE,WAAgB;IAIvD,SAAS,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,EAAE,SAAS,GAAG,OAAO;IAEhD,MAAM,CAAC,UAAU,EAAE,UAAU,GAAG,MAAM;CAcvC"}
|