@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 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/parsers`](../parsers) — primary consumer
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 > union first schema > pattern-based > type-based.
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) && schema.examples.length > 0) {
96
- return JSON.stringify(schema.examples[0]);
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];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amritk/helpers",
3
- "version": "0.23.0",
3
+ "version": "0.23.1",
4
4
  "description": "Shared utilities for the mjst code generation ecosystem.",
5
5
  "type": "module",
6
6
  "sideEffects": false,