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

Add Schema Reflection API #211

Merged
merged 19 commits into from
Nov 19, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
11 changes: 11 additions & 0 deletions library/src/schemas/any/any.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, test } from 'vitest';
import { parse } from '../../methods/index.ts';
import { toCustom } from '../../transformations/index.ts';
import { maxLength } from '../../validations/index.ts';
import { any } from './any.ts';

describe('any', () => {
Expand All @@ -22,4 +23,14 @@ describe('any', () => {
const output = parse(any([toCustom(transformInput)]), 123);
expect(output).toBe(transformInput());
});

test(`should expose an array of applied validation checks`, () => {
const schema1 = any([toCustom(String), maxLength(5)]);
expect(schema1.checks).toStrictEqual([
{ kind: 'max_length', requirement: 5, message: 'Invalid length' },
]);

const schema2 = any();
expect(schema2.checks).toStrictEqual([]);
});
});
11 changes: 9 additions & 2 deletions library/src/schemas/any/any.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { BaseSchema, Pipe } from '../../types.ts';
import { executePipe } from '../../utils/index.ts';
import type { BaseSchema, Pipe, PipeMeta } from '../../types.ts';
import { executePipe, getChecks } from '../../utils/index.ts';

/**
* Any schema type.
*/
export type AnySchema<TOutput = any> = BaseSchema<any, TOutput> & {
schema: 'any';
checks: PipeMeta[];
};

/**
Expand All @@ -27,6 +28,12 @@ export function any(pipe: Pipe<any> = []): AnySchema {
*/
async: false,

/**
* Validation checks that will be run against
* the input value.
*/
checks: getChecks(pipe),

/**
fabian-hiller marked this conversation as resolved.
Show resolved Hide resolved
* Parses unknown input based on its schema.
*
Expand Down
11 changes: 11 additions & 0 deletions library/src/schemas/any/anyAsync.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, test } from 'vitest';
import { parseAsync } from '../../methods/index.ts';
import { toCustom } from '../../transformations/index.ts';
import { maxLength } from '../../validations/index.ts';
import { anyAsync } from './anyAsync.ts';

describe('anyAsync', () => {
Expand All @@ -22,4 +23,14 @@ describe('anyAsync', () => {
const output = await parseAsync(anyAsync([toCustom(transformInput)]), 123);
expect(output).toBe(transformInput());
});

test(`should expose an array of applied validation checks`, () => {
const schema1 = anyAsync([toCustom(String), maxLength(5)]);
expect(schema1.checks).toStrictEqual([
{ kind: 'max_length', requirement: 5, message: 'Invalid length' },
]);

const schema2 = anyAsync();
expect(schema2.checks).toStrictEqual([]);
});
});
11 changes: 9 additions & 2 deletions library/src/schemas/any/anyAsync.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { BaseSchemaAsync, PipeAsync } from '../../types.ts';
import { executePipeAsync } from '../../utils/index.ts';
import type { BaseSchemaAsync, PipeAsync, PipeMeta } from '../../types.ts';
import { executePipeAsync, getChecks } from '../../utils/index.ts';

/**
* Any schema type.
*/
export type AnySchemaAsync<TOutput = any> = BaseSchemaAsync<any, TOutput> & {
schema: 'any';
checks: PipeMeta[];
};

/**
Expand All @@ -27,6 +28,12 @@ export function anyAsync(pipe: PipeAsync<any> = []): AnySchemaAsync {
*/
async: true,

/**
* Validation checks that will be run against
* the input value.
*/
checks: getChecks(pipe),

