Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support metadata #411

Open
wants to merge 31 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
121a5e1
✨ feat: SchemaMetadata interface
xcfox Feb 3, 2024
a0abc5f
✨ feat: metadata for any, array
xcfox Feb 3, 2024
146195b
✨ feat: Add metadata support to object schemas
xcfox Feb 3, 2024
81a03fb
✨ feat: Add metadata support to tuple schemas
xcfox Feb 3, 2024
4be4252
✨ feat: Add metadata support to bigint and bigintAsync schemas
xcfox Feb 3, 2024
977f84b
⚙️ refactor: Update metadata and add default values for schema objects
xcfox Feb 4, 2024
f6ef468
✨ feat: metadata for blob, boolean, date
xcfox Feb 4, 2024
47aa815
✨ feat: metadata for instance, intersect
xcfox Feb 4, 2024
ece4fc8
✨ feat: Add metadata support to map and literal schemas
xcfox Feb 4, 2024
59d61b3
✨ feat: Add metadata support to never and nan schemas
xcfox Feb 4, 2024
9bc2219
Merge branch 'main' into feat/message-metadata
xcfox Feb 4, 2024
22e1259
✨ feat: Add metadata support to nonOptional, nonNullable, nonNullish …
xcfox Feb 4, 2024
76d5633
✨ feat: Add metadata support to null and nullAsync schemas
xcfox Feb 4, 2024
fa46ccc
✨ feat: Add metadata property to schema wrappers
xcfox Feb 4, 2024
36dcffb
✨ feat: Add metadata getter to nonNullable, nonNullableAsync, nonNull…
xcfox Feb 4, 2024
eeea3fb
✨ feat: Add metadata support to number and picklist schemas
xcfox Feb 4, 2024
e266a3e
✨ feat: Add metadata support to record schemas
xcfox Feb 4, 2024
ca80d57
✨ feat: Add metadata support to recursive and recursiveAsync schemas
xcfox Feb 4, 2024
0a8db50
Fix error message in set schema
xcfox Feb 4, 2024
a28f20f
✨ feat: Add metadata support to special and specialAsync schemas
xcfox Feb 4, 2024
6a7370c
✨ feat: Add metadata support to symbol schemas
xcfox Feb 4, 2024
8680a29
✨ feat: Add metadata support to undefined schemas
xcfox Feb 4, 2024
4483bae
✨ feat: Add metadata support to void, voidAsync, union, and unionAsyn…
xcfox Feb 4, 2024
7eb53f3
✨ feat: Add metadata support to unknown schemas
xcfox Feb 4, 2024
ed595ff
✨ feat: Add metadata for partial, required, pick, partialAsync, requi…
xcfox Feb 4, 2024
3975c03
Merge branch 'main' into feat/message-metadata
xcfox Feb 6, 2024
05f5e3f
🐞 fix: expose metadata for nonNullable
xcfox Feb 6, 2024
ffd3264
Merge branch 'main' into feat/message-metadata
xcfox Feb 18, 2024
ee1496f
Merge branch 'main' into feat/message-metadata
xcfox Feb 20, 2024
08fa39f
Merge branch 'main' into feat/message-metadata
xcfox Feb 25, 2024
02903ea
🐞 fix: format code
xcfox Feb 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions library/src/methods/merge/merge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,25 @@ describe('merge', () => {
expect(output1).toEqual(transformInput());
expect(output2).toEqual(transformInput());
});

test('should expose the metadata', async () => {
const schema1 = merge(
[object({ key1: string() }), object({ key2: number() })],
{ description: 'a simple object' }
);
expect(schema1.metadata).toEqual({ description: 'a simple object' });
const schema2 = merge(
[object({ key1: string() }), object({ key2: number() })],
number(),
{ description: 'an object with a rest' }
);
expect(schema2.metadata).toEqual({ description: 'an object with a rest' });

const schema3 = merge(
[object({ key1: string() }), object({ key2: number() })],
{ description: 'a simple object', message: 'Value is not an object!' }
);
expect(schema3.metadata).toEqual({ description: 'a simple object' });
expect(schema3.message).toEqual('Value is not an object!');
});
});
24 changes: 15 additions & 9 deletions library/src/methods/merge/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import {
type ObjectOutput,
type ObjectSchema,
} from '../../schemas/index.ts';
import type { BaseSchema, ErrorMessage, Pipe } from '../../types/index.ts';
import type {
BaseSchema,
ErrorMessageOrMetadata,
Pipe,
} from '../../types/index.ts';
import { restAndDefaultArgs } from '../../utils/index.ts';
import type { MergeObjects } from './types.ts';

