forked from sinclairzx81/typebox
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Required Undefined Property Check (sinclairzx81#331)
- Loading branch information
1 parent
5a520a1
commit 078cfac
Showing
18 changed files
with
493 additions
and
243 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { TypeSystem } from '@sinclair/typebox/system' | ||
|
||
TypeSystem.AllowNaN = true |
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,29 @@ | ||
/*-------------------------------------------------------------------------- | ||
@sinclair/typebox/extensions | ||
The MIT License (MIT) | ||
Copyright (c) 2017-2023 Haydn Paterson (sinclair) <[email protected]> | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
---------------------------------------------------------------------------*/ | ||
|
||
export * from './typedef' |
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,100 @@ | ||
# JSON Type Definition | ||
|
||
JSON Type Definition (JTD) specification is an alternative schema specification to JSON Schema that provides better support for nominal type systems. TypeBox doesn't provide JTD support by default, but can be expressed through unsafe types and validated with Ajv. | ||
|
||
This example provides a reference implementation for JSON Type Definition. | ||
|
||
## TypeDef | ||
|
||
Refer to the `typedef.ts` file in this directory for a reference implementation of the JSON Type Definition type builder. | ||
|
||
```typescript | ||
import { Static } from '@sinclair/typebox' | ||
import { TypeDef } from './typedef' | ||
``` | ||
|
||
## Properties | ||
|
||
```typescript | ||
// ------------------------------------------------------------------------ | ||
// PropertiesType | ||
// | ||
// https://jsontypedef.com/docs/jtd-in-5-minutes/#properties-schemas | ||
// | ||
// ------------------------------------------------------------------------ | ||
|
||
export type PropertiesType = Static<typeof PropertiesType> | ||
export const PropertiesType = TypeDef.Properties({ | ||
x: TypeDef.Float32(), | ||
y: TypeDef.Float32(), | ||
z: TypeDef.Float32(), | ||
}) | ||
``` | ||
|
||
## Values | ||
|
||
```typescript | ||
// ------------------------------------------------------------------------ | ||
// ValuesType | ||
// | ||
// https://jsontypedef.com/docs/jtd-in-5-minutes/#values-schemas | ||
// | ||
// ------------------------------------------------------------------------ | ||
|
||
export type ValuesType = Static<typeof ValuesType> | ||
export const ValuesType = TypeDef.Values(TypeDef.Float64()) | ||
``` | ||
|
||
## Enum | ||
|
||
```typescript | ||
// ------------------------------------------------------------------------ | ||
// EnumType | ||
// | ||
// https://jsontypedef.com/docs/jtd-in-5-minutes/#enum-schemas | ||
// | ||
// ------------------------------------------------------------------------ | ||
|
||
export type EnumType = Static<typeof EnumType> | ||
export const EnumType = TypeDef.Enum(['FOO', 'BAR', 'BAZ']) | ||
``` | ||
|
||
## Elements | ||
|
||
```typescript | ||
// ------------------------------------------------------------------------ | ||
// ElementsType | ||
// | ||
// https://jsontypedef.com/docs/jtd-in-5-minutes/#elements-schemas | ||
// | ||
// ------------------------------------------------------------------------ | ||
|
||
export type ElementsType = Static<typeof ElementsType> | ||
export const ElementsType = TypeDef.Elements(PropertiesType) | ||
``` | ||
|
||
## Union | ||
|
||
```typescript | ||
// ------------------------------------------------------------------------ | ||
// UnionType | ||
// | ||
// https://jsontypedef.com/docs/jtd-in-5-minutes/#discriminator-schemas | ||
// | ||
// ------------------------------------------------------------------------ | ||
|
||
export type UnionType = Static<typeof UnionType> | ||
export const UnionType = TypeDef.Union('eventType', { | ||
USER_CREATED: TypeDef.Properties({ | ||
id: TypeDef.String(), | ||
}), | ||
USER_PAYMENT_PLAN_CHANGED: TypeDef.Properties({ | ||
id: TypeDef.String(), | ||
plan: TypeDef.Enum(['FREE', 'PAID']), | ||
}), | ||
USER_DELETED: TypeDef.Properties({ | ||
id: TypeDef.String(), | ||
softDelete: TypeDef.Boolean(), | ||
}), | ||
}) | ||
``` |
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,88 @@ | ||
/*-------------------------------------------------------------------------- | ||
@sinclair/typebox/typedef | ||
The MIT License (MIT) | ||
Copyright (c) 2017-2023 Haydn Paterson (sinclair) <[email protected]> | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
---------------------------------------------------------------------------*/ | ||
|
||
import { Type, Static, TUnsafe } from '@sinclair/typebox' | ||
|
||
// ------------------------------------------------------------------------ | ||
// TypeDef Namespace | ||
// | ||
// https://jsontypedef.com/docs/jtd-in-5-minutes/ | ||
// ------------------------------------------------------------------------ | ||
|
||
export type StaticTypeDefUnion<D extends string, M extends Record<string, TUnsafe<any>>> = { [K in keyof M]: { [P in D]: K } & Static<M[K]> }[keyof M] | ||
|
||
export namespace TypeDef { | ||
export function Boolean() { | ||
return Type.Unsafe<boolean>({ type: 'boolean' }) | ||
} | ||
export function String() { | ||
return Type.Unsafe<string>({ type: 'string' }) | ||
} | ||
export function TimeStamp() { | ||
return Type.Unsafe<string>({ type: 'timestamp' }) | ||
} | ||
export function Float32() { | ||
return Type.Unsafe<number>({ type: 'float32' }) | ||
} | ||
export function Float64() { | ||
return Type.Unsafe<number>({ type: 'float64' }) | ||
} | ||
export function Int8() { | ||
return Type.Unsafe<number>({ type: 'int8' }) | ||
} | ||
export function Uint8() { | ||
return Type.Unsafe<number>({ type: 'uint8' }) | ||
} | ||
export function Int16() { | ||
return Type.Unsafe<number>({ type: 'int16' }) | ||
} | ||
export function Uint16() { | ||
return Type.Unsafe<number>({ type: 'uint16' }) | ||
} | ||
export function Int32() { | ||
return Type.Unsafe<number>({ type: 'int32' }) | ||
} | ||
export function Uint32() { | ||
return Type.Unsafe<number>({ type: 'uint32' }) | ||
} | ||
export function Enum<T extends string[]>(values: [...T]) { | ||
return Type.Unsafe<T[number]>({ enum: values }) | ||
} | ||
export function Elements<T extends TUnsafe<any>>(element: T) { | ||
return Type.Unsafe<Array<Static<T>>>({ elements: element }) | ||
} | ||
export function Properties<T extends Record<string, TUnsafe<any>>>(properties: T) { | ||
return Type.Unsafe<{ [K in keyof T]: Static<T[K]> }>({ properties }) | ||
} | ||
export function Values<V extends TUnsafe<any>>(values: V) { | ||
return Type.Unsafe<Record<string, Static<V>>>({ values }) | ||
} | ||
export function Union<D extends string, M extends Record<string, TUnsafe<any>>>(discriminator: D, mapping: M) { | ||
return Type.Unsafe<StaticTypeDefUnion<D, M>>({ discriminator, mapping }) | ||
} | ||
} |
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
Oops, something went wrong.