Skip to content

Commit

Permalink
Add support for dynamic error messages #136
Browse files Browse the repository at this point in the history
Co-authored-by: PromiSe#### <vicimpa@users.noreply.github.com>
  • Loading branch information
fabian-hiller and vicimpa committed Sep 17, 2023
1 parent f096e4e commit f78aff6
Show file tree
Hide file tree
Showing 117 changed files with 360 additions and 270 deletions.
1 change: 1 addition & 0 deletions library/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes to the library will be documented in this file.
## vX.X.X (Month DD, YYYY)

- Add support for multiple branding of a value (pull request #88)
- Add support for dynamic error messages via functions (pull request #136)
- Add `skipPipe` option to skip execution of pipeline (pull request #164)

## v0.16.0 (September 16, 2023)
Expand Down
6 changes: 3 additions & 3 deletions library/src/methods/merge/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
type ObjectOutput,
type ObjectSchema,
} from '../../schemas/index.ts';
import type { Pipe } from '../../types.ts';
import type { ErrorMessage, Pipe } from '../../types.ts';
import { getDefaultArgs } from '../../utils/index.ts';
import type { MergeSchemaObjects } from './types.ts';

Expand Down Expand Up @@ -42,13 +42,13 @@ export function merge<TObjectSchemas extends ObjectSchemas>(
*/
export function merge<TObjectSchemas extends ObjectSchemas>(
schemas: TObjectSchemas,
error?: string,
error?: ErrorMessage,
pipe?: Pipe<ObjectOutput<MergeSchemaObjects<TObjectSchemas>>>
): ObjectSchema<MergeSchemaObjects<TObjectSchemas>>;

export function merge<TObjectSchemas extends ObjectSchemas>(
schemas: TObjectSchemas,
arg2?: Pipe<ObjectOutput<MergeSchemaObjects<TObjectSchemas>>> | string,
arg2?: Pipe<ObjectOutput<MergeSchemaObjects<TObjectSchemas>>> | ErrorMessage,
arg3?: Pipe<ObjectOutput<MergeSchemaObjects<TObjectSchemas>>>
): ObjectSchema<MergeSchemaObjects<TObjectSchemas>> {
// Get error and pipe argument
Expand Down
8 changes: 5 additions & 3 deletions library/src/methods/merge/mergeAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
type ObjectSchema,
type ObjectSchemaAsync,
} from '../../schemas/index.ts';
import type { PipeAsync } from '../../types.ts';
import type { ErrorMessage, PipeAsync } from '../../types.ts';
import { getDefaultArgs } from '../../utils/index.ts';
import type { MergeSchemaObjects } from './types.ts';

Expand Down Expand Up @@ -43,13 +43,15 @@ export function mergeAsync<TObjectSchemas extends ObjectSchemas>(
*/
export function mergeAsync<TObjectSchemas extends ObjectSchemas>(
schemas: TObjectSchemas,
error?: string,
error?: ErrorMessage,
pipe?: PipeAsync<ObjectOutput<MergeSchemaObjects<TObjectSchemas>>>
): ObjectSchemaAsync<MergeSchemaObjects<TObjectSchemas>>;

export function mergeAsync<TObjectSchemas extends ObjectSchemas>(
schemas: TObjectSchemas,
arg2?: PipeAsync<ObjectOutput<MergeSchemaObjects<TObjectSchemas>>> | string,
arg2?:
| PipeAsync<ObjectOutput<MergeSchemaObjects<TObjectSchemas>>>
| ErrorMessage,
arg3?: PipeAsync<ObjectOutput<MergeSchemaObjects<TObjectSchemas>>>
): ObjectSchemaAsync<MergeSchemaObjects<TObjectSchemas>> {
// Get error and pipe argument
Expand Down
6 changes: 3 additions & 3 deletions library/src/methods/omit/omit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
type ObjectOutput,
type ObjectSchema,
} from '../../schemas/index.ts';
import type { Pipe } from '../../types.ts';
import type { ErrorMessage, Pipe } from '../../types.ts';
import { getDefaultArgs } from '../../utils/index.ts';
import type { ObjectKeys } from './types.ts';

Expand Down Expand Up @@ -43,7 +43,7 @@ export function omit<
>(
schema: TObjectSchema,
keys: TKeys,
error?: string,
error?: ErrorMessage,
pipe?: Pipe<ObjectOutput<Omit<TObjectSchema['object'], TKeys[number]>>>
): ObjectSchema<Omit<TObjectSchema['object'], TKeys[number]>>;

Expand All @@ -55,7 +55,7 @@ export function omit<
keys: TKeys,
arg3?:
| Pipe<ObjectOutput<Omit<TObjectSchema['object'], TKeys[number]>>>
| string,
| ErrorMessage,
arg4?: Pipe<ObjectOutput<Omit<TObjectSchema['object'], TKeys[number]>>>
): ObjectSchema<Omit<TObjectSchema['object'], TKeys[number]>> {
// Get error and pipe argument
Expand Down
6 changes: 3 additions & 3 deletions library/src/methods/omit/omitAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
type ObjectSchema,
type ObjectSchemaAsync,
} from '../../schemas/index.ts';
import type { PipeAsync } from '../../types.ts';
import type { ErrorMessage, PipeAsync } from '../../types.ts';
import { getDefaultArgs } from '../../utils/index.ts';
import type { ObjectKeys } from './types.ts';

Expand Down Expand Up @@ -44,7 +44,7 @@ export function omitAsync<
>(
schema: TObjectSchema,
keys: TKeys,
error?: string,
error?: ErrorMessage,
pipe?: PipeAsync<ObjectOutput<Omit<TObjectSchema['object'], TKeys[number]>>>
): ObjectSchemaAsync<Omit<TObjectSchema['object'], TKeys[number]>>;

Expand All @@ -56,7 +56,7 @@ export function omitAsync<
keys: TKeys,
arg3?:
| PipeAsync<ObjectOutput<Omit<TObjectSchema['object'], TKeys[number]>>>
| string,
| ErrorMessage,
arg4?: PipeAsync<ObjectOutput<Omit<TObjectSchema['object'], TKeys[number]>>>
): ObjectSchemaAsync<Omit<TObjectSchema['object'], TKeys[number]>> {
// Get error and pipe argument
Expand Down
6 changes: 3 additions & 3 deletions library/src/methods/partial/partial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
optional,
type OptionalSchema,
} from '../../schemas/index.ts';
import type { BaseSchema, Pipe } from '../../types.ts';
import type { BaseSchema, ErrorMessage, Pipe } from '../../types.ts';
import { getDefaultArgs } from '../../utils/index.ts';

/**
Expand Down Expand Up @@ -42,13 +42,13 @@ export function partial<TObjectSchema extends ObjectSchema<any>>(
*/
export function partial<TObjectSchema extends ObjectSchema<any>>(
schema: TObjectSchema,
error?: string,
error?: ErrorMessage,
pipe?: Pipe<ObjectOutput<Partial<TObjectSchema['object']>>>
): ObjectSchema<Partial<TObjectSchema['object']>>;

export function partial<TObjectSchema extends ObjectSchema<any>>(
schema: TObjectSchema,
arg3?: Pipe<ObjectOutput<Partial<TObjectSchema['object']>>> | string,
arg3?: Pipe<ObjectOutput<Partial<TObjectSchema['object']>>> | ErrorMessage,
arg4?: Pipe<ObjectOutput<Partial<TObjectSchema['object']>>>
): ObjectSchema<Partial<TObjectSchema['object']>> {
// Get error and pipe argument
Expand Down
8 changes: 5 additions & 3 deletions library/src/methods/partial/partialAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
optionalAsync,
type OptionalSchemaAsync,
} from '../../schemas/index.ts';
import type { BaseSchema, PipeAsync } from '../../types.ts';
import type { BaseSchema, ErrorMessage, PipeAsync } from '../../types.ts';
import { getDefaultArgs } from '../../utils/index.ts';

/**
Expand Down Expand Up @@ -47,15 +47,17 @@ export function partialAsync<
TObjectSchema extends ObjectSchema<any> | ObjectSchemaAsync<any>
>(
schema: TObjectSchema,
error?: string,
error?: ErrorMessage,
pipe?: PipeAsync<ObjectOutput<Partial<TObjectSchema['object']>>>
): ObjectSchemaAsync<Partial<TObjectSchema['object']>>;

export function partialAsync<
TObjectSchema extends ObjectSchema<any> | ObjectSchemaAsync<any>
>(
schema: TObjectSchema,
arg3?: PipeAsync<ObjectOutput<Partial<TObjectSchema['object']>>> | string,
arg3?:
| PipeAsync<ObjectOutput<Partial<TObjectSchema['object']>>>
| ErrorMessage,
arg4?: PipeAsync<ObjectOutput<Partial<TObjectSchema['object']>>>
): ObjectSchemaAsync<Partial<TObjectSchema['object']>> {
// Get error and pipe argument
Expand Down
6 changes: 3 additions & 3 deletions library/src/methods/pick/pick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
type ObjectOutput,
type ObjectSchema,
} from '../../schemas/index.ts';
import type { Pipe } from '../../types.ts';
import type { ErrorMessage, Pipe } from '../../types.ts';
import { getDefaultArgs } from '../../utils/index.ts';

/**
Expand Down Expand Up @@ -42,7 +42,7 @@ export function pick<
>(
schema: TObjectSchema,
keys: TKeys,
error?: string,
error?: ErrorMessage,
pipe?: Pipe<ObjectOutput<Pick<TObjectSchema['object'], TKeys[number]>>>
): ObjectSchema<Pick<TObjectSchema['object'], TKeys[number]>>;

Expand All @@ -54,7 +54,7 @@ export function pick<
keys: TKeys,
arg3?:
| Pipe<ObjectOutput<Pick<TObjectSchema['object'], TKeys[number]>>>
| string,
| ErrorMessage,
arg4?: Pipe<ObjectOutput<Pick<TObjectSchema['object'], TKeys[number]>>>
): ObjectSchema<Pick<TObjectSchema['object'], TKeys[number]>> {
// Get error and pipe argument
Expand Down
6 changes: 3 additions & 3 deletions library/src/methods/pick/pickAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
type ObjectSchema,
type ObjectSchemaAsync,
} from '../../schemas/index.ts';
import type { PipeAsync } from '../../types.ts';
import type { ErrorMessage, PipeAsync } from '../../types.ts';
import { getDefaultArgs } from '../../utils/index.ts';

/**
Expand Down Expand Up @@ -43,7 +43,7 @@ export function pickAsync<
>(
schema: TObjectSchema,
keys: TKeys,
error?: string,
error?: ErrorMessage,
pipe?: PipeAsync<ObjectOutput<Pick<TObjectSchema['object'], TKeys[number]>>>
): ObjectSchemaAsync<Pick<TObjectSchema['object'], TKeys[number]>>;

Expand All @@ -55,7 +55,7 @@ export function pickAsync<
keys: TKeys,
arg3?:
| PipeAsync<ObjectOutput<Pick<TObjectSchema['object'], TKeys[number]>>>
| string,
| ErrorMessage,
arg4?: PipeAsync<ObjectOutput<Pick<TObjectSchema['object'], TKeys[number]>>>
): ObjectSchemaAsync<Pick<TObjectSchema['object'], TKeys[number]>> {
// Get error and pipe argument
Expand Down
6 changes: 3 additions & 3 deletions library/src/methods/required/required.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
type ObjectSchema,
type ObjectShape,
} from '../../schemas/index.ts';
import type { BaseSchema, Pipe } from '../../types.ts';
import type { BaseSchema, ErrorMessage, Pipe } from '../../types.ts';
import { getDefaultArgs } from '../../utils/index.ts';

/**
Expand Down Expand Up @@ -42,13 +42,13 @@ export function required<TObjectSchema extends ObjectSchema<any>>(
*/
export function required<TObjectSchema extends ObjectSchema<any>>(
schema: TObjectSchema,
error?: string,
error?: ErrorMessage,
pipe?: Pipe<ObjectOutput<Required<TObjectSchema['object']>>>
): ObjectSchema<Required<TObjectSchema['object']>>;

export function required<TObjectSchema extends ObjectSchema<any>>(
schema: TObjectSchema,
arg3?: Pipe<ObjectOutput<Required<TObjectSchema['object']>>> | string,
arg3?: Pipe<ObjectOutput<Required<TObjectSchema['object']>>> | ErrorMessage,
arg4?: Pipe<ObjectOutput<Required<TObjectSchema['object']>>>
): ObjectSchema<Required<TObjectSchema['object']>> {
// Get error and pipe argument
Expand Down
8 changes: 5 additions & 3 deletions library/src/methods/required/requiredAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
type ObjectSchemaAsync,
type ObjectShapeAsync,
} from '../../schemas/index.ts';
import type { BaseSchema, PipeAsync } from '../../types.ts';
import type { BaseSchema, ErrorMessage, PipeAsync } from '../../types.ts';
import { getDefaultArgs } from '../../utils/index.ts';

/**
Expand Down Expand Up @@ -47,15 +47,17 @@ export function requiredAsync<
TObjectSchema extends ObjectSchema<any> | ObjectSchemaAsync<any>
>(
schema: TObjectSchema,
error?: string,
error?: ErrorMessage,
pipe?: PipeAsync<ObjectOutput<Required<TObjectSchema['object']>>>
): ObjectSchemaAsync<Required<TObjectSchema['object']>>;

export function requiredAsync<
TObjectSchema extends ObjectSchema<any> | ObjectSchemaAsync<any>
>(
schema: TObjectSchema,
arg3?: PipeAsync<ObjectOutput<Required<TObjectSchema['object']>>> | string,
arg3?:
| PipeAsync<ObjectOutput<Required<TObjectSchema['object']>>>
| ErrorMessage,
arg4?: PipeAsync<ObjectOutput<Required<TObjectSchema['object']>>>
): ObjectSchemaAsync<Required<TObjectSchema['object']>> {
// Get error and pipe argument
Expand Down
3 changes: 2 additions & 1 deletion library/src/methods/strict/strict.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ObjectSchema } from '../../schemas/object/index.ts';
import type { ErrorMessage } from '../../types.ts';
import { getSchemaIssues } from '../../utils/index.ts';

/**
Expand All @@ -12,7 +13,7 @@ import { getSchemaIssues } from '../../utils/index.ts';
*/
export function strict<TSchema extends ObjectSchema<any>>(
schema: TSchema,
error?: string
error?: ErrorMessage
): TSchema {
return {
...schema,
Expand Down
3 changes: 2 additions & 1 deletion library/src/methods/strict/strictAsync.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ObjectSchemaAsync } from '../../schemas/object/index.ts';
import type { ErrorMessage } from '../../types.ts';
import { getSchemaIssues } from '../../utils/index.ts';

/**
Expand All @@ -12,7 +13,7 @@ import { getSchemaIssues } from '../../utils/index.ts';
*/
export function strictAsync<TSchema extends ObjectSchemaAsync<any>>(
schema: TSchema,
error?: string
error?: ErrorMessage
): TSchema {
return {
...schema,
Expand Down
13 changes: 10 additions & 3 deletions library/src/schemas/array/array.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import type { BaseSchema, Input, Issues, Output, Pipe } from '../../types.ts';
import type {
BaseSchema,
ErrorMessage,
Input,
Issues,
Output,
Pipe,
} from '../../types.ts';
import {
executePipe,
getDefaultArgs,
Expand Down Expand Up @@ -42,13 +49,13 @@ export function array<TArrayItem extends BaseSchema>(
*/
export function array<TArrayItem extends BaseSchema>(
item: TArrayItem,
error?: string,
error?: ErrorMessage,
pipe?: Pipe<Output<TArrayItem>[]>
): ArraySchema<TArrayItem>;

export function array<TArrayItem extends BaseSchema>(
item: TArrayItem,
arg2?: string | Pipe<Output<TArrayItem>[]>,
arg2?: ErrorMessage | Pipe<Output<TArrayItem>[]>,
arg3?: Pipe<Output<TArrayItem>[]>
): ArraySchema<TArrayItem> {
// Get error and pipe argument
Expand Down
5 changes: 3 additions & 2 deletions library/src/schemas/array/arrayAsync.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
BaseSchema,
BaseSchemaAsync,
ErrorMessage,
Input,
Issues,
Output,
Expand Down Expand Up @@ -49,13 +50,13 @@ export function arrayAsync<TArrayItem extends BaseSchema | BaseSchemaAsync>(
*/
export function arrayAsync<TArrayItem extends BaseSchema | BaseSchemaAsync>(
item: TArrayItem,
error?: string,
error?: ErrorMessage,
pipe?: PipeAsync<Output<TArrayItem>[]>
): ArraySchemaAsync<TArrayItem>;

export function arrayAsync<TArrayItem extends BaseSchema | BaseSchemaAsync>(
item: TArrayItem,
arg2?: string | PipeAsync<Output<TArrayItem>[]>,
arg2?: ErrorMessage | PipeAsync<Output<TArrayItem>[]>,
arg3?: PipeAsync<Output<TArrayItem>[]>
): ArraySchemaAsync<TArrayItem> {
// Get error and pipe argument
Expand Down
6 changes: 3 additions & 3 deletions library/src/schemas/bigint/bigint.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { BaseSchema, Pipe } from '../../types.ts';
import type { BaseSchema, ErrorMessage, Pipe } from '../../types.ts';
import {
executePipe,
getDefaultArgs,
Expand Down Expand Up @@ -29,10 +29,10 @@ export function bigint(pipe?: Pipe<bigint>): BigintSchema;
*
* @returns A bigint schema.
*/
export function bigint(error?: string, pipe?: Pipe<bigint>): BigintSchema;
export function bigint(error?: ErrorMessage, pipe?: Pipe<bigint>): BigintSchema;

export function bigint(
arg1?: string | Pipe<bigint>,
arg1?: ErrorMessage | Pipe<bigint>,
arg2?: Pipe<bigint>
): BigintSchema {
// Get error and pipe argument
Expand Down
6 changes: 3 additions & 3 deletions library/src/schemas/bigint/bigintAsync.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { BaseSchemaAsync, PipeAsync } from '../../types.ts';
import type { BaseSchemaAsync, ErrorMessage, PipeAsync } from '../../types.ts';
import {
executePipeAsync,
getDefaultArgs,
Expand Down Expand Up @@ -33,12 +33,12 @@ export function bigintAsync(pipe?: PipeAsync<bigint>): BigintSchemaAsync;
* @returns An async bigint schema.
*/
export function bigintAsync(
error?: string,
error?: ErrorMessage,
pipe?: PipeAsync<bigint>
): BigintSchemaAsync;

export function bigintAsync(
arg1?: string | PipeAsync<bigint>,
arg1?: ErrorMessage | PipeAsync<bigint>,
arg2?: PipeAsync<bigint>
): BigintSchemaAsync {
// Get error and pipe argument
Expand Down
Loading

0 comments on commit f78aff6

Please sign in to comment.