Expand Down Expand Up @@ -35,14 +39,14 @@ export function merge<TSchemas extends ObjectSchemas>(
* overwrite the previous ones.
*
* @param schemas The schemas to be merged.
* @param message The error message.
* @param messageOrMetadata The error message or schema metadata.
* @param pipe A validation and transformation pipe.
*
* @returns An object schema.
*/
export function merge<TSchemas extends ObjectSchemas>(
schemas: TSchemas,
message?: ErrorMessage,
messageOrMetadata?: ErrorMessageOrMetadata,
pipe?: Pipe<ObjectOutput<MergeObjects<TSchemas>, undefined>>
): ObjectSchema<MergeObjects<TSchemas>>;

Expand Down Expand Up @@ -71,7 +75,7 @@ export function merge<
*
* @param schemas The schemas to be merged.
* @param rest The object rest.
* @param message The error message.
* @param messageOrMetadata The error message or schema metadata.
* @param pipe A validation and transformation pipe.
*
* @returns An object schema.
Expand All @@ -82,7 +86,7 @@ export function merge<
>(
schemas: TSchemas,
rest: TRest,
message?: ErrorMessage,
messageOrMetadata?: ErrorMessageOrMetadata,
pipe?: Pipe<ObjectOutput<MergeObjects<TSchemas>, TRest>>
): ObjectSchema<MergeObjects<TSchemas>, TRest>;

Expand All @@ -93,13 +97,15 @@ export function merge<
schemas: TSchemas,
arg2?:
| Pipe<ObjectOutput<MergeObjects<TSchemas>, TRest>>
| ErrorMessage
| ErrorMessageOrMetadata
| TRest,
arg3?: Pipe<ObjectOutput<MergeObjects<TSchemas>, TRest>> | ErrorMessage,
arg3?:
| Pipe<ObjectOutput<MergeObjects<TSchemas>, TRest>>
| ErrorMessageOrMetadata,
arg4?: Pipe<ObjectOutput<MergeObjects<TSchemas>, TRest>>
): ObjectSchema<MergeObjects<TSchemas>, TRest> {
// Get rest, message and pipe argument
const [rest, message, pipe] = restAndDefaultArgs<
const [rest, message, pipe, metadata] = restAndDefaultArgs<
TRest,
Pipe<ObjectOutput<MergeObjects<TSchemas>, TRest>>
>(arg2, arg3, arg4);
Expand All @@ -111,7 +117,7 @@ export function merge<
{}
) as MergeObjects<TSchemas>,
rest,
message,
metadata === undefined ? message : { message, ...metadata },
pipe
);
}
21 changes: 21 additions & 0 deletions library/src/methods/merge/mergeAsync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,25 @@ describe('mergeAsync', () => {
expect(output1).toEqual(transformInput());
expect(output2).toEqual(transformInput());
});

