Skip to content

Commit

Permalink
Add serialization support to Effect
Browse files Browse the repository at this point in the history
  • Loading branch information
decs committed Mar 2, 2024
1 parent 8fa21a3 commit 75dd140
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 13 deletions.
7 changes: 7 additions & 0 deletions .changeset/breezy-pugs-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@typeschema/effect": patch
"@typeschema/main": patch
"@typeschema/all": patch
---

Add serialization support to Effect
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.

60 changes: 57 additions & 3 deletions packages/all/src/__tests__/effect.test.ts

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

4 changes: 3 additions & 1 deletion packages/effect/README.md

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

2 changes: 1 addition & 1 deletion packages/effect/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
},
"//peerDependencies": {
"//": "This field is manually maintained.",
"@effect/schema": "Required for inference and validation",
"@effect/schema": "Required for inference, validation, and serialization",
"effect": "Required for inference and validation"
},
"peerDependencies": {
Expand Down
60 changes: 57 additions & 3 deletions packages/effect/src/__tests__/effect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,27 @@ import {initTRPC} from '@trpc/server';
import {expectTypeOf} from 'expect-type';
import {describe, expect, test} from 'vitest';

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

const readonly = <A extends Record<string, unknown>>(a: A): Readonly<A> => a;

const DateFromString = S.DateFromString.pipe(
S.jsonSchema({
type: 'string',
description: 'an ISO-date string',
title: 'ISOString',
format: 'date-time',
}),
);

describe('effect', () => {
const schema = S.struct({
age: S.number,
createdAt: S.DateFromString,
createdAt: DateFromString,
email: S.string,
id: S.string,
name: S.string,
updatedAt: S.DateFromString,
updatedAt: DateFromString,
});

const data = readonly({
Expand Down Expand Up @@ -83,4 +92,49 @@ describe('effect', () => {
const caller = createCaller({});
expect(await caller.hello(data)).toStrictEqual(outputData);
});

test('toJSONSchema', async () => {
expect(await toJSONSchema(schema)).toStrictEqual({
$defs: {
DateFromString: {
description: 'an ISO-date string',
format: 'date-time',
title: 'ISOString',
type: 'string',
},
},
$schema: 'http://json-schema.org/draft-07/schema#',
additionalProperties: false,
properties: {
age: {
description: 'a number',
title: 'number',
type: 'number',
},
createdAt: {
$ref: '#/$defs/DateFromString',
},
email: {
description: 'a string',
title: 'string',
type: 'string',
},
id: {
description: 'a string',
title: 'string',
type: 'string',
},
name: {
description: 'a string',
title: 'string',
type: 'string',
},
updatedAt: {
$ref: '#/$defs/DateFromString',
},
},
required: ['age', 'email', 'id', 'name', 'createdAt', 'updatedAt'],
type: 'object',
});
});
});
4 changes: 4 additions & 0 deletions packages/effect/src/index.ts

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

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

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

const importSerializationModule = memoize(async () => {
const {make} = await import('@effect/schema/JSONSchema');
return {make};
});

export const serializationAdapter: SerializationAdapter<
AdapterResolver
> = async schema => {
const {make} = await importSerializationModule();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return make(schema) as any;
};
60 changes: 57 additions & 3 deletions packages/main/src/__tests__/effect.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.

0 comments on commit 75dd140

Please sign in to comment.