Skip to content

Commit

Permalink
Refactor internal arg and type names in schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-hiller committed Oct 29, 2023
1 parent 0944448 commit 30e460a
Show file tree
Hide file tree
Showing 14 changed files with 181 additions and 179 deletions.
10 changes: 5 additions & 5 deletions library/src/methods/fallback/fallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type SchemaWithFallback<
* Returns a fallback value when validating the passed schema failed.
*
* @param schema The schema to catch.
* @param value The fallback value.
* @param fallback The fallback value.
*
* @returns The passed schema.
*/
Expand All @@ -23,7 +23,7 @@ export function fallback<
TFallback extends Output<TSchema>
>(
schema: TSchema,
value: TFallback | ((info?: FallbackInfo) => TFallback)
fallback: TFallback | ((info?: FallbackInfo) => TFallback)
): SchemaWithFallback<TSchema, TFallback> {
return {
...schema,
Expand All @@ -32,9 +32,9 @@ export function fallback<
* Returns the fallback value.
*/
getFallback(info) {
return typeof value === 'function'
? (value as (info?: FallbackInfo) => TFallback)(info)
: (value as TFallback);
return typeof fallback === 'function'
? (fallback as (info?: FallbackInfo) => TFallback)(info)
: (fallback as TFallback);
},

/**
Expand Down
12 changes: 7 additions & 5 deletions library/src/methods/fallback/fallbackAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type SchemaWithFallbackAsync<
* Returns a fallback value when validating the passed schema failed.
*
* @param schema The schema to catch.
* @param value The fallback value.
* @param fallback The fallback value.
*
* @returns The passed schema.
*/
Expand All @@ -23,7 +23,9 @@ export function fallbackAsync<
TFallback extends Output<TSchema>
>(
schema: TSchema,
value: TFallback | ((info?: FallbackInfo) => TFallback | Promise<TFallback>)
fallback:
| TFallback
| ((info?: FallbackInfo) => TFallback | Promise<TFallback>)
): SchemaWithFallbackAsync<TSchema, TFallback> {
return {
...schema,
Expand All @@ -32,11 +34,11 @@ export function fallbackAsync<
* Returns the default value.
*/
async getFallback(info) {
return typeof value === 'function'
return typeof fallback === 'function'
? await (
value as (info?: FallbackInfo) => TFallback | Promise<TFallback>
fallback as (info?: FallbackInfo) => TFallback | Promise<TFallback>
)(info)
: (value as TFallback);
: (fallback as TFallback);
},

/**
Expand Down
20 changes: 10 additions & 10 deletions library/src/schemas/nonNullable/nonNullable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@ export type NonNullable<T> = T extends null ? never : T;
* Non nullable schema type.
*/
export type NonNullableSchema<
TSchema extends BaseSchema,
TOutput = NonNullable<Output<TSchema>>
> = BaseSchema<NonNullable<Input<TSchema>>, TOutput> & {
TWrapped extends BaseSchema,
TOutput = NonNullable<Output<TWrapped>>
> = BaseSchema<NonNullable<Input<TWrapped>>, TOutput> & {
schema: 'non_nullable';
wrapped: TSchema;
wrapped: TWrapped;
};

/**
* Creates a non nullable schema.
*
* @param schema The wrapped schema.
* @param wrapped The wrapped schema.
* @param error The error message.
*
* @returns A non nullable schema.
*/
export function nonNullable<TSchema extends BaseSchema>(
schema: TSchema,
export function nonNullable<TWrapped extends BaseSchema>(
wrapped: TWrapped,
error?: ErrorMessage
): NonNullableSchema<TSchema> {
): NonNullableSchema<TWrapped> {
return {
/**
* The schema type.
Expand All @@ -38,7 +38,7 @@ export function nonNullable<TSchema extends BaseSchema>(
/**
* The wrapped schema.
*/
wrapped: schema,
wrapped,

/**
* Whether it's async.
Expand Down Expand Up @@ -66,7 +66,7 @@ export function nonNullable<TSchema extends BaseSchema>(
}

// Return result of wrapped schema
return schema._parse(input, info);
return wrapped._parse(input, info);
},
};
}
20 changes: 10 additions & 10 deletions library/src/schemas/nonNullable/nonNullableAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@ import type { NonNullable } from './nonNullable.ts';
* Non nullable schema async type.
*/
export type NonNullableSchemaAsync<
TSchema extends BaseSchema | BaseSchemaAsync,
TOutput = NonNullable<Output<TSchema>>
> = BaseSchemaAsync<NonNullable<Input<TSchema>>, TOutput> & {
TWrapped extends BaseSchema | BaseSchemaAsync,
TOutput = NonNullable<Output<TWrapped>>
> = BaseSchemaAsync<NonNullable<Input<TWrapped>>, TOutput> & {
schema: 'non_nullable';
wrapped: TSchema;
wrapped: TWrapped;
};

/**
* Creates an async non nullable schema.
*
* @param schema The wrapped schema.
* @param wrapped The wrapped schema.
* @param error The error message.
*
* @returns An async non nullable schema.
*/
export function nonNullableAsync<TSchema extends BaseSchema | BaseSchemaAsync>(
schema: TSchema,
export function nonNullableAsync<TWrapped extends BaseSchema | BaseSchemaAsync>(
wrapped: TWrapped,
error?: ErrorMessage
): NonNullableSchemaAsync<TSchema> {
): NonNullableSchemaAsync<TWrapped> {
return {
/**
* The schema type.
Expand All @@ -40,7 +40,7 @@ export function nonNullableAsync<TSchema extends BaseSchema | BaseSchemaAsync>(
/**
* The wrapped schema.
*/
wrapped: schema,
wrapped,

/**
* Whether it's async.
Expand Down Expand Up @@ -68,7 +68,7 @@ export function nonNullableAsync<TSchema extends BaseSchema | BaseSchemaAsync>(
}

// Return result of wrapped schema
return schema._parse(input, info);
return wrapped._parse(input, info);
},
};
}
20 changes: 10 additions & 10 deletions library/src/schemas/nonNullish/nonNullish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@ export type NonNullish<T> = T extends null | undefined ? never : T;
* Non nullish schema type.
*/
export type NonNullishSchema<
TSchema extends BaseSchema,
TOutput = NonNullish<Output<TSchema>>
> = BaseSchema<NonNullish<Input<TSchema>>, TOutput> & {
TWrapped extends BaseSchema,
TOutput = NonNullish<Output<TWrapped>>
> = BaseSchema<NonNullish<Input<TWrapped>>, TOutput> & {
schema: 'non_nullish';
wrapped: TSchema;
wrapped: TWrapped;
};

/**
* Creates a non nullish schema.
*
* @param schema The wrapped schema.
* @param wrapped The wrapped schema.
* @param error The error message.
*
* @returns A non nullish schema.
*/
export function nonNullish<TSchema extends BaseSchema>(
schema: TSchema,
export function nonNullish<TWrapped extends BaseSchema>(
wrapped: TWrapped,
error?: ErrorMessage
): NonNullishSchema<TSchema> {
): NonNullishSchema<TWrapped> {
return {
/**
* The schema type.
Expand All @@ -38,7 +38,7 @@ export function nonNullish<TSchema extends BaseSchema>(
/**
* The wrapped schema.
*/
wrapped: schema,
wrapped,

/**
* Whether it's async.
Expand Down Expand Up @@ -66,7 +66,7 @@ export function nonNullish<TSchema extends BaseSchema>(
}

// Return result of wrapped schema
return schema._parse(input, info);
return wrapped._parse(input, info);
},
};
}
20 changes: 10 additions & 10 deletions library/src/schemas/nonNullish/nonNullishAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@ import type { NonNullish } from './nonNullish.ts';
* Non nullish schema async type.
*/
export type NonNullishSchemaAsync<
TSchema extends BaseSchema | BaseSchemaAsync,
TOutput = NonNullish<Output<TSchema>>
> = BaseSchemaAsync<NonNullish<Input<TSchema>>, TOutput> & {
TWrapped extends BaseSchema | BaseSchemaAsync,
TOutput = NonNullish<Output<TWrapped>>
> = BaseSchemaAsync<NonNullish<Input<TWrapped>>, TOutput> & {
schema: 'non_nullish';
wrapped: TSchema;
wrapped: TWrapped;
};

/**
* Creates an async non nullish schema.
*
* @param schema The wrapped schema.
* @param wrapped The wrapped schema.
* @param error The error message.
*
* @returns An async non nullish schema.
*/
export function nonNullishAsync<TSchema extends BaseSchema | BaseSchemaAsync>(
schema: TSchema,
export function nonNullishAsync<TWrapped extends BaseSchema | BaseSchemaAsync>(
wrapped: TWrapped,
error?: ErrorMessage
): NonNullishSchemaAsync<TSchema> {
): NonNullishSchemaAsync<TWrapped> {
return {
/**
* The schema type.
Expand All @@ -40,7 +40,7 @@ export function nonNullishAsync<TSchema extends BaseSchema | BaseSchemaAsync>(
/**
* The wrapped schema.
*/
wrapped: schema,
wrapped,

/**
* Whether it's async.
Expand Down Expand Up @@ -68,7 +68,7 @@ export function nonNullishAsync<TSchema extends BaseSchema | BaseSchemaAsync>(
}

// Return result of wrapped schema
return schema._parse(input, info);
return wrapped._parse(input, info);
},
};
}
20 changes: 10 additions & 10 deletions library/src/schemas/nonOptional/nonOptional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@ export type NonOptional<T> = T extends undefined ? never : T;
* Non optional schema type.
*/
export type NonOptionalSchema<
TSchema extends BaseSchema,
TOutput = NonOptional<Output<TSchema>>
> = BaseSchema<NonOptional<Input<TSchema>>, TOutput> & {
TWrapped extends BaseSchema,
TOutput = NonOptional<Output<TWrapped>>
> = BaseSchema<NonOptional<Input<TWrapped>>, TOutput> & {
schema: 'non_optional';
wrapped: TSchema;
wrapped: TWrapped;
};

/**
* Creates a non optional schema.
*
* @param schema The wrapped schema.
* @param wrapped The wrapped schema.
* @param error The error message.
*
* @returns A non optional schema.
*/
export function nonOptional<TSchema extends BaseSchema>(
schema: TSchema,
export function nonOptional<TWrapped extends BaseSchema>(
wrapped: TWrapped,
error?: ErrorMessage
): NonOptionalSchema<TSchema> {
): NonOptionalSchema<TWrapped> {
return {
/**
* The schema type.
Expand All @@ -38,7 +38,7 @@ export function nonOptional<TSchema extends BaseSchema>(
/**
* The wrapped schema.
*/
wrapped: schema,
wrapped,

/**
* Whether it's async.
Expand Down Expand Up @@ -66,7 +66,7 @@ export function nonOptional<TSchema extends BaseSchema>(
}

// Return result of wrapped schema
return schema._parse(input, info);
return wrapped._parse(input, info);
},
};
}
14 changes: 7 additions & 7 deletions library/src/schemas/nonOptional/nonOptionalAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import type { NonOptional } from './nonOptional.ts';
* Non optional schema async type.
*/
export type NonOptionalSchemaAsync<
TSchema extends BaseSchema | BaseSchemaAsync,
TOutput = NonOptional<Output<TSchema>>
> = BaseSchemaAsync<NonOptional<Input<TSchema>>, TOutput> & {
TWrapped extends BaseSchema | BaseSchemaAsync,
TOutput = NonOptional<Output<TWrapped>>
> = BaseSchemaAsync<NonOptional<Input<TWrapped>>, TOutput> & {
schema: 'non_optional';
wrapped: TSchema;
wrapped: TWrapped;
};

/**
Expand All @@ -27,10 +27,10 @@ export type NonOptionalSchemaAsync<
*
* @returns An async non optional schema.
*/
export function nonOptionalAsync<TSchema extends BaseSchema | BaseSchemaAsync>(
wrapped: TSchema,
export function nonOptionalAsync<TWrapped extends BaseSchema | BaseSchemaAsync>(
wrapped: TWrapped,
error?: ErrorMessage
): NonOptionalSchemaAsync<TSchema> {
): NonOptionalSchemaAsync<TWrapped> {
return {
/**
* The schema type.
Expand Down
Loading

0 comments on commit 30e460a

Please sign in to comment.