Skip to content

Commit 7f8591d

Browse files
committed
fix: mixed() is the the base class
fixes #1156
1 parent 67de534 commit 7f8591d

File tree

4 files changed

+38
-16
lines changed

4 files changed

+38
-16
lines changed

docs/extending.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function parseDateFromFormats(formats, parseStrict) {
4040
yup.addMethod(yup.date, 'format', parseDateFromFormats);
4141
```
4242

43-
Note that `addMethod` isn't really magic, it mutates the prototype of the passed in schema.
43+
Note that `addMethod` isn't magic, it mutates the prototype of the passed in schema.
4444

4545
> Note: if you are using TypeScript you also need to adjust the class or interface
4646
> see the [typescript](./typescript) docs for details.
@@ -49,7 +49,7 @@ Note that `addMethod` isn't really magic, it mutates the prototype of the passed
4949

5050
If you're use case calls for creating an entirely new type. inheriting from
5151
and existing schema class may be best: Generally you should not inheriting from
52-
the abstract `Schema` unless you know what you are doing. The other types are fair game though.
52+
the abstract `BaseSchema` unless you know what you are doing. The other types are fair game though.
5353

5454
You should keep in mind some basic guidelines when extending schemas:
5555

docs/typescript.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,13 @@ declare module 'yup' {
118118
> Watch out!: If your method needs to adjust schema generics, you likely
119119
> need to also extend the Required*, and Defined* interfaces associated with
120120
> each basic type. Consult the core types for examples on how to do this
121+
122+
Be careful of the yup type hierarchy as it's a bit tricky. All schema (including `mixed`)
123+
extend the abstract `BaseSchema` class.
124+
125+
### Special note about MixedSchema and BaseSchema
126+
127+
As far as typescript is concerned, `mixed` schema inherit from `BaseSchema` like other schema; all other schema do **not** extend `MixedSchema`. **In actuality** Mixed is an alias for BaseSchema, meaning `addMethod(mixed)` will add a new method to all schema.
128+
129+
This means that type extensions to `mixed` should generally be put on `BaseSchema` if
130+
you want the method to be available to all sub classes.

src/mixed.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,11 @@ import { AnyObject, Maybe, Optionals } from './types';
44
import type { Defined } from './util/types';
55
import BaseSchema from './schema';
66

7-
export function create<TType = any>() {
8-
return new MixedSchema<TType | undefined>();
9-
}
10-
11-
export default class MixedSchema<
7+
declare class MixedSchema<
128
TType = any,
139
TContext = AnyObject,
1410
TOut = TType
15-
> extends BaseSchema<TType, TContext, TOut> {}
16-
17-
create.prototype = MixedSchema.prototype;
18-
19-
export default interface MixedSchema<
20-
TType = any,
21-
TContext = AnyObject,
22-
TOut = TType
23-
> {
11+
> extends BaseSchema<TType, TContext, TOut> {
2412
default<TNextDefault extends Maybe<TType>>(
2513
def: TNextDefault | (() => TNextDefault),
2614
): TNextDefault extends undefined
@@ -46,3 +34,13 @@ export default interface MixedSchema<
4634
nullable(isNullable?: true): MixedSchema<TType | null, TContext>;
4735
nullable(isNullable: false): MixedSchema<Exclude<TType, null>, TContext>;
4836
}
37+
38+
const Mixed: typeof MixedSchema = BaseSchema as any;
39+
40+
export default Mixed;
41+
42+
export function create<TType = any>() {
43+
return new Mixed<TType | undefined>();
44+
}
45+
// XXX: this is using the Base schema so that `addMethod(mixed)` works as a base class
46+
create.prototype = Mixed.prototype;

test/yup.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import {
1616
NumberSchema,
1717
BooleanSchema,
1818
DateSchema,
19+
mixed,
20+
MixedSchema,
1921
} from '../src';
2022

2123
describe('Yup', function () {
@@ -191,6 +193,18 @@ describe('Yup', function () {
191193
});
192194

193195
describe('addMethod', () => {
196+
it('extending mixed should make method accessible everywhere', () => {
197+
addMethod(mixed, 'foo', () => 'here');
198+
199+
expect(string().foo()).to.equal('here');
200+
});
201+
202+
it('extending Mixed should make method accessible everywhere', () => {
203+
addMethod(MixedSchema, 'foo', () => 'here');
204+
205+
expect(string().foo()).to.equal('here');
206+
});
207+
194208
test.each([
195209
['object', object],
196210
['array', array],

0 commit comments

Comments
 (0)