@amritk/generate-examples 0.6.3 → 0.6.4
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.
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { getMjstInstanceOf, getMjstPrimitive } from "@amritk/helpers/mjst-extension";
|
|
2
|
+
import { readKey } from "@amritk/helpers/read-key";
|
|
2
3
|
import { resolveRef } from "@amritk/helpers/resolve-ref";
|
|
3
4
|
import { hasAdditionalProperties, hasAllOf, hasAnyOf, hasConst, hasDefault, hasDependentRequired, hasDependentSchemas, hasEnum, hasExamples, hasExclusiveMaximum, hasExclusiveMinimum, hasFormat, hasItems, hasMaxItems, hasMaximum, hasMaxLength, hasMaxProperties, hasMinItems, hasMinimum, hasMinLength, hasMinProperties, hasMultipleOf, hasOneOf, hasPattern, hasPatternProperties, hasProperties, hasPropertyNames, hasRef, hasRequired, hasType, hasUniqueItems, isSchemaObject } from "@amritk/helpers/schema-guards";
|
|
4
5
|
import { makeInstanceCheck, needsValidationFilter } from "./schema-validation.js";
|
|
@@ -67,6 +68,13 @@ const topLevelAlternatives = (s) => {
|
|
|
67
68
|
parts.push(cur);
|
|
68
69
|
return parts;
|
|
69
70
|
};
|
|
71
|
+
const matchesPattern = (pattern, value) => {
|
|
72
|
+
try {
|
|
73
|
+
return new RegExp(pattern).test(value);
|
|
74
|
+
} catch {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
70
78
|
const sampleFromPattern = (pattern, minLength) => {
|
|
71
79
|
let body = pattern;
|
|
72
80
|
if (body.startsWith("^"))
|
|
@@ -180,14 +188,14 @@ const FORMAT_EXAMPLES = {
|
|
|
180
188
|
};
|
|
181
189
|
const exampleString = (schema) => {
|
|
182
190
|
if (hasFormat(schema)) {
|
|
183
|
-
const formatted = FORMAT_EXAMPLES
|
|
191
|
+
const formatted = readKey(FORMAT_EXAMPLES, schema.format);
|
|
184
192
|
if (formatted !== void 0)
|
|
185
193
|
return formatted;
|
|
186
194
|
}
|
|
187
195
|
const minLength = hasMinLength(schema) ? schema.minLength : 0;
|
|
188
196
|
if (hasPattern(schema)) {
|
|
189
197
|
const sampled = sampleFromPattern(schema.pattern, minLength);
|
|
190
|
-
if (sampled !== void 0 && sampled.length >= minLength &&
|
|
198
|
+
if (sampled !== void 0 && sampled.length >= minLength && matchesPattern(schema.pattern, sampled)) {
|
|
191
199
|
if (!(hasMaxLength(schema) && sampled.length > schema.maxLength))
|
|
192
200
|
return sampled;
|
|
193
201
|
}
|
|
@@ -16,23 +16,47 @@ const FILTER_KEYWORDS = /* @__PURE__ */ new Set([
|
|
|
16
16
|
"maxProperties",
|
|
17
17
|
"contains"
|
|
18
18
|
]);
|
|
19
|
-
const
|
|
19
|
+
const SCHEMA_MAPS = /* @__PURE__ */ new Set([
|
|
20
|
+
"properties",
|
|
21
|
+
"patternProperties",
|
|
22
|
+
"$defs",
|
|
23
|
+
"definitions",
|
|
24
|
+
"dependentSchemas",
|
|
25
|
+
"dependencies"
|
|
26
|
+
]);
|
|
27
|
+
const SKIP_RECURSE = /* @__PURE__ */ new Set([
|
|
28
|
+
// The data keywords, matching `@amritk/helpers`' `DATA_KEYWORDS` —
|
|
29
|
+
// `example` is OpenAPI 3.0's singular spelling and belongs with `examples`.
|
|
30
|
+
"enum",
|
|
31
|
+
"const",
|
|
32
|
+
"examples",
|
|
33
|
+
"example",
|
|
34
|
+
"default",
|
|
35
|
+
// Plus this walker's own additions, which are not data but are equally not
|
|
36
|
+
// applied here.
|
|
37
|
+
"$ref",
|
|
38
|
+
"required",
|
|
39
|
+
"$defs",
|
|
40
|
+
"definitions"
|
|
41
|
+
]);
|
|
20
42
|
const filterAnswers = /* @__PURE__ */ new WeakMap();
|
|
21
43
|
const needsValidationFilter = (schema) => {
|
|
22
|
-
const walk = (node) => {
|
|
44
|
+
const walk = (node, inSchemaMap = false) => {
|
|
23
45
|
if (Array.isArray(node))
|
|
24
|
-
return node.some(walk);
|
|
46
|
+
return node.some((item) => walk(item, inSchemaMap));
|
|
25
47
|
if (node === null || typeof node !== "object")
|
|
26
48
|
return false;
|
|
27
49
|
const obj = node;
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
50
|
+
if (!inSchemaMap) {
|
|
51
|
+
for (const key of Object.keys(obj)) {
|
|
52
|
+
if (FILTER_KEYWORDS.has(key))
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
31
55
|
}
|
|
32
|
-
for (const
|
|
33
|
-
if (SKIP_RECURSE.has(key))
|
|
56
|
+
for (const key of Object.keys(obj)) {
|
|
57
|
+
if (!inSchemaMap && SKIP_RECURSE.has(key))
|
|
34
58
|
continue;
|
|
35
|
-
if (walk(
|
|
59
|
+
if (walk(obj[key], !inSchemaMap && SCHEMA_MAPS.has(key)))
|
|
36
60
|
return true;
|
|
37
61
|
}
|
|
38
62
|
return false;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@amritk/generate-examples",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.4",
|
|
4
4
|
"description": "Generate fast-check arbitraries and example values from JSON Schemas.",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -55,8 +55,8 @@
|
|
|
55
55
|
},
|
|
56
56
|
"dependencies": {
|
|
57
57
|
"json-schema-typed": "^8.0.1",
|
|
58
|
-
"@amritk/helpers": "^0.15.
|
|
59
|
-
"@amritk/runtime-validators": "^0.
|
|
58
|
+
"@amritk/helpers": "^0.15.4",
|
|
59
|
+
"@amritk/runtime-validators": "^0.11.0"
|
|
60
60
|
},
|
|
61
61
|
"devDependencies": {
|
|
62
62
|
"ajv": "^8.17.1"
|