test('should expose the metadata', async () => {
const schema1 = mergeAsync(
[objectAsync({ key1: string() }), object({ key2: number() })],
{ description: 'a simple object' }
);
expect(schema1.metadata).toEqual({ description: 'a simple object' });
const schema2 = mergeAsync(
[objectAsync({ key1: string() }), object({ key2: number() })],
number(),
{ description: 'an object with a rest' }
);
expect(schema2.metadata).toEqual({ description: 'an object with a rest' });

const schema3 = mergeAsync(
[object({ key1: string() }), object({ key2: number() })],
{ description: 'a simple object', message: 'Value is not an object!' }
);
expect(schema3.metadata).toEqual({ description: 'a simple object' });
expect(schema3.message).toEqual('Value is not an object!');
});
});
20 changes: 11 additions & 9 deletions library/src/methods/merge/mergeAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import type {
BaseSchema,
BaseSchemaAsync,
ErrorMessage,
ErrorMessageOrMetadata,
PipeAsync,
} from '../../types/index.ts';
import { restAndDefaultArgs } from '../../utils/index.ts';
Expand Down Expand Up @@ -41,14 +41,14 @@ export function mergeAsync<TSchemas extends ObjectSchemas>(
* entries overwrite the previous ones.
*
* @param schemas The schemas to be merged.
* @param message The error message.
* @param messageOrMetadata The error message or schema metadata.
* @param pipe A validation and transformation pipe.
*
* @returns An async object schema.
*/
export function mergeAsync<TSchemas extends ObjectSchemas>(
schemas: TSchemas,
message?: ErrorMessage,
messageOrMetadata?: ErrorMessageOrMetadata,
pipe?: PipeAsync<ObjectOutput<MergeObjects<TSchemas>, undefined>>
): ObjectSchemaAsync<MergeObjects<TSchemas>>;

Expand Down Expand Up @@ -77,7 +77,7 @@ export function mergeAsync<
*
* @param schemas The schemas to be merged.
* @param rest The object rest.
* @param message The error message.
* @param messageOrMetadata The error message or schema metadata.
* @param pipe A validation and transformation pipe.
*
* @returns An async object schema.
Expand All @@ -88,7 +88,7 @@ export function mergeAsync<
>(
schemas: TSchemas,
rest: TRest,
message?: ErrorMessage,
messageOrMetadata?: ErrorMessageOrMetadata,
pipe?: PipeAsync<ObjectOutput<MergeObjects<TSchemas>, TRest>>
): ObjectSchemaAsync<MergeObjects<TSchemas>, TRest>;

Expand All @@ -99,13 +99,15 @@ export function mergeAsync<
schemas: TSchemas,
arg2?:
| PipeAsync<ObjectOutput<MergeObjects<TSchemas>, TRest>>
| ErrorMessage
| ErrorMessageOrMetadata
| TRest,
arg3?: PipeAsync<ObjectOutput<MergeObjects<TSchemas>, TRest>> | ErrorMessage,
arg3?:
| PipeAsync<ObjectOutput<MergeObjects<TSchemas>, TRest>>
| ErrorMessageOrMetadata,
arg4?: PipeAsync<ObjectOutput<MergeObjects<TSchemas>, TRest>>
): ObjectSchemaAsync<MergeObjects<TSchemas>, TRest> {
// Get rest, message and pipe argument
const [rest, message, pipe] = restAndDefaultArgs<
const [rest, message, pipe, metadata] = restAndDefaultArgs<
TRest,
PipeAsync<ObjectOutput<MergeObjects<TSchemas>, TRest>>
>(arg2, arg3, arg4);
Expand All @@ -117,7 +119,7 @@ export function mergeAsync<
{}
) as MergeObjects<TSchemas>,
rest,
message,
metadata === undefined ? message : { message, ...metadata },
pipe
);
}
27 changes: 26 additions & 1 deletion library/src/methods/omit/omit.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, test } from 'vitest';
import { comparable } from '../../comparable.ts';
import { object, string } from '../../schemas/index.ts';
import { number, object, string } from '../../schemas/index.ts';
import { toCustom } from '../../transformations/index.ts';
import { omit } from '../omit/index.ts';
import { parse } from '../parse/index.ts';
Expand Down Expand Up @@ -48,4 +48,29 @@ describe('omit', () => {
expect(output1).toEqual(transformInput());
expect(output2).toEqual(transformInput());
});

