Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed intersection with inner exact of a empty interface #593

Open
wants to merge 2 commits into
base: 1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/modules/index.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -1827,7 +1827,7 @@ export const array = <C extends Mixed>(codec: C, name: string = `Array<${codec.n
name,
(u): u is Array<TypeOf<C>> => UnknownArray.is(u) && u.every(codec.is),
(u, c) =>
chain(UnknownArray.validate(u, c), us => ...
UnknownArray.validate(u, c).chain(us => ...
```

Added in v1.0.0
Expand Down
21 changes: 8 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1168,19 +1168,14 @@ export interface IntersectionC<CS extends [Mixed, Mixed, ...Array<Mixed>]>
> {}

const mergeAll = (base: any, us: Array<any>): any => {
let r: any = base
for (let i = 0; i < us.length; i++) {
const u = us[i]
if (u !== base) {
// `u` contains a prismatic value or is the result of a stripping combinator
if (r === base) {
r = Object.assign({}, u)
continue
}
for (const k in u) {
if (u[k] !== base[k] || !r.hasOwnProperty(k)) {
r[k] = u[k]
}
if (!us.some(u => u !== base)) {
return base
}
const r: any = {}
for (const u of us) {
for (const k in u) {
if (!r.hasOwnProperty(k) || u[k] !== base[k]) {
r[k] = u[k]
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions test/exact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ describe('exact', () => {
assertSuccess(T.decode({ foo: 'foo' }))
})

it('should succeed validating a valid value (intersection with inner exact of a empty interface)', () => {
const T = t.intersection([
t.type({ foo: t.string }),
t.exact(t.intersection([t.interface({}), t.partial({ bar: t.number })]))
])
assertSuccess(T.decode({ foo: 'foo', bar: 1 }))
assertSuccess(T.decode({ foo: 'foo', bar: undefined }))
assertSuccess(T.decode({ foo: 'foo' }), { foo: 'foo' })
})

it('should succeed validating a valid value (refinement)', () => {
// tslint:disable-next-line: deprecation
const T = t.exact(t.refinement(t.type({ foo: t.string }), p => p.foo.length > 2))
Expand Down