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: add support to fastest-validator #46

Merged
merged 3 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions .changeset/ten-dogs-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@typeschema/all": patch
"@typeschema/fastest-validator": minor
"@typeschema/main": patch
---

feat: add support to fastest-validator
6 changes: 6 additions & 0 deletions .gitattributes

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions packages/all/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"class-validator",
"deepkit",
"effect",
"fastest-validator",
"function",
"io-ts",
"joi",
Expand Down Expand Up @@ -78,6 +79,7 @@
"@typeschema/class-validator": "workspace:*",
"@typeschema/deepkit": "workspace:*",
"@typeschema/effect": "workspace:*",
"@typeschema/fastest-validator": "workspace:*",
"@typeschema/function": "workspace:*",
"@typeschema/io-ts": "workspace:*",
"@typeschema/joi": "workspace:*",
Expand All @@ -99,6 +101,7 @@
"@deepkit/type-compiler": "^1.0.1-alpha.142",
"@effect/schema": "^0.63.2",
"effect": "^2.4.1",
"fastest-validator": "^1.17.0",
"typia": "^5.5.3",
"fp-ts": "^2.16.2",
"io-ts": "^2.2.21",
Expand Down
70 changes: 70 additions & 0 deletions packages/all/src/__tests__/fastest-validator.test.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions packages/fastest-validator/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 74 additions & 0 deletions packages/fastest-validator/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"//": "This file is partially generated. Only some fields can be modified manually!",
"name": "@typeschema/fastest-validator",
"//version": "This field is manually maintained.",
"version": "0.0.0",
"//description": "This field is manually maintained.",
"description": "Reusable adapter for fastest-validator schemas",
"keywords": [
"typescript",
"type",
"schema",
"adapter",
"validation",
"inference",
"assert"
],
"homepage": "https://typeschema.com",
"license": "MIT",
"author": {
"name": "André Costa",
"email": "andrefonsecacosta@gmail.com"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
},
"files": [
"/dist"
],
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"exports": {
".": {
"import": {
"types": "./dist/index.d.mts",
"default": "./dist/index.mjs"
},
"require": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
},
"sideEffects": false,
"repository": {
"type": "git",
"url": "https://github.com/decs/typeschema.git"
},
"scripts": {
"build": "tsup --config ../../tsup.config.ts",
"lint": "eslint src --fix",
"lint:package": "publint && attw --pack",
"test": "vitest --config ../../vitest.config.ts",
"upgrade:deps": "ncu -u --dep=dev,peer --reject ow"
},
"dependencies": {
"@typeschema/core": "workspace:*"
},
"//devDependencies": "This field is manually maintained.",
"devDependencies": {
"fastest-validator": "^1.17.0"
},
"//peerDependencies": "This field is manually maintained.",
"peerDependencies": {
"fastest-validator": "^1.17.0"
},
"//peerDependenciesMeta": "This field is manually maintained.",
"peerDependenciesMeta": {
"fastest-validator": {
"optional": true
}
}
}
13 changes: 13 additions & 0 deletions packages/fastest-validator/src/__tests__/example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {initTRPC} from '@trpc/server';

import {wrap} from '..';

const schema = {name: 'string'};

const t = initTRPC.create();
const appRouter = t.router({
hello: t.procedure
.input(wrap(schema))
.query(({input}) => `Hello, ${(input as any).name}!`),
// ^? unknown
});
66 changes: 66 additions & 0 deletions packages/fastest-validator/src/__tests__/fastest-validator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import type {Infer, InferIn} from '..';

import {initTRPC} from '@trpc/server';
import {expectTypeOf} from 'expect-type';
import {describe, expect, test} from 'vitest';

import {assert, validate, wrap} from '..';

describe('fastest-validator', () => {
const schema = {
age: 'number',
createdAt: 'string',
email: 'string',
id: 'string',
name: 'string',
updatedAt: 'string',
};

const data = {
age: 123,
createdAt: '2021-01-01T00:00:00.000Z',
email: 'john.doe@test.com',
id: 'c4a760a8-dbcf-4e14-9f39-645a8e933d74',
name: 'John Doe',
updatedAt: '2021-01-01T00:00:00.000Z',
};
const badData = {
age: '123',
createdAt: '2021-01-01T00:00:00.000Z',
email: 'john.doe@test.com',
id: 'c4a760a8-dbcf-4e14-9f39-645a8e933d74',
name: 'John Doe',
updatedAt: '2021-01-01T00:00:00.000Z',
};

test('infer', () => {
expectTypeOf<Infer<typeof schema>>().toEqualTypeOf<unknown>();
expectTypeOf<InferIn<typeof schema>>().toEqualTypeOf<unknown>();
});

test('validate', async () => {
expect(await validate(schema, data)).toStrictEqual({data, success: true});
expect(await validate(schema, badData)).toStrictEqual({
issues: [{message: "The 'age' field must be a number.", path: ['age']}],
success: false,
});
});

test('assert', async () => {
expect(await assert(schema, data)).toStrictEqual(data);
await expect(assert(schema, badData)).rejects.toThrow();
});

test('wrap', async () => {
const tRPC = initTRPC.create();
const router = tRPC.router({
hello: tRPC.procedure.input(wrap(schema)).query(({input}) => {
expectTypeOf<typeof input>().toEqualTypeOf<unknown>();
return input;
}),
});
const createCaller = tRPC.createCallerFactory(router);
const caller = createCaller({});
expect(await caller.hello(data)).toStrictEqual(data);
});
});
5 changes: 5 additions & 0 deletions packages/fastest-validator/src/__tests__/tsconfig.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions packages/fastest-validator/src/index.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading