Skip to content

Commit

Permalink
feat: pass options to default(options => value)
Browse files Browse the repository at this point in the history
closes #1984
  • Loading branch information
jquense committed May 25, 2023
1 parent 1fadba1 commit e5c5440
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 35 deletions.
17 changes: 11 additions & 6 deletions src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ import isSchema from './util/isSchema';
import printValue from './util/printValue';
import parseJson from './util/parseJson';
import { array as locale } from './locale';
import type { AnyObject, InternalOptions, Message, ISchema } from './types';
import type {
AnyObject,
InternalOptions,
Message,
ISchema,
DefaultThunk,
} from './types';
import type Reference from './Reference';
import type {
Defined,
Flags,
NotNull,
SetFlag,
Thunk,
Maybe,
Optionals,
ToggleDefault,
Expand Down Expand Up @@ -39,7 +44,7 @@ export function create<C extends Maybe<AnyObject> = AnyObject, T = any>(
}

interface ArraySchemaSpec<TIn, TContext> extends SchemaSpec<any> {
types?: ISchema<InnerType<TIn>, TContext>
types?: ISchema<InnerType<TIn>, TContext>;
}

export default class ArraySchema<
Expand Down Expand Up @@ -191,8 +196,8 @@ export default class ArraySchema<

next.spec = {
...next.spec,
types: schema as ISchema<InnerType<TIn>, TContext>
}
types: schema as ISchema<InnerType<TIn>, TContext>,
};

