-
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.
- Loading branch information
1 parent
3ef2db7
commit 41347e6
Showing
7 changed files
with
162 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { test } from 'uvu' | ||
import assert from 'uvu/assert' | ||
import { object } from './object' | ||
import { number } from './number' | ||
import { isValid } from './isValid' | ||
import { allOf } from './allOf' | ||
import { explain } from './explain' | ||
|
||
test(`allOf allows specifying intersections`, function () { | ||
const T = allOf(object({ a: number }), object({ b: number })) | ||
|
||
const res1 = isValid(T, { a: 10, b: 20 }) | ||
const res2 = isValid(T, { a: 10, b: 20, c: 30 }) | ||
|
||
assert.is(res1, true) | ||
assert.is(res2, true) | ||
}) | ||
|
||
test('allOf rejects input values that are not coercible to all given types', function () { | ||
const T = allOf(object({ a: number }), object({ b: number })) | ||
|
||
const res1 = isValid(T, { a: 10 }) | ||
const res2 = isValid(T, { b: 20 }) | ||
const res3 = isValid(T, { c: 30 }) | ||
const res4 = isValid(T, 'hello') | ||
|
||
assert.is(res1, false) | ||
assert.is(res2, false) | ||
assert.is(res3, false) | ||
assert.is(res4, false) | ||
}) | ||
|
||
test(`there is an explanation if the input value is not coercibile to any given type`, function () { | ||
const T = allOf(object({ a: number }), object({ b: number })) | ||
|
||
const exp1 = explain(T, { a: 10 }) | ||
const exp2 = explain(T, { c: 30 }) | ||
|
||
assert.equal(exp1, { | ||
value: { a: 10 }, | ||
isNot: { allOf: [{ object: { a: 'number' } }, { object: { b: 'number' } }] }, | ||
since: [ | ||
{ | ||
value: { a: 10 }, | ||
isNot: { object: { b: 'number' } }, | ||
since: [{ missingKey: 'b' }], | ||
}, | ||
], | ||
}) | ||
|
||
assert.equal(exp2, { | ||
value: { c: 30 }, | ||
isNot: { allOf: [{ object: { a: 'number' } }, { object: { b: 'number' } }] }, | ||
since: [ | ||
{ | ||
value: { c: 30 }, | ||
isNot: { object: { a: 'number' } }, | ||
since: [{ missingKey: 'a' }], | ||
}, | ||
{ | ||
value: { c: 30 }, | ||
isNot: { object: { b: 'number' } }, | ||
since: [{ missingKey: 'b' }], | ||
}, | ||
], | ||
}) | ||
}) | ||
|
||
test.run() |
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,19 @@ | ||
import { Infer, Type } from './Type' | ||
|
||
export function allOf<T1 extends Type<unknown>, T2 extends Type<unknown>, Ts extends Type<unknown>[]>( | ||
first: T1, | ||
second: T2, | ||
...rest: Ts | ||
): Type<AllOf<[T1, T2, ...Ts]>> { | ||
const allOf = [first, second, ...rest].map((type) => type.schema) | ||
return { schema: { allOf } } | ||
} | ||
|
||
type AllOf<Types, P = unknown, O = unknown> = Types extends [infer Head, ...infer Rest] | ||
? Infer<Head> extends object | ||
? AllOf<Rest, P, Infer<Head> & O> | ||
: AllOf<Rest, Infer<Head> & P, O> | ||
: P & PrettifyObj<O> | ||
type PrettifyObj<T> = { | ||
[K in keyof T]: T[K] | ||
} & {} |
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