export type Normalizer = (data: In) => Out; export type NormalizeOptions = { /** * - Materialize `default` for absent object properties, recursively */ useDefaults?: boolean | ((schemaNode: Record) => boolean); /** * - Strip unknown properties: `true` only where `additionalProperties: false`, `'all'` wherever an object shape is declared */ removeAdditional?: boolean | 'all'; /** * - Convert a value to the node's declared scalar `type` when it is convertible */ coerceTypes?: boolean | ((schemaNode: Record) => boolean); /** * - Trim leading/trailing whitespace from strings, before coercion */ trimStrings?: boolean | ((schemaNode: Record) => boolean); }; /** * Collect the `$anchor` declarations of one schema document: a map from * anchor name to the schema node that declares it. Exported because * `@jarenjs/emit` resolves the same references when it derives types, and two * walks with different scope rules would make a generated type disagree with * this normalizer — the one defect class that package must not have. * * The scope is the same-document scope the rest of this module uses: a * subtree that declares its own `$id` is an embedded resource with its own * anchor scope, so it is not descended. First declaration wins, which keeps * the map deterministic for a document that (invalidly) repeats a name. * @param {object|boolean} root - The root schema of the document * @returns {Map} anchor name -> schema node */ export declare function collectSameDocumentAnchors(root: object | boolean): Map; /** * Resolve a same-document `$ref` — `#`, `#/` followed by a JSON Pointer, or * `#name` for a plain `$anchor` — to the schema it addresses. URI fragment * escapes are decoded once, before JSON Pointer token escapes. Refs into other * documents are not followed: a normalizer compiles one schema, and reaching * a registered sibling would mean owning the whole resolution scope that * `compile` owns. Exported for `@jarenjs/emit`, which must resolve references * with exactly these rules when it derives the accepted/normalized variants. * @param {string} ref - The reference * @param {object|boolean} root - The root schema being compiled * @param {Map} [anchors] - The document's anchor map, from * {@link collectSameDocumentAnchors}; omit to skip anchor resolution * @returns {object|boolean|undefined} The addressed schema, or undefined */ export declare function resolveSameDocumentRef(ref: string, root: object | boolean, anchors?: Map): object | boolean | undefined; /** * Resolve a per-node normalization switch at COMPILE time. Exported because * `@jarenjs/emit` has to answer the same question when it derives the accepted * and normalized type variants: two implementations of this rule would drift, * and a type that disagrees with the normalizer is worse than no type. `true` turns the * behavior on everywhere, `false` nowhere, and a predicate decides per schema * node — which is how a consumer expresses "trim these 34 string fields, not * the other 185" without the option becoming a whole-schema blunt instrument. * Because it runs during compilation, a predicate costs nothing at runtime. * @param {boolean|((node: Record) => boolean)|undefined} option * @param {object} node - The schema node the switch applies to * @returns {boolean} */ export declare function resolveNormalizeSwitch(option: boolean | ((node: Record) => boolean) | undefined, node: object): boolean; /** * Compile a JSON Schema into a normalizer: a function that returns a * normalized copy of its input, leaving the input untouched. * * Validation is unaffected and unchanged - normalize first, then hand the * result to a compiled validator: * * ```javascript * const normalize = compileNormalizer(schema, { useDefaults: true, trimStrings: true }); * const validate = new JarenValidator({ collectErrors: true }).compile(schema); * const shaped = normalize(input); * const result = validate(shaped); * ``` * * **What is normalized.** `properties`, `patternProperties`, * `additionalProperties`, `items`/`prefixItems`/`additionalItems`, same-document * `$ref` (`#`, `#/pointer` and plain `#anchor` forms), and `allOf` (composed, * with stripping disabled inside it). A materialized root default passes * through the same normalization steps as an explicitly supplied value. * A property default runs through that property's schema and every matching * `patternProperties` schema, in the same order as a supplied member. * * **What is not, and why.** `anyOf`, `oneOf`, `if`/`then`/`else` and `not` * are not descended: which branch applies is only known after validating, * and normalizing under a branch can change which branch validates. Nothing * arbitrary runs either - there is no transform hook, because an arbitrary * transform is application code, not schema semantics, and belongs on the * caller's side of the boundary. * @template [In=unknown] * @template [Out=In] * @param {object|boolean} schema - The schema to compile * @param {NormalizeOptions} [options] - Which normalizations to apply * @returns {Normalizer} The compiled normalizer * @example * const normalize = compileNormalizer({ * type: 'object', * properties: { * name: { type: 'string' }, * port: { type: 'integer', default: 8080 }, * }, * additionalProperties: false, * }, { useDefaults: true, removeAdditional: true, coerceTypes: true, trimStrings: true }); * * const input = { name: ' jaren ', port: '9000', stray: 1 }; * normalize(input); // { name: 'jaren', port: 9000 } * input; // { name: ' jaren ', port: '9000', stray: 1 } - untouched */ export declare function compileNormalizer(schema: object | boolean, options?: NormalizeOptions): Normalizer;