-
Notifications
You must be signed in to change notification settings - Fork 167
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
18d1cf7
commit 824bd88
Showing
9 changed files
with
229 additions
and
7 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
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,80 @@ | ||
import { Type } from '@sinclair/typebox' | ||
import { Ok, Fail } from './validate' | ||
|
||
describe('compiler/Constructor', () => { | ||
it('Should validate constructor 1', () => { | ||
const T = Type.Constructor([], Type.Object({})) | ||
Ok(T, class {}) | ||
}) | ||
it('Should validate constructor 2', () => { | ||
const T = Type.Constructor([Type.Number()], Type.Object({})) | ||
// note: constructor arguments are non-checkable | ||
Ok(T, class {}) | ||
}) | ||
it('Should validate constructor 3', () => { | ||
const T = Type.Constructor( | ||
[Type.Number()], | ||
Type.Object({ | ||
method: Type.Function([], Type.Void()), | ||
}), | ||
) | ||
Ok( | ||
T, | ||
class { | ||
method() {} | ||
}, | ||
) | ||
}) | ||
it('Should validate constructor 4', () => { | ||
const T = Type.Constructor( | ||
[Type.Number()], | ||
Type.Object({ | ||
x: Type.Number(), | ||
y: Type.Number(), | ||
z: Type.Number(), | ||
}), | ||
) | ||
Ok( | ||
T, | ||
class { | ||
get x() { | ||
return 1 | ||
} | ||
get y() { | ||
return 1 | ||
} | ||
get z() { | ||
return 1 | ||
} | ||
}, | ||
) | ||
}) | ||
it('Should not validate constructor 1', () => { | ||
const T = Type.Constructor([Type.Number()], Type.Object({})) | ||
Fail(T, 1) | ||
}) | ||
it('Should not validate constructor 2', () => { | ||
const T = Type.Constructor( | ||
[Type.Number()], | ||
Type.Object({ | ||
x: Type.Number(), | ||
y: Type.Number(), | ||
z: Type.Number(), | ||
}), | ||
) | ||
Fail( | ||
T, | ||
class { | ||
get x() { | ||
return null | ||
} | ||
get y() { | ||
return null | ||
} | ||
get z() { | ||
return null | ||
} | ||
}, | ||
) | ||
}) | ||
}) |
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,25 @@ | ||
import { Type } from '@sinclair/typebox' | ||
import { Ok, Fail } from './validate' | ||
|
||
describe('compiler/Function', () => { | ||
it('Should validate function 1', () => { | ||
const T = Type.Function([Type.Number()], Type.Number()) | ||
Ok(T, function () {}) | ||
}) | ||
it('Should validate function 2', () => { | ||
const T = Type.Function([Type.Number()], Type.Number()) | ||
// note: validation only checks typeof 'function' | ||
Ok(T, function (a: string, b: string, c: string, d: string) {}) | ||
}) | ||
it('Should validate function 3', () => { | ||
const T = Type.Function([Type.Number()], Type.Number()) | ||
// note: validation only checks typeof 'function' | ||
Ok(T, function () { | ||
return 'not-a-number' | ||
}) | ||
}) | ||
it('Should not validate function', () => { | ||
const T = Type.Function([Type.Number()], Type.Number()) | ||
Fail(T, 1) | ||
}) | ||
}) |
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,87 @@ | ||
import { Value } from '@sinclair/typebox/value' | ||
import { Type } from '@sinclair/typebox' | ||
import { Assert } from '../../assert/index' | ||
|
||
describe('value/check/Constructor', () => { | ||
it('Should validate constructor 1', () => { | ||
const T = Type.Constructor([], Type.Object({})) | ||
Assert.IsTrue(Value.Check(T, class {})) | ||
}) | ||
it('Should validate constructor 2', () => { | ||
const T = Type.Constructor([Type.Number()], Type.Object({})) | ||
// note: constructor arguments are non-checkable | ||
Assert.IsTrue(Value.Check(T, class {})) | ||
}) | ||
it('Should validate constructor 3', () => { | ||
const T = Type.Constructor( | ||
[Type.Number()], | ||
Type.Object({ | ||
method: Type.Function([], Type.Void()), | ||
}), | ||
) | ||
Assert.IsTrue( | ||
Value.Check( | ||
T, | ||
class { | ||
method() {} | ||
}, | ||
), | ||
) | ||
}) | ||
it('Should validate constructor 4', () => { | ||
const T = Type.Constructor( | ||
[Type.Number()], | ||
Type.Object({ | ||
x: Type.Number(), | ||
y: Type.Number(), | ||
z: Type.Number(), | ||
}), | ||
) | ||
Assert.IsTrue( | ||
Value.Check( | ||
T, | ||
class { | ||
get x() { | ||
return 1 | ||
} | ||
get y() { | ||
return 1 | ||
} | ||
get z() { | ||
return 1 | ||
} | ||
}, | ||
), | ||
) | ||
}) | ||
it('Should not validate constructor 1', () => { | ||
const T = Type.Constructor([Type.Number()], Type.Object({})) | ||
Assert.IsFalse(Value.Check(T, 1)) | ||
}) | ||
it('Should not validate constructor 2', () => { | ||
const T = Type.Constructor( | ||
[Type.Number()], | ||
Type.Object({ | ||
x: Type.Number(), | ||
y: Type.Number(), | ||
z: Type.Number(), | ||
}), | ||
) | ||
Assert.IsFalse( | ||
Value.Check( | ||
T, | ||
class { | ||
get x() { | ||
return null | ||
} | ||
get y() { | ||
return null | ||
} | ||
get z() { | ||
return null | ||
} | ||
}, | ||
), | ||
) | ||
}) | ||
}) |
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,28 @@ | ||
import { Value } from '@sinclair/typebox/value' | ||
import { Type } from '@sinclair/typebox' | ||
import { Assert } from '../../assert/index' | ||
|
||
describe('value/check/Function', () => { | ||
it('Should validate function 1', () => { | ||
const T = Type.Function([Type.Number()], Type.Number()) | ||
Assert.IsTrue(Value.Check(T, function () {})) | ||
}) | ||
it('Should validate function 2', () => { | ||
const T = Type.Function([Type.Number()], Type.Number()) | ||
// note: validation only checks typeof 'function' | ||
Assert.IsTrue(Value.Check(T, function (a: string, b: string, c: string, d: string) {})) | ||
}) | ||
it('Should validate function 3', () => { | ||
const T = Type.Function([Type.Number()], Type.Number()) | ||
// note: validation only checks typeof 'function' | ||
Assert.IsTrue( | ||
Value.Check(T, function () { | ||
return 'not-a-number' | ||
}), | ||
) | ||
}) | ||
it('Should not validate function', () => { | ||
const T = Type.Function([Type.Number()], Type.Number()) | ||
Assert.IsFalse(Value.Check(T, 1)) | ||
}) | ||
}) |
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