Skip to content

Commit

Permalink
Simplify return type for validate
Browse files Browse the repository at this point in the history
  • Loading branch information
decs committed Jul 12, 2023
1 parent 860d708 commit a02bdff
Show file tree
Hide file tree
Showing 19 changed files with 79 additions and 136 deletions.
10 changes: 3 additions & 7 deletions src/__tests__/arktype.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@ import {describe, expect, jest, test} from '@jest/globals';
import {type} from 'arktype';

import {assert, validate} from '..';
import {ValidationError} from '../schema';
import {ValidationIssue} from '../schema';

describe('arktype', () => {
const schema = type('string');
const module = 'arktype';

test('validate', async () => {
expect(await validate(schema, '123')).toStrictEqual({
valid: true,
value: '123',
});
expect(await validate(schema, '123')).toStrictEqual({data: '123'});
expect(await validate(schema, 123)).toStrictEqual({
errors: [new ValidationError('Must be a string (was number)')],
valid: false,
issues: [new ValidationIssue('Must be a string (was number)')],
});
jest.mock(module, () => {
throw new Error('Cannot find module');
Expand Down
10 changes: 3 additions & 7 deletions src/__tests__/custom.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {describe, expect, test} from '@jest/globals';

import {assert, validate} from '..';
import {ValidationError} from '../schema';
import {ValidationIssue} from '../schema';

function assertString(value: unknown): string {
if (typeof value !== 'string') {
Expand All @@ -14,13 +14,9 @@ describe('custom', () => {
const schema = assertString;

test('validate', async () => {
expect(await validate(schema, '123')).toStrictEqual({
valid: true,
value: '123',
});
expect(await validate(schema, '123')).toStrictEqual({data: '123'});
expect(await validate(schema, 123)).toStrictEqual({
errors: [new ValidationError('Not a string')],
valid: false,
issues: [new ValidationIssue('Not a string')],
});
});

Expand Down
10 changes: 3 additions & 7 deletions src/__tests__/joi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@ import {describe, expect, jest, test} from '@jest/globals';
import Joi from 'joi';

import {assert, validate} from '..';
import {ValidationError} from '../schema';
import {ValidationIssue} from '../schema';

describe('joi', () => {
const schema = Joi.string();
const module = 'joi';

test('validate', async () => {
expect(await validate(schema, '123')).toStrictEqual({
valid: true,
value: '123',
});
expect(await validate(schema, '123')).toStrictEqual({data: '123'});
expect(await validate(schema, 123)).toStrictEqual({
errors: [new ValidationError('"value" must be a string')],
valid: false,
issues: [new ValidationIssue('"value" must be a string')],
});
jest.mock(module, () => {
throw new Error('Cannot find module');
Expand Down
10 changes: 3 additions & 7 deletions src/__tests__/runtypes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@ import {describe, expect, jest, test} from '@jest/globals';
import {String} from 'runtypes';

import {assert, validate} from '..';
import {ValidationError} from '../schema';
import {ValidationIssue} from '../schema';

describe('runtypes', () => {
const schema = String;
const module = 'runtypes';

test('validate', async () => {
expect(await validate(schema, '123')).toStrictEqual({
valid: true,
value: '123',
});
expect(await validate(schema, '123')).toStrictEqual({data: '123'});
expect(await validate(schema, 123)).toStrictEqual({
errors: [new ValidationError('Expected string, but was number')],
valid: false,
issues: [new ValidationIssue('Expected string, but was number')],
});
jest.mock(module, () => {
throw new Error('Cannot find module');
Expand Down
10 changes: 3 additions & 7 deletions src/__tests__/superstruct.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@ import {describe, expect, jest, test} from '@jest/globals';
import {string} from 'superstruct';

import {assert, validate} from '..';
import {ValidationError} from '../schema';
import {ValidationIssue} from '../schema';

describe('superstruct', () => {
const schema = string();
const module = 'superstruct';

test('validate', async () => {
expect(await validate(schema, '123')).toStrictEqual({
valid: true,
value: '123',
});
expect(await validate(schema, '123')).toStrictEqual({data: '123'});
expect(await validate(schema, 123)).toStrictEqual({
errors: [new ValidationError('Expected a string, but received: 123')],
valid: false,
issues: [new ValidationIssue('Expected a string, but received: 123')],
});
jest.mock(module, () => {
throw new Error('Cannot find module');
Expand Down
10 changes: 3 additions & 7 deletions src/__tests__/typebox.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@ import {describe, expect, jest, test} from '@jest/globals';
import {Type} from '@sinclair/typebox';

import {assert, validate} from '..';
import {ValidationError} from '../schema';
import {ValidationIssue} from '../schema';

describe('typebox', () => {
const schema = Type.String();
const module = '@sinclair/typebox';

test('validate', async () => {
expect(await validate(schema, '123')).toStrictEqual({
valid: true,
value: '123',
});
expect(await validate(schema, '123')).toStrictEqual({data: '123'});
expect(await validate(schema, 123)).toStrictEqual({
errors: [new ValidationError('Expected string')],
valid: false,
issues: [new ValidationIssue('Expected string')],
});
jest.mock(module, () => {
throw new Error('Cannot find module');
Expand Down
12 changes: 4 additions & 8 deletions src/__tests__/typia.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,19 @@ import {describe, expect, test} from '@jest/globals';
import typia from 'typia';

import {assert, validate} from '..';
import {ValidationError} from '../schema';
import {ValidationIssue} from '../schema';

describe('typia', () => {
const schema = typia.createAssert<string>();

test('validate', async () => {
expect(await validate(schema, '123')).toStrictEqual({
valid: true,
value: '123',
});
expect(await validate(schema, '123')).toStrictEqual({data: '123'});
expect(await validate(schema, 123)).toStrictEqual({
errors: [
new ValidationError(
issues: [
new ValidationIssue(
'Error on typia.assert(): invalid type on $input, expect to be string',
),
],
valid: false,
});
});

Expand Down
12 changes: 4 additions & 8 deletions src/__tests__/yup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,20 @@ import {describe, expect, jest, test} from '@jest/globals';
import {string} from 'yup';

import {assert, validate} from '..';
import {ValidationError} from '../schema';
import {ValidationIssue} from '../schema';

describe('yup', () => {
const schema = string();
const module = 'yup';

test('validate', async () => {
expect(await validate(schema, '123')).toStrictEqual({
valid: true,
value: '123',
});
expect(await validate(schema, '123')).toStrictEqual({data: '123'});
expect(await validate(schema, 123)).toStrictEqual({
errors: [
new ValidationError(
issues: [
new ValidationIssue(
'this must be a `string` type, but the final value was: `123`.',
),
],
valid: false,
});
jest.mock(module, () => {
throw new Error('Cannot find module');
Expand Down
10 changes: 3 additions & 7 deletions src/__tests__/zod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@ import {describe, expect, jest, test} from '@jest/globals';
import {z} from 'zod';

import {assert, validate} from '..';
import {ValidationError} from '../schema';
import {ValidationIssue} from '../schema';

describe('zod', () => {
const schema = z.string();
const module = 'zod';

test('validate', async () => {
expect(await validate(schema, '123')).toStrictEqual({
valid: true,
value: '123',
});
expect(await validate(schema, '123')).toStrictEqual({data: '123'});
expect(await validate(schema, 123)).toStrictEqual({
errors: [new ValidationError('Expected string, received number')],
valid: false,
issues: [new ValidationIssue('Expected string, received number')],
});
jest.mock(module, () => {
throw new Error('Cannot find module');
Expand Down
12 changes: 6 additions & 6 deletions src/adapters/arktype.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type {TypeSchemaResolver} from '../resolver';
import type {TypeSchema} from '../schema';
import type {Problems, Type} from 'arktype';

import {register} from '../registry';
import {ValidationError} from '../schema';
import {ValidationIssue} from '../schema';
import {maybe} from '../utils';

interface ArkTypeResolver extends TypeSchemaResolver {
Expand All @@ -29,17 +30,16 @@ register<'arktype'>(
}
return schema;
},
<T>(schema: Type<T>) => ({
<T>(schema: Type<T>): TypeSchema<T> => ({
validate: async data => {
const result = schema(data);
if (result.problems == null) {
return {valid: true, value: result.data};
return {data: result.data as T};
}
return {
errors: [...result.problems].map(
({message, path}) => new ValidationError(message, path),
issues: [...result.problems].map(
({message, path}) => new ValidationIssue(message, path),
),
valid: false,
};
},
}),
Expand Down
9 changes: 3 additions & 6 deletions src/adapters/function.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {TypeSchemaResolver} from '../resolver';

import {register} from '../registry';
import {ValidationError} from '../schema';
import {ValidationIssue} from '../schema';

type FunctionSchema<T = unknown> = (data: unknown) => T;

Expand Down Expand Up @@ -32,13 +32,10 @@ register<'function'>(
schema => ({
validate: async data => {
try {
return {valid: true, value: schema(data)};
return {data: schema(data)};
} catch (error) {
if (error instanceof Error) {
return {
errors: [new ValidationError(error.message)],
valid: false,
};
return {issues: [new ValidationIssue(error.message)]};
}
throw error;
}
Expand Down
11 changes: 5 additions & 6 deletions src/adapters/joi.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type {TypeSchemaResolver} from '../resolver';
import type {AnySchema} from 'joi';
import type {AnySchema, ValidationError} from 'joi';

import {register} from '../registry';
import {ValidationError} from '../schema';
import {ValidationIssue} from '../schema';
import {maybe} from '../utils';

interface JoiResolver extends TypeSchemaResolver {
Expand Down Expand Up @@ -33,13 +33,12 @@ register<'joi'>(
validate: async data => {
const result = schema.validate(data);
if (result.error == null) {
return {valid: true, value: result.value};
return {data: result.value};
}
return {
errors: result.error.details.map(
({message, path}) => new ValidationError(message, path),
issues: result.error.details.map(
({message, path}) => new ValidationIssue(message, path),
),
valid: false,
};
},
}),
Expand Down
9 changes: 3 additions & 6 deletions src/adapters/runtypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {TypeSchemaResolver} from '../resolver';
import type {Failure, Runtype, Static} from 'runtypes';

import {register} from '../registry';
import {ValidationError} from '../schema';
import {ValidationIssue} from '../schema';
import {maybe} from '../utils';

interface RuntypesResolver extends TypeSchemaResolver {
Expand Down Expand Up @@ -33,12 +33,9 @@ register<'runtypes'>(
validate: async data => {
const result = schema.validate(data);
if (result.success) {
return {valid: true, value: result.value};
return {data: result.value};
}
return {
errors: [new ValidationError(result.message)],
valid: false,
};
return {issues: [new ValidationIssue(result.message)]};
},
}),
);
9 changes: 3 additions & 6 deletions src/adapters/superstruct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {TypeSchemaResolver} from '../resolver';
import type {Infer, Struct, StructError} from 'superstruct';

import {register} from '../registry';
import {ValidationError} from '../schema';
import {ValidationIssue} from '../schema';
import {maybe} from '../utils';

interface SuperstructResolver extends TypeSchemaResolver {
Expand Down Expand Up @@ -33,13 +33,10 @@ register<'superstruct'>(
validate: async data => {
const result = schema.validate(data);
if (result[0] == null) {
return {valid: true, value: result[1]};
return {data: result[1]};
}
const {message, path} = result[0];
return {
errors: [new ValidationError(message, path)],
valid: false,
};
return {issues: [new ValidationIssue(message, path)]};
},
}),
);
9 changes: 4 additions & 5 deletions src/adapters/typebox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {Static, TSchema} from '@sinclair/typebox';
import type {TypeCheck} from '@sinclair/typebox/compiler';

import {register} from '../registry';
import {ValidationError} from '../schema';
import {ValidationIssue} from '../schema';
import {maybe} from '../utils';

type TypeBoxSchema<T> = TSchema & {static: T};
Expand Down Expand Up @@ -37,13 +37,12 @@ register<'typebox'>(
const {TypeCompiler} = await import('@sinclair/typebox/compiler');
const result = TypeCompiler.Compile(schema);
if (result.Check(data)) {
return {valid: true, value: data};
return {data};
}
return {
errors: [...result.Errors(data)].map(
({message, path}) => new ValidationError(message, [path]),
issues: [...result.Errors(data)].map(
({message, path}) => new ValidationIssue(message, [path]),
),
valid: false,
};
},
}),
Expand Down
Loading

0 comments on commit a02bdff

Please sign in to comment.