test('should expose the metadata', async () => {
const schema1 = omit(
object({ key1: string(), key2: string(), key3: string() }),
['key1', 'key3'],
{ description: 'a simple object' }
);
expect(schema1.metadata).toEqual({ description: 'a simple object' });

const schema2 = omit(
object({ key1: string(), key2: string(), key3: string() }),
['key1', 'key3'],
number(),
{ description: 'an object with a rest' }
);
expect(schema2.metadata).toEqual({ description: 'an object with a rest' });

const schema3 = omit(
object({ key1: string(), key2: string(), key3: string() }),
['key1', 'key3'],
{ description: 'a simple object', message: 'Value is not an object!' }
);
expect(schema3.metadata).toEqual({ description: 'a simple object' });
expect(schema3.message).toEqual('Value is not an object!');
});
});
18 changes: 9 additions & 9 deletions library/src/methods/omit/omit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from '../../schemas/index.ts';
import type {
BaseSchema,
ErrorMessage,
ErrorMessageOrMetadata,
ObjectKeys,
Pipe,
} from '../../types/index.ts';
Expand Down Expand Up @@ -36,7 +36,7 @@ export function omit<
*
* @param schema The schema to omit from.
* @param keys The selected keys
* @param message The error message.
* @param messageOrMetadata The error message or schema metadata.
* @param pipe A validation and transformation pipe.
*
* @returns An object schema.
Expand All @@ -47,7 +47,7 @@ export function omit<
>(
schema: TSchema,
keys: TKeys,
message?: ErrorMessage,
messageOrMetadata?: ErrorMessageOrMetadata,
pipe?: Pipe<ObjectOutput<Omit<TSchema['entries'], TKeys[number]>, undefined>>
): ObjectSchema<Omit<TSchema['entries'], TKeys[number]>>;

Expand Down Expand Up @@ -80,7 +80,7 @@ export function omit<
* @param schema The schema to omit from.
* @param keys The selected keys
* @param rest The object rest.
* @param message The error message.
* @param messageOrMetadata The error message or schema metadata.
* @param pipe A validation and transformation pipe.
*
* @returns An object schema.
Expand All @@ -93,7 +93,7 @@ export function omit<
schema: TSchema,
keys: TKeys,
rest: TRest,
message?: ErrorMessage,
messageOrMetadata?: ErrorMessageOrMetadata,
pipe?: Pipe<ObjectOutput<Omit<TSchema['entries'], TKeys[number]>, TRest>>
): ObjectSchema<Omit<TSchema['entries'], TKeys[number]>, TRest>;

Expand All @@ -106,15 +106,15 @@ export function omit<
keys: TKeys,
arg3?:
| Pipe<ObjectOutput<Omit<TSchema['entries'], TKeys[number]>, TRest>>
| ErrorMessage
| ErrorMessageOrMetadata
| TRest,
arg4?:
| Pipe<ObjectOutput<Omit<TSchema['entries'], TKeys[number]>, TRest>>
| ErrorMessage,
| ErrorMessageOrMetadata,
arg5?: Pipe<ObjectOutput<Omit<TSchema['entries'], TKeys[number]>, TRest>>
): ObjectSchema<Omit<TSchema['entries'], TKeys[number]>, TRest> {
// Get rest, message and pipe argument
const [rest, message, pipe] = restAndDefaultArgs<
const [rest, message, pipe, metadata] = restAndDefaultArgs<
TRest,
Pipe<ObjectOutput<Omit<TSchema['entries'], TKeys[number]>, TRest>>
>(arg3, arg4, arg5);
Expand All @@ -127,7 +127,7 @@ export function omit<
{}
) as Omit<TSchema['entries'], TKeys[number]>,
rest,
message,
metadata === undefined ? message : { message, ...metadata },
pipe
);
}
25 changes: 25 additions & 0 deletions library/src/methods/omit/omitAsync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,29 @@ describe('omitAsync', () => {
expect(output1).toEqual(transformInput());
expect(output2).toEqual(transformInput());
});

test('should expose the metadata', async () => {
const schema1 = omitAsync(
object({ key1: string(), key2: string(), key3: string() }),
['key1', 'key3'],
{ description: 'a simple object' }
);
expect(schema1.metadata).toEqual({ description: 'a simple object' });

const schema2 = omitAsync(
object({ key1: string(), key2: string(), key3: string() }),
['key1', 'key3'],
string(),
{ description: 'an object with a rest' }
);
expect(schema2.metadata).toEqual({ description: 'an object with a rest' });

const schema3 = omitAsync(
object({ key1: string(), key2: string(), key3: string() }),
['key1', 'key3'],
{ description: 'a simple object', message: 'Value is not an object!' }
);
expect(schema3.metadata).toEqual({ description: 'a simple object' });
expect(schema3.message).toEqual('Value is not an object!');
});
});
Loading
Loading