-
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.
Merge pull request #5 from Resetand/validate-type-improvements
improve type inference for validate
- Loading branch information
Showing
4 changed files
with
134 additions
and
69 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 |
---|---|---|
@@ -1,44 +1,110 @@ | ||
import { test, expect } from 'vitest'; | ||
import { test, expect, describe } from 'vitest'; | ||
import is, { validate, validateStrict } from '../src'; | ||
import { assertGuardedType } from './utils'; | ||
|
||
test('Should validate by schema shape (validate)', () => { | ||
const obj = { | ||
a: 1, | ||
b: 'string', | ||
c: { | ||
d: true, | ||
e: { | ||
f: 1, | ||
g: 'string', | ||
describe('Validate runtime tests', () => { | ||
test('Should validate by schema shape (validate)', () => { | ||
const obj = { | ||
a: 1, | ||
b: 'string', | ||
c: { | ||
d: true, | ||
e: { | ||
f: 1, | ||
g: 'string', | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const schema = { | ||
a: is.Number, | ||
b: is.$some(is.String, is.Nil), | ||
c: { | ||
d: is.Boolean, | ||
e: { | ||
f: is.Number, | ||
g: is.String, | ||
}; | ||
|
||
const schema = { | ||
a: is.Number, | ||
b: is.$some(is.String, is.Nil), | ||
c: { | ||
d: is.Boolean, | ||
e: { | ||
f: is.Number, | ||
g: is.String, | ||
}, | ||
}, | ||
}, | ||
}; | ||
}; | ||
|
||
expect(validate(obj, schema)).toBe(true); | ||
|
||
expect(validate(42, is.Number)).toBe(true); | ||
expect(validate(42, is.$some(is.Number, is.Nil))).toBe(true); | ||
|
||
expect(validate(42, is.String)).toBe(false); | ||
expect(validate({ a: 1 }, schema)).toBe(false); | ||
expect(validate('SOME STRING VALUE', schema)).toBe(false); | ||
|
||
expect(validate({ a: 1, otherParam: 'here' }, { a: is.Number })).toBe(true); | ||
expect(validateStrict({ a: 1, otherParam: 'here' }, { a: is.Number })).toBe(false); | ||
|
||
expect(validate([21, 2, 32], is.ArrayOf(is.Number))).toBe(true); | ||
expect(validate(is.ArrayOf(is.Number))([21, 2, 32])).toBe(true); | ||
|
||
expect(() => validate(42, 'UNKNOWN_SCHEMA' as any)).toThrowError(); | ||
}); | ||
|
||
expect(validate(obj, schema)).toBe(true); | ||
test('Should validate array schema', () => { | ||
const schema = [is.Number, is.String, is.Boolean]; | ||
|
||
expect(validate([1, '2', true], schema)).toBe(true); | ||
expect(validate([1, '2', true, 4], schema)).toBe(false); | ||
expect(validate([1, '2'], schema)).toBe(false); | ||
expect(validate([1, '2', 'true'], schema)).toBe(false); | ||
}); | ||
|
||
test('Should validate array schema (complex type)', () => { | ||
const schema = [is.Number, is.String, is.Boolean, [validate({ a: is.Number, b: is.String })]]; | ||
|
||
expect(validate([1, '2', true, [{ a: 1, b: '2' }]], schema)).toBe(true); | ||
expect(validate([1, '2', true, [{ a: 1, b: 2 }]], schema)).toBe(false); | ||
expect(validate([1, '2', true, [{ a: 1 }]], schema)).toBe(false); | ||
expect(validate([1, '2', true, [{ a: 1, b: '2', c: 3 }]], schema)).toBe(true); // extra properties are allowed | ||
}); | ||
}); | ||
|
||
describe('Validate static typing tests', () => { | ||
test('should infer guarded type from object schema', () => { | ||
const schema = { | ||
a: is.Number, | ||
b: is.$some(is.String, is.Nil), | ||
c: { | ||
d: is.Boolean, | ||
e: { | ||
f: is.Number, | ||
g: is.String, | ||
}, | ||
}, | ||
}; | ||
|
||
expect(validate(42, is.Number)).toBe(true); | ||
expect(validate(42, is.$some(is.Number, is.Nil))).toBe(true); | ||
type ExpectedType = { | ||
a: number; | ||
b: string | null | undefined; | ||
c: { | ||
d: boolean; | ||
e: { | ||
f: number; | ||
g: string; | ||
}; | ||
}; | ||
}; | ||
|
||
expect(validate(42, is.String)).toBe(false); | ||
expect(validate({ a: 1 }, schema)).toBe(false); | ||
assertGuardedType<ExpectedType>()(validate(schema)); | ||
assertGuardedType<ExpectedType>()(validateStrict(schema)); | ||
}); | ||
|
||
expect(validate({ a: 1, otherParam: 'here' }, { a: is.Number })).toBe(true); | ||
expect(validateStrict({ a: 1, otherParam: 'here' }, { a: is.Number })).toBe(false); | ||
test('should infer guarded type from array schema', () => { | ||
type ExpectedType = [number, string, boolean]; | ||
|
||
expect(validate([21, 2, 32], is.ArrayOf(is.Number))).toBe(true); | ||
expect(validate(is.ArrayOf(is.Number))([21, 2, 32])).toBe(true); | ||
assertGuardedType<ExpectedType>()(validate([is.Number, is.String, is.Boolean]), []); | ||
assertGuardedType<ExpectedType>()(validateStrict([is.Number, is.String, is.Boolean]), []); | ||
}); | ||
|
||
expect(() => validate(42, 'UNKNOWN_SCHEMA' as any)).toThrowError(); | ||
test('should infer guarded type from guard schema', () => { | ||
assertGuardedType<number>()(validate(is.Number)); | ||
assertGuardedType<string>()(validate(is.String)); | ||
assertGuardedType<string | null | undefined>()(validate(is.$some(is.String, is.Nil))); | ||
}); | ||
}); |