Skip to content

Commit

Permalink
Add serialization support to Joi
Browse files Browse the repository at this point in the history
  • Loading branch information
decs committed Mar 1, 2024
1 parent b5674eb commit 2cb436f
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 12 deletions.
7 changes: 7 additions & 0 deletions .changeset/eleven-ligers-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@typeschema/main": patch
"@typeschema/all": patch
"@typeschema/joi": patch
---

Add serialization support to Joi
2 changes: 1 addition & 1 deletion README.md

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

1 change: 1 addition & 0 deletions packages/all/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
"io-ts": "^2.2.21",
"io-ts-types": "^0.5.19",
"joi": "^17.12.0",
"joi-to-json": "^4.2.1",
"ajv": "^8.12.0",
"json-schema-to-ts": "^3.0.0",
"ow": "^0.28.2",
Expand Down
18 changes: 17 additions & 1 deletion packages/all/src/__tests__/joi.test.ts

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

3 changes: 3 additions & 0 deletions packages/joi/README.md

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

12 changes: 9 additions & 3 deletions packages/joi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,25 @@
},
"//devDependencies": "This field is manually maintained.",
"devDependencies": {
"joi": "^17.12.0"
"joi": "^17.12.0",
"joi-to-json": "^4.2.1"
},
"//peerDependencies": {
"//": "This field is manually maintained.",
"joi": "Required for validation"
"joi": "Required for validation",
"joi-to-json": "Required for serialization"
},
"peerDependencies": {
"joi": "^17.12.0"
"joi": "^17.12.0",
"joi-to-json": "^4.2.1"
},
"//peerDependenciesMeta": "This field is manually maintained.",
"peerDependenciesMeta": {
"joi": {
"optional": true
},
"joi-to-json": {
"optional": true
}
}
}
18 changes: 17 additions & 1 deletion packages/joi/src/__tests__/joi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {expectTypeOf} from 'expect-type';
import Joi from 'joi';
import {describe, expect, test} from 'vitest';

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

describe('joi', () => {
const schema = Joi.object({
Expand Down Expand Up @@ -67,4 +67,20 @@ describe('joi', () => {
const caller = createCaller({});
expect(await caller.hello(data)).toStrictEqual(data);
});

test('toJSONSchema', async () => {
expect(await toJSONSchema(schema)).toStrictEqual({
additionalProperties: false,
properties: {
age: {type: 'number'},
createdAt: {type: 'integer'},
email: {format: 'email', type: 'string'},
id: {type: 'string'},
name: {type: 'string'},
updatedAt: {type: 'integer'},
},
required: ['age', 'createdAt', 'email', 'id', 'name', 'updatedAt'],
type: 'object',
});
});
});
4 changes: 4 additions & 0 deletions packages/joi/src/index.ts

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

16 changes: 16 additions & 0 deletions packages/joi/src/serialization.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type {AdapterResolver} from './resolver';
import type {SerializationAdapter} from '@typeschema/core';

import {memoize} from '@typeschema/core';

const importSerializationModule = memoize(async () => {
const {default: parse} = await import('joi-to-json');
return {parse};
});

export const serializationAdapter: SerializationAdapter<
AdapterResolver
> = async schema => {
const {parse} = await importSerializationModule();
return parse(schema);
};
1 change: 1 addition & 0 deletions packages/main/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
"io-ts-types": "^0.5.19",
"@typeschema/joi": "workspace:*",
"joi": "^17.12.0",
"joi-to-json": "^4.2.1",
"@typeschema/json": "workspace:*",
"ajv": "^8.12.0",
"json-schema-to-ts": "^3.0.0",
Expand Down
18 changes: 17 additions & 1 deletion packages/main/src/__tests__/joi.test.ts

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

7 changes: 6 additions & 1 deletion packages/main/src/serialization.ts

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

33 changes: 29 additions & 4 deletions pnpm-lock.yaml

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

0 comments on commit 2cb436f

Please sign in to comment.