From 79ff326f5e8b4851274655efee6e56e493d5fc3c Mon Sep 17 00:00:00 2001 From: alexander-akait <4567934+alexander-akait@users.noreply.github.com> Date: Sun, 1 Nov 2026 11:14:31 +0000 Subject: [PATCH 1/2] fix: type the validated value as unknown A validator checks a value of a type not yet known, so `validate`'s options and a precompiled check's value are `unknown` rather than `object | object[]` and `any`. Callers no longer cast to `object` before validating. --- .changeset/validate-unknown-options.md | 5 +++++ declarations/validate.d.ts | 8 ++++---- src/validate.js | 6 +++--- 3 files changed, 12 insertions(+), 7 deletions(-) create mode 100644 .changeset/validate-unknown-options.md diff --git a/.changeset/validate-unknown-options.md b/.changeset/validate-unknown-options.md new file mode 100644 index 0000000..4aaa145 --- /dev/null +++ b/.changeset/validate-unknown-options.md @@ -0,0 +1,5 @@ +--- +"schema-utils": patch +--- + +Type the value `validate` and a precompiled check take as `unknown`, since they check it. diff --git a/declarations/validate.d.ts b/declarations/validate.d.ts index 0122c49..dcf48cc 100644 --- a/declarations/validate.d.ts +++ b/declarations/validate.d.ts @@ -78,7 +78,7 @@ export type ErrorPathNode = { /** * A check precompiled from a schema with `precompile`, it only says whether the value is valid. */ -export type PrecompiledCheck = (value: EXPECTED_ANY) => boolean; +export type PrecompiledCheck = (value: unknown) => boolean; /** * Whether validation is skipped, shared by every `schema-utils` in the process. */ @@ -103,19 +103,19 @@ export function needValidate(): boolean; /** * A check precompiled from a schema with `precompile`, it only says whether the value is valid. * @callback PrecompiledCheck - * @param {EXPECTED_ANY} value value + * @param {unknown} value value * @returns {boolean} true when valid, otherwise false */ /** * @param {Schema} schema schema - * @param {object[] | object} options options + * @param {unknown} options options, an object or an array of objects * @param {ValidationErrorConfiguration=} configuration configuration * @param {PrecompiledCheck=} check precompiled check of the schema, so a valid value never compiles the schema * @returns {void} */ export function validate( schema: Schema, - options: object[] | object, + options: unknown, configuration?: ValidationErrorConfiguration | undefined, check?: PrecompiledCheck | undefined, ): void; diff --git a/src/validate.js b/src/validate.js index a18ccf7..4a221df 100644 --- a/src/validate.js +++ b/src/validate.js @@ -437,7 +437,7 @@ function filterErrors(errors) { /** * @param {Schema} schema schema - * @param {object[] | object} options options + * @param {unknown} options options, an object or an array of objects * @returns {SchemaUtilErrorObject[]} array of error objects */ function validateObject(schema, options) { @@ -453,13 +453,13 @@ function validateObject(schema, options) { /** * A check precompiled from a schema with `precompile`, it only says whether the value is valid. * @callback PrecompiledCheck - * @param {EXPECTED_ANY} value value + * @param {unknown} value value * @returns {boolean} true when valid, otherwise false */ /** * @param {Schema} schema schema - * @param {object[] | object} options options + * @param {unknown} options options, an object or an array of objects * @param {ValidationErrorConfiguration=} configuration configuration * @param {PrecompiledCheck=} check precompiled check of the schema, so a valid value never compiles the schema * @returns {void} From 7ea2e686ec094b7bdd54f4a0130606b5457b1d3f Mon Sep 17 00:00:00 2001 From: alexander-akait <4567934+alexander-akait@users.noreply.github.com> Date: Sun, 1 Nov 2026 11:48:40 +0000 Subject: [PATCH 2/2] fix: keep a precompiled check's value any A check a caller types by its own options, `(value: MyOptions) => boolean`, is not assignable to `(value: unknown) => boolean`, so keep the value `any` there. Only `validate`'s options become `unknown`, which accepts every value it did. --- .changeset/validate-unknown-options.md | 2 +- declarations/validate.d.ts | 4 ++-- src/validate.js | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.changeset/validate-unknown-options.md b/.changeset/validate-unknown-options.md index 4aaa145..a57325f 100644 --- a/.changeset/validate-unknown-options.md +++ b/.changeset/validate-unknown-options.md @@ -2,4 +2,4 @@ "schema-utils": patch --- -Type the value `validate` and a precompiled check take as `unknown`, since they check it. +Type the `validate` options parameter as `unknown`. diff --git a/declarations/validate.d.ts b/declarations/validate.d.ts index dcf48cc..43c9dad 100644 --- a/declarations/validate.d.ts +++ b/declarations/validate.d.ts @@ -78,7 +78,7 @@ export type ErrorPathNode = { /** * A check precompiled from a schema with `precompile`, it only says whether the value is valid. */ -export type PrecompiledCheck = (value: unknown) => boolean; +export type PrecompiledCheck = (value: EXPECTED_ANY) => boolean; /** * Whether validation is skipped, shared by every `schema-utils` in the process. */ @@ -103,7 +103,7 @@ export function needValidate(): boolean; /** * A check precompiled from a schema with `precompile`, it only says whether the value is valid. * @callback PrecompiledCheck - * @param {unknown} value value + * @param {EXPECTED_ANY} value value * @returns {boolean} true when valid, otherwise false */ /** diff --git a/src/validate.js b/src/validate.js index 4a221df..ed5ad17 100644 --- a/src/validate.js +++ b/src/validate.js @@ -453,7 +453,7 @@ function validateObject(schema, options) { /** * A check precompiled from a schema with `precompile`, it only says whether the value is valid. * @callback PrecompiledCheck - * @param {unknown} value value + * @param {EXPECTED_ANY} value value * @returns {boolean} true when valid, otherwise false */