https://github.com/ptpt/json-schema-ts
JSON Schema for TypeScript
https://github.com/ptpt/json-schema-ts
json-schema typescript
Last synced: about 2 months ago
JSON representation
JSON Schema for TypeScript
- Host: GitHub
- URL: https://github.com/ptpt/json-schema-ts
- Owner: ptpt
- License: mit
- Created: 2018-12-18T09:49:14.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2021-12-25T22:56:04.000Z (over 4 years ago)
- Last Synced: 2026-04-06T11:06:26.711Z (3 months ago)
- Topics: json-schema, typescript
- Language: TypeScript
- Homepage:
- Size: 27.3 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# JSON Schema for TypeScript
This package provides:
1. JSON schema types for TypeScript (in namespace `t`)
2. a builder for building JSON schema (in namespace `s`)
3. infer the type given the JSON schema built by the builder (with `t.TSType`)
## Installation
```
npm install json-schema-ts
```
## Examples
```typescript
import {s, t} from 'json-schema-ts';
const sex = s.string<'male' | 'female'>({'enum': ['male', 'female']});
// personSchema is a valid JSON schema
const personSchema = s.object({
'title': 'person',
'description': 'Person information',
'definitions': {
'sex': sex,
},
'properties': {
'name': s.string(),
'fullName': s.object({'properties': {
'firstName': s.string(),
'lastName': s.string(),
}}),
'age': s.number(),
'friends': s.array({'items': s.string()}),
'sex': s.ref('#/definitions/sex'),
'location': s.oneOf(
s.string(),
s.tuple({'items': s.items(s.number(), s.number())}),
)
}
});
// Infer IPerson from personSchema
type IPerson = t.TSType;
// IPerson is equivalent to
// interface IPerson {
// name: string;
// fullName: {
// firstName: string,
// lastName: string,
// };
// age: number;
// friends: string[];
// sex: 'male' | 'female';
// location: string | [number, number];
// }
```
## Development
```
npx tsc -w
```
## License
MIT. See LICENSE.