Repository: jquense/yup
Pull request #2119 (2023-10-07 10:18:52)

fix: add optional message to nonNullable schema methods
Fixes https://github.com/jquense/yup/issues/2069

Adds an optional `msg?: Message` argument to `nonNullable` method on every schema object. The argument is already supported by the [base Schema class](https://github.com/jquense/yup/blob/master/src/schema.ts#L713), and we only need to pass it properly with TypeScript.

Also updates `nullable` and `nonNullable` signatures in `README.md`.

ranquild: @jquense could you take a look, please? 

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 865cee1..01724de 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,14 @@
+## [1.3.3](https://github.com/jquense/yup/compare/v1.3.2...v1.3.3) (2023-12-14)
+
+
+### Bug Fixes
+
+* **addMethod:** allow Schema without making TypeScript upset ([f921fe6](https://github.com/jquense/yup/commit/f921fe69a2d6ecc6e7d0101d2bd81148dfe83e64))
+
+
+
+
+
 ## [1.3.2](https://github.com/jquense/yup/compare/v1.3.1...v1.3.2) (2023-09-29)
 
 
diff --git a/README.md b/README.md
index a3dcd72..2bbb32a 100644
--- a/README.md
+++ b/README.md
@@ -111,8 +111,8 @@ const parsedUser = await userSchema.validate(
     - [`Schema.withMutation(builder: (current: Schema) => void): void`](#schemawithmutationbuilder-current-schema--void-void)
     - [`Schema.default(value: any): Schema`](#schemadefaultvalue-any-schema)
     - [`Schema.getDefault(options?: object): Any`](#schemagetdefaultoptions-object-any)
-    - [`Schema.nullable(): Schema`](#schemanullable-schema)
-    - [`Schema.nonNullable(): Schema`](#schemanonnullable-schema)
+    - [`Schema.nullable(message?: string | function): Schema`](#schemanullable-schema)
+    - [`Schema.nonNullable(message?: string | function): Schema`](#schemanonnullable-schema)
     - [`Schema.defined(): Schema`](#schemadefined-schema)
     - [`Schema.optional(): Schema`](#schemaoptional-schema)
     - [`Schema.required(message?: string | function): Schema`](#schemarequiredmessage-string--function-schema)
@@ -946,7 +946,7 @@ yup.date.default(() => new Date()); // also helpful for defaults that change ove
 
 Retrieve a previously set default value. `getDefault` will resolve any conditions that may alter the default. Optionally pass `options` with `context` (for more info on `context` see `Schema.validate`).
 
-#### `Schema.nullable(): Schema`
+#### `Schema.nullable(message?: string | function): Schema`
 
 Indicates that `null` is a valid value for the schema. Without `nullable()`
 `null` is treated as a different type and will fail `Schema.isType()` checks.
@@ -959,7 +959,7 @@ schema.cast(null); // null
 InferType<typeof schema>; // number | null
 ```
 
-#### `Schema.nonNullable(): Schema`
+#### `Schema.nonNullable(message?: string | function): Schema`
 
 The opposite of `nullable`, removes `null` from valid type values for the schema.
 **Schema are non nullable by default**.
diff --git a/package.json b/package.json
index 73a8259..dd1d8ea 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "yup",
-  "version": "1.3.2",
+  "version": "1.3.3",
   "description": "Dead simple Object schema validation",
   "main": "lib/index.js",
   "module": "lib/index.esm.js",
diff --git a/src/array.ts b/src/array.ts
index 3d73101..cd94b9d 100644
--- a/src/array.ts
+++ b/src/array.ts
@@ -307,7 +307,9 @@ export default interface ArraySchema<
   notRequired(): ArraySchema<Maybe<TIn>, TContext, TDefault, TFlags>;
 
   nullable(msg?: Message): ArraySchema<TIn | null, TContext, TDefault, TFlags>;
-  nonNullable(): ArraySchema<NotNull<TIn>, TContext, TDefault, TFlags>;
+  nonNullable(
+    msg?: Message,
+  ): ArraySchema<NotNull<TIn>, TContext, TDefault, TFlags>;
 
   strip(
     enabled: false,
diff --git a/src/date.ts b/src/date.ts
index 34f9169..8d23601 100644
--- a/src/date.ts
+++ b/src/date.ts
@@ -134,7 +134,9 @@ export default interface DateSchema<
   notRequired(): DateSchema<Maybe<TType>, TContext, TDefault, TFlags>;
 
   nullable(msg?: Message): DateSchema<TType | null, TContext, TDefault, TFlags>;
-  nonNullable(): DateSchema<NotNull<TType>, TContext, TDefault, TFlags>;
+  nonNullable(
+    msg?: Message,
+  ): DateSchema<NotNull<TType>, TContext, TDefault, TFlags>;
 
   strip(
     enabled: false,
diff --git a/src/index.ts b/src/index.ts
index 00e7930..2bdce99 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -43,7 +43,7 @@ function addMethod<T extends ISchema<any>>(
   name: string,
   fn: (this: T, ...args: any[]) => T,
 ): void;
-function addMethod<T extends new (...args: any) => ISchema<any>>(
+function addMethod<T extends abstract new (...args: any) => ISchema<any>>(
   schemaType: T,
   name: string,
   fn: (this: InstanceType<T>, ...args: any[]) => InstanceType<T>,
diff --git a/src/mixed.ts b/src/mixed.ts
index 63b077e..7fba52e 100644
--- a/src/mixed.ts
+++ b/src/mixed.ts
@@ -73,7 +73,9 @@ export default interface MixedSchema<
     msg?: Message,
   ): MixedSchema<TType | null, TContext, TDefault, TFlags>;
 
-  nonNullable(): MixedSchema<Exclude<TType, null>, TContext, TDefault, TFlags>;
+  nonNullable(
+    msg?: Message,
+  ): MixedSchema<Exclude<TType, null>, TContext, TDefault, TFlags>;
 
   strip(
     enabled: false,
diff --git a/src/number.ts b/src/number.ts
index de97ea6..91c0932 100644
--- a/src/number.ts
+++ b/src/number.ts
@@ -192,7 +192,9 @@ export default interface NumberSchema<
   nullable(
     msg?: Message,
   ): NumberSchema<TType | null, TContext, TDefault, TFlags>;
-  nonNullable(): NumberSchema<NotNull<TType>, TContext, TDefault, TFlags>;
+  nonNullable(
+    msg?: Message,
+  ): NumberSchema<NotNull<TType>, TContext, TDefault, TFlags>;
 
   strip(
     enabled: false,
diff --git a/src/object.ts b/src/object.ts
index 9ce4a87..9089897 100644
--- a/src/object.ts
+++ b/src/object.ts
@@ -112,7 +112,9 @@ export default interface ObjectSchema<
   notRequired(): ObjectSchema<Maybe<TIn>, TContext, TDefault, TFlags>;
 
   nullable(msg?: Message): ObjectSchema<TIn | null, TContext, TDefault, TFlags>;
-  nonNullable(): ObjectSchema<NotNull<TIn>, TContext, TDefault, TFlags>;
+  nonNullable(
+    msg?: Message,
+  ): ObjectSchema<NotNull<TIn>, TContext, TDefault, TFlags>;
 
   strip(
     enabled: false,
diff --git a/src/string.ts b/src/string.ts
index a92c3d8..bfcade2 100644
--- a/src/string.ts
+++ b/src/string.ts
@@ -288,9 +288,11 @@ export default interface StringSchema<
   notRequired(): StringSchema<Maybe<TType>, TContext, TDefault, TFlags>;
 
   nullable(
-    msg?: Message<any>,
+    msg?: Message,
   ): StringSchema<TType | null, TContext, TDefault, TFlags>;
-  nonNullable(): StringSchema<NotNull<TType>, TContext, TDefault, TFlags>;
+  nonNullable(
+    msg?: Message
+  ): StringSchema<NotNull<TType>, TContext, TDefault, TFlags>;
 
   strip(
     enabled: false,
diff --git a/src/tuple.ts b/src/tuple.ts
index f4bf644..c68a120 100644
--- a/src/tuple.ts
+++ b/src/tuple.ts
@@ -58,7 +58,9 @@ export default interface TupleSchema<
   nullable(
     msg?: Message,
   ): TupleSchema<TType | null, TContext, TDefault, TFlags>;
-  nonNullable(): TupleSchema<NotNull<TType>, TContext, TDefault, TFlags>;
+  nonNullable(
+    msg?: Message
+  ): TupleSchema<NotNull<TType>, TContext, TDefault, TFlags>;
 
   strip(
     enabled: false,


Test patch:
diff --git a/test/types/types.ts b/test/types/types.ts
index 8a27b49..30960ac 100644
--- a/test/types/types.ts
+++ b/test/types/types.ts
@@ -10,6 +10,7 @@ import {
   bool,
   reach,
   addMethod,
+  Schema,
 } from '../../src';
 import { create as tuple } from '../../src/tuple';
 import { create as lazy } from '../../src/Lazy';
@@ -1079,6 +1080,10 @@ reach: {
 }
 
 addMethod: {
+  addMethod(Schema, 'foo', function () {
+    return this.clone();
+  });
+
   addMethod(string, 'foo', function () {
     return this.clone();
   });