-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: expose FromSchema options (#22)
* feat: expose FromSchema options * test: add tests for external references * docs: update to add an example of how to use external references * Update README.md * Update README.md
- Loading branch information
1 parent
25cd92a
commit 357ff54
Showing
3 changed files
with
118 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { JsonSchemaToTsProvider } from '../index' | ||
import { expectAssignable, expectType } from 'tsd' | ||
import Fastify, { FastifyInstance, FastifyLoggerInstance, RawReplyDefaultExpression, RawRequestDefaultExpression, RawServerDefault } from 'fastify' | ||
import { FromSchema } from 'json-schema-to-ts' | ||
|
||
const addressSchema = { | ||
type: 'object', | ||
additionalProperties: false, | ||
properties: { | ||
line1: { type: 'string' }, | ||
line2: { type: 'string' }, | ||
city: { type: 'string' } | ||
}, | ||
required: ['line1', 'city'] | ||
} as const | ||
type Address = FromSchema<typeof addressSchema>; | ||
|
||
const userSchema = { | ||
type: 'object', | ||
additionalProperties: false, | ||
properties: { | ||
givenName: { type: 'string' }, | ||
familyName: { type: 'string' } | ||
}, | ||
required: ['givenName', 'familyName'] | ||
} as const | ||
type User = FromSchema<typeof userSchema> | ||
|
||
const sharedSchema = { | ||
$id: 'shared-schema', | ||
definitions: { | ||
address: addressSchema, | ||
user: userSchema | ||
} | ||
} as const | ||
|
||
type JsonSchemaToTsProviderWithSharedSchema = JsonSchemaToTsProvider<{ | ||
references: [typeof sharedSchema]; | ||
}>; | ||
const fastify = Fastify().withTypeProvider<JsonSchemaToTsProviderWithSharedSchema>() | ||
|
||
expectAssignable<FastifyInstance<RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, FastifyLoggerInstance, JsonSchemaToTsProviderWithSharedSchema>>(fastify) | ||
|
||
fastify.get('/profile', { | ||
schema: { | ||
body: { | ||
type: 'object', | ||
properties: { | ||
user: { | ||
$ref: 'shared-schema#/definitions/user' | ||
}, | ||
address: { | ||
$ref: 'shared-schema#/definitions/address' | ||
} | ||
}, | ||
required: ['user', 'address'] | ||
} as const | ||
} | ||
}, (req) => { | ||
expectType<User>(req.body.user) | ||
expectType<Address>(req.body.address) | ||
}) |