json-schema-library 10.1.2 → 10.2.0

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
@@ -539,6 +539,50 @@ console.log(data); // { valid: "stays", invalid: "removed" }
539
539
 
540
540
  </details>
541
541
 
542
+ <details><summary>Option: useTypeDefaults (default: true)</summary>
543
+
544
+ With `useTypeDefaults:true`, `getData` will return initial values for all primitives (non-objects/arrays) that do not have a default-property set:
545
+
546
+ ```ts
547
+ const data = compileSchema({
548
+ required: ["valid"],
549
+ properties: { valid: { type: "string" } }
550
+ }).getData(null, { useTypeDefaults: true });
551
+ console.log(data); // { valid: "" }
552
+ ```
553
+
554
+ Setting `useTypeDefaults:false` will _not_ remove data that is valid, but unspecified:
555
+
556
+ ```ts
557
+ const data = compileSchema({
558
+ required: ["valid"],
559
+ properties: { valid: { type: "string" } }
560
+ }).getData(null, { useTypeDefaults: false });
561
+ console.log(data); // {}
562
+ ```
563
+
564
+ _Note_ object and array-properties will still be added if required:
565
+
566
+ ```ts
567
+ const data = compileSchema({
568
+ required: ["valid"],
569
+ properties: { valid: { type: "object" } }
570
+ }).getData(null, { useTypeDefaults: true });
571
+ console.log(data); // { valid: {} }
572
+ ```
573
+
574
+ _Note_ enforced array-items will be `undefined` if required and `initialValues: false`
575
+
576
+ ```ts
577
+ const data = compileSchema({
578
+ required: ["valid"],
579
+ properties: { valid: { type: "array", items: { type: "string" }, minItems: 1 } }
580
+ }).getData(null, { useTypeDefaults: false });
581
+ console.log(data); // { valid: [undefined] }
582
+ ```
583
+
584
+ </details>
585
+
542
586
  ### getNode
543
587
 
544
588
  `getNode` returns the JSON Schema from data location specified by a JSON Pointer. In many cases the JSON Schema can be retrieved without passing any data, but in situations where the schema is dynamic (for example in _oneOf_, _dependencies_, etc.), input-data is required or `getNode` will return a _JsonError_ as is done when the JSON Pointer path is invalid.