/**
* Parses unknown input based on its schema.
*
Expand Down
10 changes: 10 additions & 0 deletions library/src/schemas/array/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,14 @@ describe('array', () => {
expect(() => parse(schema2, [1, 2])).toThrowError(lengthError);
expect(() => parse(schema2, [1])).toThrowError(contentError);
});

test(`should expose an array of applied validation checks`, () => {
const schema1 = array(string(), [maxLength(5)]);
expect(schema1.checks).toStrictEqual([
{ kind: 'max_length', requirement: 5, message: 'Invalid length' },
]);

const schema2 = array(string());
expect(schema2.checks).toStrictEqual([]);
});
});
9 changes: 9 additions & 0 deletions library/src/schemas/array/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import type {
Issues,
Output,
Pipe,
PipeMeta,
} from '../../types.ts';
import { getChecks } from '../../utils/getChecks/getChecks.ts';
import {
executePipe,
getDefaultArgs,
Expand All @@ -23,6 +25,7 @@ export type ArraySchema<
> = BaseSchema<Input<TArrayItem>[], TOutput> & {
schema: 'array';
array: { item: TArrayItem };
checks: PipeMeta[];
};

/**
Expand Down Expand Up @@ -78,6 +81,12 @@ export function array<TArrayItem extends BaseSchema>(
*/
async: false,

/**
* Validation checks that will be run against
* the input value.
*/
checks: getChecks(pipe ?? []),

/**
* Parses unknown input based on its schema.
*
Expand Down
10 changes: 10 additions & 0 deletions library/src/schemas/array/arrayAsync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,14 @@ describe('array', () => {
await expect(parseAsync(schema2, [1, 2])).rejects.toThrowError(lengthError);
await expect(parseAsync(schema2, [1])).rejects.toThrowError(contentError);
});

test(`should expose an array of applied validation checks`, () => {
const schema1 = arrayAsync(string(), [maxLength(5)]);
expect(schema1.checks).toStrictEqual([
{ kind: 'max_length', requirement: 5, message: 'Invalid length' },
]);

const schema2 = arrayAsync(string());
expect(schema2.checks).toStrictEqual([]);
});
});
9 changes: 9 additions & 0 deletions library/src/schemas/array/arrayAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import type {
Issues,
Output,
PipeAsync,
PipeMeta,
} from '../../types.ts';
import {
executePipeAsync,
getChecks,
getDefaultArgs,
getIssues,
getSchemaIssues,
Expand All @@ -24,6 +26,7 @@ export type ArraySchemaAsync<
> = BaseSchemaAsync<Input<TArrayItem>[], TOutput> & {
schema: 'array';
array: { item: TArrayItem };
checks: PipeMeta[];
};

/**
Expand Down Expand Up @@ -79,6 +82,12 @@ export function arrayAsync<TArrayItem extends BaseSchema | BaseSchemaAsync>(
*/
async: true,

/**
* Validation checks that will be run against
* the input value.
*/
checks: getChecks(pipe ?? []),

/**
* Parses unknown input based on its schema.
*
Expand Down
10 changes: 10 additions & 0 deletions library/src/schemas/bigint/bigint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,14 @@ describe('bigint', () => {
expect(output2).toEqual(input2);
expect(() => parse(schema2, 12346789n)).toThrowError(valueError);
});

test(`should expose an array of applied validation checks`, () => {
const schema1 = bigint([maxValue(500n)]);
expect(schema1.checks).toStrictEqual([
{ kind: 'max_value', requirement: 500n, message: 'Invalid value' },
]);

const schema2 = bigint();
expect(schema2.checks).toStrictEqual([]);
});
});
10 changes: 9 additions & 1 deletion library/src/schemas/bigint/bigint.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { BaseSchema, ErrorMessage, Pipe } from '../../types.ts';
import type { BaseSchema, ErrorMessage, Pipe, PipeMeta } from '../../types.ts';
import { getChecks } from '../../utils/getChecks/getChecks.ts';
import {
executePipe,
getDefaultArgs,
Expand All @@ -10,6 +11,7 @@ import {
*/
export type BigintSchema<TOutput = bigint> = BaseSchema<bigint, TOutput> & {
schema: 'bigint';
checks: PipeMeta[];
};

/**
Expand Down Expand Up @@ -50,6 +52,12 @@ export function bigint(
*/
async: false,

/**
* Validation checks that will be run against
* the input value.
*/
checks: getChecks(pipe ?? []),

