@amritk/helpers 0.23.0 → 0.23.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/README.md +1 -1
- package/dist/get-default-value.d.ts +3 -1
- package/dist/get-default-value.js +5 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -112,7 +112,7 @@ Most helpers have a colocated test file (`*.test.ts`) — read those for canonic
|
|
|
112
112
|
|
|
113
113
|
## Related packages
|
|
114
114
|
|
|
115
|
-
- [`@amritk/
|
|
115
|
+
- [`@amritk/validation`](../parsers) — primary consumer
|
|
116
116
|
- [`@amritk/mjst`](../cli) — the CLI surface
|
|
117
117
|
|
|
118
118
|
---
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import type { JSONSchema } from 'json-schema-typed/draft-2020-12';
|
|
2
2
|
/**
|
|
3
3
|
* Returns the default value for a JSON Schema property.
|
|
4
|
-
* Priority order: explicit default > first enum value > first example >
|
|
4
|
+
* Priority order: explicit default > const > first enum value > first example >
|
|
5
|
+
* union first schema > pattern-based > type-based. `default` and `examples` are
|
|
6
|
+
* taken only when they are instances of the declared type; see below.
|
|
5
7
|
* These defaults ensure that parsing never fails, even with missing data.
|
|
6
8
|
*/
|
|
7
9
|
export declare const getDefaultValue: (schema: JSONSchema) => string;
|
|
@@ -92,8 +92,11 @@ const getDefaultValue = (schema) => {
|
|
|
92
92
|
if (hasEnum(schema) && schema.enum.length > 0) {
|
|
93
93
|
return JSON.stringify(schema.enum[0]);
|
|
94
94
|
}
|
|
95
|
-
if (hasExamples(schema)
|
|
96
|
-
|
|
95
|
+
if (hasExamples(schema)) {
|
|
96
|
+
const usable = schema.examples.findIndex((example) => defaultMatchesType(example, schema));
|
|
97
|
+
if (usable !== -1) {
|
|
98
|
+
return JSON.stringify(schema.examples[usable]);
|
|
99
|
+
}
|
|
97
100
|
}
|
|
98
101
|
if (hasOneOf(schema) && schema.oneOf.length > 0) {
|
|
99
102
|
const firstSchema = schema.oneOf[0];
|