-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
93547e5
commit b764ef3
Showing
19 changed files
with
244 additions
and
115 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,72 @@ | ||
import { TypeRegistry, Type, Kind, TSchema } from '@sinclair/typebox' | ||
import { TypeCompiler } from '@sinclair/typebox/compiler' | ||
import { Ok, Fail } from './validate' | ||
import { Assert } from '../assert' | ||
|
||
describe('type/compiler/Kind', () => { | ||
// ------------------------------------------------------------ | ||
// Fixtures | ||
// ------------------------------------------------------------ | ||
beforeEach(() => TypeRegistry.Set('PI', (_, value) => value === Math.PI)) | ||
afterEach(() => TypeRegistry.Delete('PI')) | ||
// ------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------ | ||
it('Should validate', () => { | ||
const T = Type.Unsafe({ [Kind]: 'PI' }) | ||
Ok(T, Math.PI) | ||
}) | ||
it('Should not validate', () => { | ||
const T = Type.Unsafe({ [Kind]: 'PI' }) | ||
Fail(T, Math.PI * 2) | ||
}) | ||
it('Should validate in object', () => { | ||
const T = Type.Object({ | ||
x: Type.Unsafe({ [Kind]: 'PI' }), | ||
}) | ||
Ok(T, { x: Math.PI }) | ||
}) | ||
it('Should not validate in object', () => { | ||
const T = Type.Object({ | ||
x: Type.Unsafe({ [Kind]: 'PI' }), | ||
}) | ||
Fail(T, { x: Math.PI * 2 }) | ||
}) | ||
it('Should validate in array', () => { | ||
const T = Type.Array(Type.Unsafe({ [Kind]: 'PI' })) | ||
Ok(T, [Math.PI]) | ||
}) | ||
it('Should not validate in array', () => { | ||
const T = Type.Array(Type.Unsafe({ [Kind]: 'PI' })) | ||
Fail(T, [Math.PI * 2]) | ||
}) | ||
it('Should validate in tuple', () => { | ||
const T = Type.Tuple([Type.Unsafe({ [Kind]: 'PI' })]) | ||
Ok(T, [Math.PI]) | ||
}) | ||
it('Should not validate in tuple', () => { | ||
const T = Type.Tuple([Type.Unsafe({ [Kind]: 'PI' })]) | ||
Fail(T, [Math.PI * 2]) | ||
}) | ||
// ------------------------------------------------------------ | ||
// Instances | ||
// ------------------------------------------------------------ | ||
it('Should receive kind instance on registry callback', () => { | ||
const stack: string[] = [] | ||
TypeRegistry.Set('Kind', (schema: unknown) => { | ||
// prettier-ignore | ||
return (typeof schema === 'object' && schema !== null && Kind in schema && schema[Kind] === 'Kind' && '$id' in schema && typeof schema.$id === 'string') | ||
? (() => { stack.push(schema.$id); return true })() | ||
: false | ||
}) | ||
const A = { [Kind]: 'Kind', $id: 'A' } as TSchema | ||
const B = { [Kind]: 'Kind', $id: 'B' } as TSchema | ||
const T = Type.Object({ a: A, b: B }) | ||
const C = TypeCompiler.Compile(T) | ||
const R = C.Check({ a: null, b: null }) | ||
Assert.IsTrue(R) | ||
Assert.IsEqual(stack[0], 'A') | ||
Assert.IsEqual(stack[1], 'B') | ||
TypeRegistry.Delete('Kind') | ||
}) | ||
}) |
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,13 @@ | ||
import { TypeGuard, Kind } from '@sinclair/typebox' | ||
import { Assert } from '../../assert/index' | ||
|
||
describe('type/guard/TKind', () => { | ||
it('Should guard 1', () => { | ||
const T = { [Kind]: 'Kind' } | ||
Assert.IsTrue(TypeGuard.TKind(T)) | ||
}) | ||
it('Should guard 2', () => { | ||
const T = {} | ||
Assert.IsFalse(TypeGuard.TKind(T)) | ||
}) | ||
}) |
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 was deleted.
Oops, something went wrong.
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,70 @@ | ||
import { Value } from '@sinclair/typebox/value' | ||
import { TypeRegistry, Type, Kind, TSchema } from '@sinclair/typebox' | ||
import { Assert } from '../../assert' | ||
|
||
describe('value/check/Kind', () => { | ||
// ------------------------------------------------------------ | ||
// Fixtures | ||
// ------------------------------------------------------------ | ||
beforeEach(() => TypeRegistry.Set('PI', (_, value) => value === Math.PI)) | ||
afterEach(() => TypeRegistry.Delete('PI')) | ||
// ------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------ | ||
it('Should validate', () => { | ||
const T = Type.Unsafe({ [Kind]: 'PI' }) | ||
Assert.IsTrue(Value.Check(T, Math.PI)) | ||
}) | ||
it('Should not validate', () => { | ||
const T = Type.Unsafe({ [Kind]: 'PI' }) | ||
Assert.IsFalse(Value.Check(T, Math.PI * 2)) | ||
}) | ||
it('Should validate in object', () => { | ||
const T = Type.Object({ | ||
x: Type.Unsafe({ [Kind]: 'PI' }), | ||
}) | ||
Assert.IsTrue(Value.Check(T, { x: Math.PI })) | ||
}) | ||
it('Should not validate in object', () => { | ||
const T = Type.Object({ | ||
x: Type.Unsafe({ [Kind]: 'PI' }), | ||
}) | ||
Assert.IsFalse(Value.Check(T, { x: Math.PI * 2 })) | ||
}) | ||
it('Should validate in array', () => { | ||
const T = Type.Array(Type.Unsafe({ [Kind]: 'PI' })) | ||
Assert.IsTrue(Value.Check(T, [Math.PI])) | ||
}) | ||
it('Should not validate in array', () => { | ||
const T = Type.Array(Type.Unsafe({ [Kind]: 'PI' })) | ||
Assert.IsFalse(Value.Check(T, [Math.PI * 2])) | ||
}) | ||
it('Should validate in tuple', () => { | ||
const T = Type.Tuple([Type.Unsafe({ [Kind]: 'PI' })]) | ||
Assert.IsTrue(Value.Check(T, [Math.PI])) | ||
}) | ||
it('Should not validate in tuple', () => { | ||
const T = Type.Tuple([Type.Unsafe({ [Kind]: 'PI' })]) | ||
Assert.IsFalse(Value.Check(T, [Math.PI * 2])) | ||
}) | ||
// ------------------------------------------------------------ | ||
// Instances | ||
// ------------------------------------------------------------ | ||
it('Should receive kind instance on registry callback', () => { | ||
const stack: string[] = [] | ||
TypeRegistry.Set('Kind', (schema: unknown) => { | ||
// prettier-ignore | ||
return (typeof schema === 'object' && schema !== null && Kind in schema && schema[Kind] === 'Kind' && '$id' in schema && typeof schema.$id === 'string') | ||
? (() => { stack.push(schema.$id); return true })() | ||
: false | ||
}) | ||
const A = { [Kind]: 'Kind', $id: 'A' } as TSchema | ||
const B = { [Kind]: 'Kind', $id: 'B' } as TSchema | ||
const T = Type.Object({ a: A, b: B }) | ||
const R = Value.Check(T, { a: null, b: null }) | ||
Assert.IsTrue(R) | ||
Assert.IsEqual(stack[0], 'A') | ||
Assert.IsEqual(stack[1], 'B') | ||
TypeRegistry.Delete('Kind') | ||
}) | ||
}) |
This file was deleted.
Oops, something went wrong.
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.