return next as any;
}
Expand Down Expand Up @@ -289,7 +294,7 @@ export default interface ArraySchema<
TFlags extends Flags = '',
> extends Schema<TIn, TContext, TDefault, TFlags> {
default<D extends Maybe<TIn>>(
def: Thunk<D>,
def: DefaultThunk<D, TContext>,
): ArraySchema<TIn, TContext, D, ToggleDefault<TFlags, D>>;

defined(msg?: Message): ArraySchema<Defined<TIn>, TContext, TDefault, TFlags>;
Expand Down
5 changes: 2 additions & 3 deletions src/boolean.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import Schema from './schema';
import type { AnyObject, Message } from './types';
import type { AnyObject, DefaultThunk, Message } from './types';
import type {
Defined,
Flags,
NotNull,
SetFlag,
Thunk,
ToggleDefault,
UnsetFlag,
Maybe,
Expand Down Expand Up @@ -79,7 +78,7 @@ export default class BooleanSchema<
}

override default<D extends Maybe<TType>>(
def: Thunk<D>,
def: DefaultThunk<D, TContext>,
): BooleanSchema<TType, TContext, D, ToggleDefault<TFlags, D>> {
return super.default(def);
}
Expand Down
5 changes: 2 additions & 3 deletions src/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
import isoParse from './util/isodate';
import { date as locale } from './locale';
import Ref from './Reference';
import type { AnyObject, Message } from './types';
import type { AnyObject, DefaultThunk, Message } from './types';
import type {
Defined,
Flags,
NotNull,
SetFlag,
Thunk,
Maybe,
ToggleDefault,
UnsetFlag,
Expand Down Expand Up @@ -120,7 +119,7 @@ export default interface DateSchema<
TFlags extends Flags = '',
> extends Schema<TType, TContext, TDefault, TFlags> {
default<D extends Maybe<TType>>(
def: Thunk<D>,
def: DefaultThunk<D, TContext>,
): DateSchema<TType, TContext, D, ToggleDefault<TFlags, D>>;

concat<TOther extends DateSchema<any, any>>(schema: TOther): TOther;
Expand Down
10 changes: 8 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ import Schema, {
SchemaFieldDescription,
SchemaDescription,
} from './schema';
import type { InferType, ISchema, Message, ValidateOptions } from './types';
import type {
InferType,
ISchema,
Message,
ValidateOptions,
DefaultThunk,
} from './types';

function addMethod<T extends ISchema<any>>(
schemaType: (...arg: any[]) => T,
Expand Down Expand Up @@ -73,6 +79,7 @@ export type {
SchemaDescription,
LocaleObject,
ValidateOptions,
DefaultThunk,
};

export {
Expand Down Expand Up @@ -128,7 +135,6 @@ export type {
Maybe,
Flags,
Optionals,
Thunk,
ToggleDefault,
Defined,
NotNull,
Expand Down
5 changes: 2 additions & 3 deletions src/mixed.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { AnyObject, Message } from './types';
import { AnyObject, DefaultThunk, Message } from './types';
import type {
Concat,
Defined,
Flags,
SetFlag,
Thunk,
Maybe,
ToggleDefault,
UnsetFlag,
Expand Down Expand Up @@ -49,7 +48,7 @@ export default interface MixedSchema<
TFlags extends Flags = '',
> extends Schema<TType, TContext, TDefault, TFlags> {
default<D extends Maybe<TType>>(
def: Thunk<D>,
def: DefaultThunk<D, TContext>,
): MixedSchema<TType, TContext, D, ToggleDefault<TFlags, D>>;

concat<IT, IC, ID, IF extends Flags>(
Expand Down
5 changes: 2 additions & 3 deletions src/number.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { number as locale } from './locale';
import isAbsent from './util/isAbsent';
import type { AnyObject, Message } from './types';
import type { AnyObject, DefaultThunk, Message } from './types';
import type Reference from './Reference';
import type {
Concat,
Defined,
Flags,
NotNull,
SetFlag,
Thunk,
Maybe,
ToggleDefault,
UnsetFlag,
Expand Down Expand Up @@ -167,7 +166,7 @@ export default interface NumberSchema<
TFlags extends Flags = '',
> extends Schema<TType, TContext, TDefault, TFlags> {
default<D extends Maybe<TType>>(
def: Thunk<D>,
def: DefaultThunk<D, TContext>,
): NumberSchema<TType, TContext, D, ToggleDefault<TFlags, D>>;

concat<UType extends Maybe<number>, UContext, UFlags extends Flags, UDefault>(
Expand Down
6 changes: 3 additions & 3 deletions src/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { Flags, Maybe, SetFlag, ToggleDefault, UnsetFlag } from './util/types';
import { object as locale } from './locale';
import sortFields from './util/sortFields';
import sortByKeyOrder from './util/sortByKeyOrder';
import { InternalOptions, ISchema, Message } from './types';
import type { Defined, Thunk, NotNull, _ } from './util/types';
import { DefaultThunk, InternalOptions, ISchema, Message } from './types';
import type { Defined, NotNull, _ } from './util/types';
import Reference from './Reference';
import Schema, { SchemaObjectDescription, SchemaSpec } from './schema';
import { ResolveOptions } from './Condition';
Expand Down Expand Up @@ -98,7 +98,7 @@ export default interface ObjectSchema<
TFlags extends Flags = '',
> extends Schema<MakeKeysOptional<TIn>, TContext, TDefault, TFlags> {
default<D extends Maybe<AnyObject>>(
def: Thunk<D>,
def: DefaultThunk<D, TContext>,
): ObjectSchema<TIn, TContext, D, ToggleDefault<TFlags, 'd'>>;

defined(
Expand Down
9 changes: 5 additions & 4 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ import {
ExtraParams,
ISchema,
NestedTestConfig,
DefaultThunk,
} from './types';

import ValidationError from './ValidationError';
import ReferenceSet from './util/ReferenceSet';
import Reference from './Reference';
import isAbsent from './util/isAbsent';
import type { Flags, Maybe, ResolveFlags, Thunk, _ } from './util/types';
import type { Flags, Maybe, ResolveFlags, _ } from './util/types';
import toArray from './util/toArray';
import cloneDeep from './util/cloneDeep';

Expand Down Expand Up @@ -607,15 +608,15 @@ export default abstract class Schema<
}
}

protected _getDefault(_options?: ResolveOptions<TContext>) {
protected _getDefault(options?: ResolveOptions<TContext>) {
let defaultValue = this.spec.default;

if (defaultValue == null) {
return defaultValue;
}

return typeof defaultValue === 'function'
? defaultValue.call(this)
? defaultValue.call(this, options)
: cloneDeep(defaultValue);
}

Expand All @@ -627,7 +628,7 @@ export default abstract class Schema<
return schema._getDefault(options);
}

default(def: Thunk<any>): any {
default(def: DefaultThunk<any>): any {
if (arguments.length === 0) {
return this._getDefault();
}
Expand Down
5 changes: 2 additions & 3 deletions src/string.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { MixedLocale, mixed as mixedLocale, string as locale } from './locale';
import isAbsent from './util/isAbsent';
import type Reference from './Reference';
import type { Message, AnyObject } from './types';
import type { Message, AnyObject, DefaultThunk } from './types';
import type {
Concat,
Defined,
Flags,
NotNull,
SetFlag,
Thunk,
ToggleDefault,
UnsetFlag,
Maybe,
Expand Down Expand Up @@ -256,7 +255,7 @@ export default interface StringSchema<
TFlags extends Flags = '',
> extends Schema<TType, TContext, TDefault, TFlags> {
default<D extends Maybe<TType>>(
def: Thunk<D>,
def: DefaultThunk<D, TContext>,
): StringSchema<TType, TContext, D, ToggleDefault<TFlags, D>>;

oneOf<U extends TType>(
Expand Down
11 changes: 8 additions & 3 deletions src/tuple.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
// @ts-ignore

import type { AnyObject, InternalOptions, ISchema, Message } from './types';
import type {
AnyObject,
DefaultThunk,
InternalOptions,
ISchema,
Message,
} from './types';
import type {
Defined,
Flags,
NotNull,
SetFlag,
Thunk,
ToggleDefault,
UnsetFlag,
Maybe,
Expand Down Expand Up @@ -35,7 +40,7 @@ export default interface TupleSchema<
TFlags extends Flags = '',
> extends Schema<TType, TContext, TDefault, TFlags> {
default<D extends Maybe<TType>>(
def: Thunk<D>,
def: DefaultThunk<D, TContext>,
): TupleSchema<TType, TContext, D, ToggleDefault<TFlags, D>>;

concat<TOther extends TupleSchema<any, any>>(schema: TOther): TOther;
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export interface ISchema<T, C = any, F extends Flags = any, D = any> {
resolve(options: ResolveOptions<C>): ISchema<T, C, F>;
}

export type DefaultThunk<T, C = any> = T | ((options?: ResolveOptions<C>) => T);

export type InferType<T extends ISchema<any, any>> = T['__outputType'];

export type TransformFunction<T extends AnySchema> = (
Expand Down
2 changes: 0 additions & 2 deletions src/util/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ export type Defined<T> = T extends undefined ? never : T;

export type NotNull<T> = T extends null ? never : T;

export type Thunk<T> = T | (() => T);

/* this seems to force TS to show the full type instead of all the wrapped generics */
export type _<T> = T extends {} ? { [k in keyof T]: T[k] } : T;

Expand Down

0 comments on commit e5c5440

Please sign in to comment.