@schema-ts/core 0.1.0 → 0.1.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/dist/index.d.cts CHANGED
@@ -219,15 +219,6 @@ declare class SchemaRuntime {
219
219
  * const runtime = new SchemaRuntime(validator, schema, { name: "Alice" });
220
220
  */
221
221
  constructor(validator: Validator, schema: Schema | unknown, value: unknown);
222
- /**
223
- * Collect all dependencies for a node's schema.
224
- *
225
- * Key insight: We extract paths from condition keywords (if, oneOf, anyOf)
226
- * using extractReferencedPaths, but for then/else/allOf keywords, we recursively
227
- * call collectDependencies to ensure proper dependency isolation between
228
- * parent and child nodes.
229
- */
230
- private collectDependencies;
231
222
  /**
232
223
  * Register a node as dependent on a path
233
224
  */
@@ -350,6 +341,26 @@ declare class SchemaRuntime {
350
341
  */
351
342
  findNode(path: string): FieldNode | undefined;
352
343
  private findNearestExistingNode;
344
+ /**
345
+ * Update node dependencies when schema changes.
346
+ * Unregisters old dependencies and registers new ones.
347
+ */
348
+ private updateNodeDependencies;
349
+ /**
350
+ * Apply default values when effective schema changes.
351
+ * This handles cases like if-then-else where new properties with defaults
352
+ * may appear when conditions change.
353
+ *
354
+ * @param instanceLocation - The path to the node
355
+ * @param newSchema - The new effective schema
356
+ * @param type - The schema type
357
+ */
358
+ private applySchemaDefaults;
359
+ /**
360
+ * Build children for object and array nodes.
361
+ * Reuses existing child nodes where possible.
362
+ */
363
+ private buildNodeChildren;
353
364
  /**
354
365
  * Build/update a FieldNode in place.
355
366
  * Updates the node's schema, type, error, and children based on the current value.
@@ -409,4 +420,59 @@ interface NormalizerOptions {
409
420
  */
410
421
  declare function normalizeSchema(schema: unknown, options?: NormalizerOptions): Schema;
411
422
 
412
- export { DRAFT_URIS, type ErrorFormatter, type ErrorMessage, type FieldNode, MESSAGES, type NormalizerOptions, type Output, type Schema, type SchemaChangeEvent, type SchemaDraft, SchemaRuntime, type SchemaType, StringFormatValidator, type StringFormatValidatorInterface, Validator, type ValidatorConfig, type ValidatorOptions, deepEqual, defaultErrorFormatter, detectSchemaDraft, detectSchemaType, get, getJsonPointer, jsonPointerEscape, jsonPointerJoin, jsonPointerUnescape, matchSchemaType, normalizeSchema, parseJsonPointer, removeJsonPointer, resolveAbsolutePath, safeRegexTest, setJsonPointer, stringFormatValidator, validateSchema };
423
+ /**
424
+ * Collect all dependencies for a node's schema.
425
+ *
426
+ * Key insight: We extract paths from condition keywords (if, oneOf, anyOf)
427
+ * using extractReferencedPaths, but for then/else/allOf keywords, we recursively
428
+ * call collectDependencies to ensure proper dependency isolation between
429
+ * parent and child nodes.
430
+ */
431
+ declare function collectDependencies(schema: Schema, instanceLocation: string): Set<string>;
432
+ /**
433
+ * Extract referenced paths from a conditional schema (if, oneOf, anyOf, etc.)
434
+ * Returns paths relative to the current node
435
+ *
436
+ * @param conditionSchema - The schema to extract paths from
437
+ * @param basePath - Base path for relative paths
438
+ * @param depth - Current recursion depth (internal use)
439
+ */
440
+ declare function extractReferencedPaths(conditionSchema: Schema, basePath?: string, depth?: number): string[];
441
+
442
+ /**
443
+ * Generate a default value for a schema based on its type and constraints.
444
+ *
445
+ * Priority order for determining the default value:
446
+ * 1. const - if defined, returns the const value
447
+ * 2. default - if defined, returns the default value
448
+ * 3. Type-based defaults:
449
+ * - string: ""
450
+ * - number/integer: 0
451
+ * - boolean: false
452
+ * - null: null
453
+ * - object: {} with required properties recursively initialized
454
+ * - array: []
455
+ *
456
+ * For objects, only required properties are initialized with their default values.
457
+ * If no type is specified but properties exist, the schema is treated as an object.
458
+ *
459
+ * If a value is provided, it will be used as the base and missing defaults will be filled in.
460
+ *
461
+ * @param schema - The JSON Schema to generate a default value for
462
+ * @param value - Optional existing value to fill defaults into
463
+ * @returns The generated default value, or undefined if type cannot be determined
464
+ *
465
+ * @example
466
+ * getDefaultValue({ type: "string" }) // returns ""
467
+ * getDefaultValue({ type: "number", default: 42 }) // returns 42
468
+ * getDefaultValue({ const: "fixed" }) // returns "fixed"
469
+ * getDefaultValue({
470
+ * type: "object",
471
+ * properties: { name: { type: "string" } },
472
+ * required: ["name"]
473
+ * }) // returns { name: "" }
474
+ * getDefaultValue({ type: "object", properties: { a: { default: 1 } } }, {}) // returns { a: 1 }
475
+ */
476
+ declare function getDefaultValue(schema: Schema, value?: unknown): unknown;
477
+
478
+ export { DRAFT_URIS, type ErrorFormatter, type ErrorMessage, type FieldNode, MESSAGES, type NormalizerOptions, type Output, type Schema, type SchemaChangeEvent, type SchemaDraft, SchemaRuntime, type SchemaType, StringFormatValidator, type StringFormatValidatorInterface, Validator, type ValidatorConfig, type ValidatorOptions, collectDependencies, deepEqual, defaultErrorFormatter, detectSchemaDraft, detectSchemaType, extractReferencedPaths, get, getDefaultValue, getJsonPointer, jsonPointerEscape, jsonPointerJoin, jsonPointerUnescape, matchSchemaType, normalizeSchema, parseJsonPointer, removeJsonPointer, resolveAbsolutePath, safeRegexTest, setJsonPointer, stringFormatValidator, validateSchema };
package/dist/index.d.ts CHANGED
@@ -219,15 +219,6 @@ declare class SchemaRuntime {
219
219
  * const runtime = new SchemaRuntime(validator, schema, { name: "Alice" });
220
220
  */
221
221
  constructor(validator: Validator, schema: Schema | unknown, value: unknown);
222
- /**
223
- * Collect all dependencies for a node's schema.
224
- *
225
- * Key insight: We extract paths from condition keywords (if, oneOf, anyOf)
226
- * using extractReferencedPaths, but for then/else/allOf keywords, we recursively
227
- * call collectDependencies to ensure proper dependency isolation between
228
- * parent and child nodes.
229
- */
230
- private collectDependencies;
231
222
  /**
232
223
  * Register a node as dependent on a path
233
224
  */
@@ -350,6 +341,26 @@ declare class SchemaRuntime {
350
341
  */
351
342
  findNode(path: string): FieldNode | undefined;
352
343
  private findNearestExistingNode;
344
+ /**
345
+ * Update node dependencies when schema changes.
346
+ * Unregisters old dependencies and registers new ones.
347
+ */
348
+ private updateNodeDependencies;
349
+ /**
350
+ * Apply default values when effective schema changes.
351
+ * This handles cases like if-then-else where new properties with defaults
352
+ * may appear when conditions change.
353
+ *
354
+ * @param instanceLocation - The path to the node
355
+ * @param newSchema - The new effective schema
356
+ * @param type - The schema type
357
+ */
358
+ private applySchemaDefaults;
359
+ /**
360
+ * Build children for object and array nodes.
361
+ * Reuses existing child nodes where possible.
362
+ */
363
+ private buildNodeChildren;
353
364
  /**
354
365
  * Build/update a FieldNode in place.
355
366
  * Updates the node's schema, type, error, and children based on the current value.
@@ -409,4 +420,59 @@ interface NormalizerOptions {
409
420
  */
410
421
  declare function normalizeSchema(schema: unknown, options?: NormalizerOptions): Schema;
411
422
 
412
- export { DRAFT_URIS, type ErrorFormatter, type ErrorMessage, type FieldNode, MESSAGES, type NormalizerOptions, type Output, type Schema, type SchemaChangeEvent, type SchemaDraft, SchemaRuntime, type SchemaType, StringFormatValidator, type StringFormatValidatorInterface, Validator, type ValidatorConfig, type ValidatorOptions, deepEqual, defaultErrorFormatter, detectSchemaDraft, detectSchemaType, get, getJsonPointer, jsonPointerEscape, jsonPointerJoin, jsonPointerUnescape, matchSchemaType, normalizeSchema, parseJsonPointer, removeJsonPointer, resolveAbsolutePath, safeRegexTest, setJsonPointer, stringFormatValidator, validateSchema };
423
+ /**
424
+ * Collect all dependencies for a node's schema.
425
+ *
426
+ * Key insight: We extract paths from condition keywords (if, oneOf, anyOf)
427
+ * using extractReferencedPaths, but for then/else/allOf keywords, we recursively
428
+ * call collectDependencies to ensure proper dependency isolation between
429
+ * parent and child nodes.
430
+ */
431
+ declare function collectDependencies(schema: Schema, instanceLocation: string): Set<string>;
432
+ /**
433
+ * Extract referenced paths from a conditional schema (if, oneOf, anyOf, etc.)
434
+ * Returns paths relative to the current node
435
+ *
436
+ * @param conditionSchema - The schema to extract paths from
437
+ * @param basePath - Base path for relative paths
438
+ * @param depth - Current recursion depth (internal use)
439
+ */
440
+ declare function extractReferencedPaths(conditionSchema: Schema, basePath?: string, depth?: number): string[];
441
+
442
+ /**
443
+ * Generate a default value for a schema based on its type and constraints.
444
+ *
445
+ * Priority order for determining the default value:
446
+ * 1. const - if defined, returns the const value
447
+ * 2. default - if defined, returns the default value
448
+ * 3. Type-based defaults:
449
+ * - string: ""
450
+ * - number/integer: 0
451
+ * - boolean: false
452
+ * - null: null
453
+ * - object: {} with required properties recursively initialized
454
+ * - array: []
455
+ *
456
+ * For objects, only required properties are initialized with their default values.
457
+ * If no type is specified but properties exist, the schema is treated as an object.
458
+ *
459
+ * If a value is provided, it will be used as the base and missing defaults will be filled in.
460
+ *
461
+ * @param schema - The JSON Schema to generate a default value for
462
+ * @param value - Optional existing value to fill defaults into
463
+ * @returns The generated default value, or undefined if type cannot be determined
464
+ *
465
+ * @example
466
+ * getDefaultValue({ type: "string" }) // returns ""
467
+ * getDefaultValue({ type: "number", default: 42 }) // returns 42
468
+ * getDefaultValue({ const: "fixed" }) // returns "fixed"
469
+ * getDefaultValue({
470
+ * type: "object",
471
+ * properties: { name: { type: "string" } },
472
+ * required: ["name"]
473
+ * }) // returns { name: "" }
474
+ * getDefaultValue({ type: "object", properties: { a: { default: 1 } } }, {}) // returns { a: 1 }
475
+ */
476
+ declare function getDefaultValue(schema: Schema, value?: unknown): unknown;
477
+
478
+ export { DRAFT_URIS, type ErrorFormatter, type ErrorMessage, type FieldNode, MESSAGES, type NormalizerOptions, type Output, type Schema, type SchemaChangeEvent, type SchemaDraft, SchemaRuntime, type SchemaType, StringFormatValidator, type StringFormatValidatorInterface, Validator, type ValidatorConfig, type ValidatorOptions, collectDependencies, deepEqual, defaultErrorFormatter, detectSchemaDraft, detectSchemaType, extractReferencedPaths, get, getDefaultValue, getJsonPointer, jsonPointerEscape, jsonPointerJoin, jsonPointerUnescape, matchSchemaType, normalizeSchema, parseJsonPointer, removeJsonPointer, resolveAbsolutePath, safeRegexTest, setJsonPointer, stringFormatValidator, validateSchema };