/**
* Parses unknown input based on its schema.
*
Expand Down
10 changes: 10 additions & 0 deletions library/src/schemas/bigint/bigintAsync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,14 @@ describe('bigintAsync', () => {
valueError
);
});

test(`should expose an array of applied validation checks`, () => {
const schema1 = bigintAsync([maxValue(500n)]);
expect(schema1.checks).toStrictEqual([
{ kind: 'max_value', requirement: 500n, message: 'Invalid value' },
]);

const schema2 = bigintAsync();
expect(schema2.checks).toStrictEqual([]);
});
});
15 changes: 14 additions & 1 deletion library/src/schemas/bigint/bigintAsync.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import type { BaseSchemaAsync, ErrorMessage, PipeAsync } from '../../types.ts';
import type {
BaseSchemaAsync,
ErrorMessage,
PipeAsync,
PipeMeta,
} from '../../types.ts';
import { getChecks } from '../../utils/getChecks/getChecks.ts';
import {
executePipeAsync,
getDefaultArgs,
Expand All @@ -13,6 +19,7 @@ export type BigintSchemaAsync<TOutput = bigint> = BaseSchemaAsync<
TOutput
> & {
schema: 'bigint';
checks: PipeMeta[];
};

/**
Expand Down Expand Up @@ -56,6 +63,12 @@ export function bigintAsync(
*/
async: true,

/**
* Validation checks that will be run against
* the input value.
*/
checks: getChecks(pipe ?? []),

/**
* Parses unknown input based on its schema.
*
Expand Down
10 changes: 9 additions & 1 deletion library/src/schemas/blob/blob.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { BaseSchema, ErrorMessage, Pipe } from '../../types.ts';
import type { BaseSchema, ErrorMessage, Pipe, PipeMeta } from '../../types.ts';
import { getChecks } from '../../utils/getChecks/getChecks.ts';
import {
executePipe,
getDefaultArgs,
Expand All @@ -10,6 +11,7 @@ import {
*/
export type BlobSchema<TOutput = Blob> = BaseSchema<Blob, TOutput> & {
schema: 'blob';
checks: PipeMeta[];
};

/**
Expand Down Expand Up @@ -50,6 +52,12 @@ export function blob(
*/
async: false,

/**
* Validation checks that will be run against
* the input value.
*/
checks: getChecks(pipe),

/**
* Parses unknown input based on its schema.
*
Expand Down
15 changes: 14 additions & 1 deletion library/src/schemas/blob/blobAsync.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import type { BaseSchemaAsync, ErrorMessage, PipeAsync } from '../../types.ts';
import type {
BaseSchemaAsync,
ErrorMessage,
PipeAsync,
PipeMeta,
} from '../../types.ts';
import { getChecks } from '../../utils/getChecks/getChecks.ts';
import {
executePipeAsync,
getDefaultArgs,
Expand All @@ -10,6 +16,7 @@ import {
*/
export type BlobSchemaAsync<TOutput = Blob> = BaseSchemaAsync<Blob, TOutput> & {
schema: 'blob';
checks: PipeMeta[];
};

/**
Expand Down Expand Up @@ -53,6 +60,12 @@ export function blobAsync(
*/
async: true,

/**
* Validation checks that will be run against
* the input value.
*/
checks: getChecks(pipe),

/**
* Parses unknown input based on its schema.
*
Expand Down
10 changes: 9 additions & 1 deletion library/src/schemas/boolean/boolean.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { BaseSchema, ErrorMessage, Pipe } from '../../types.ts';
import type { BaseSchema, ErrorMessage, Pipe, PipeMeta } from '../../types.ts';
import { getChecks } from '../../utils/getChecks/getChecks.ts';
import {
executePipe,
getDefaultArgs,
Expand All @@ -10,6 +11,7 @@ import {
*/
export type BooleanSchema<TOutput = boolean> = BaseSchema<boolean, TOutput> & {
schema: 'boolean';
checks: PipeMeta[];
};

/**
Expand Down Expand Up @@ -53,6 +55,12 @@ export function boolean(
*/
async: false,

/**
* Validation checks that will be run against
* the input value.
*/
checks: getChecks(pipe),

/**
* Parses unknown input based on its schema.
*
Expand Down
Loading