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

Improve interactions between recursive and exact types. #390

Open
wants to merge 1 commit into
base: master
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions docs/modules/index.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ parent: Modules
- [Errors (interface)](#errors-interface)
- [ExactC (interface)](#exactc-interface)
- [~~FunctionC~~ (interface)](#functionc-interface)
- [HasPropsExact (interface)](#haspropsexact-interface)
- [HasPropsIntersection (interface)](#haspropsintersection-interface)
- [HasPropsReadonly (interface)](#haspropsreadonly-interface)
- [HasPropsRecursive (interface)](#haspropsrecursive-interface)
- [HasPropsRefinement (interface)](#haspropsrefinement-interface)
- [IntBrand (interface)](#intbrand-interface)
- [IntersectionC (interface)](#intersectionc-interface)
Expand Down Expand Up @@ -321,6 +323,16 @@ export interface FunctionC extends FunctionType {}

Added in v1.5.3

# HasPropsExact (interface)

**Signature**

```ts
export interface HasPropsExact extends ExactType<HasProps, any, any, any> {}
```

Added in v2.1.0

# HasPropsIntersection (interface)

**Signature**
Expand All @@ -341,6 +353,16 @@ export interface HasPropsReadonly extends ReadonlyType<HasProps, any, any, any>

Added in v1.1.0

# HasPropsRecursive (interface)

**Signature**

```ts
export interface HasPropsRecursive extends RecursiveType<HasProps, any, any, any> {}
```

Added in v2.1.0

# HasPropsRefinement (interface)

**Signature**
Expand Down Expand Up @@ -798,6 +820,8 @@ export type HasProps =
| HasPropsRefinement
| HasPropsReadonly
| HasPropsIntersection
| HasPropsRecursive
| HasPropsExact
| InterfaceType<any, any, any, any>
// tslint:disable-next-line: deprecation
| StrictType<any, any, any, any>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "io-ts",
"version": "2.0.1",
"version": "2.1.0",
"description": "TypeScript compatible runtime type system for IO validation",
"files": [
"lib",
Expand Down
24 changes: 21 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1541,13 +1541,23 @@ export interface HasPropsReadonly extends ReadonlyType<HasProps, any, any, any>
* @since 1.1.0
*/
export interface HasPropsIntersection extends IntersectionType<Array<HasProps>, any, any, any> {}
/**
* @since 2.1.0
*/
export interface HasPropsRecursive extends RecursiveType<HasProps, any, any, any> {}
/**
* @since 2.1.0
*/
export interface HasPropsExact extends ExactType<HasProps, any, any, any> {}
/**
* @since 1.1.0
*/
export type HasProps =
| HasPropsRefinement
| HasPropsReadonly
| HasPropsIntersection
| HasPropsRecursive
| HasPropsExact
| InterfaceType<any, any, any, any>
// tslint:disable-next-line: deprecation
| StrictType<any, any, any, any>
Expand All @@ -1557,6 +1567,8 @@ const getProps = (codec: HasProps): Props => {
switch (codec._tag) {
case 'RefinementType':
case 'ReadonlyType':
case 'ExactType':
case 'RecursiveType':
return getProps(codec.type)
case 'InterfaceType':
case 'StrictType':
Expand Down Expand Up @@ -1601,12 +1613,18 @@ const getExactTypeName = (codec: Any): string => {
* @since 1.1.0
*/
export const exact = <C extends HasProps>(codec: C, name: string = getExactTypeName(codec)): ExactC<C> => {
const props: Props = getProps(codec)
const props = (() => {
let p: Props | undefined
return () => {
p = p || getProps(codec)
return p
}
})()
return new ExactType(
name,
codec.is,
(u, c) => chain(UnknownRecord.validate(u, c), () => map(codec.validate(u, c), a => stripKeys(a, props))),
a => codec.encode(stripKeys(a, props)),
(u, c) => chain(UnknownRecord.validate(u, c), () => map(codec.validate(u, c), a => stripKeys(a, props()))),
a => codec.encode(stripKeys(a, props())),
codec
)
}
Expand Down
17 changes: 17 additions & 0 deletions test/exact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,23 @@ describe('exact', () => {
const T = t.exact(t.readonly(t.type({ foo: t.string })))
assertSuccess(T.decode({ foo: 'foo', bar: 1 }), { foo: 'foo' })
})

it('should strip additional properties (recursive)', () => {
type C = t.PartialC<{ rec: R }>
type R = t.ExactC<t.RecursiveType<C, t.TypeOf<C>, t.OutputOf<C>>>
const T: R = t.exact(t.recursion('T', () => t.partial({ rec: T })))
assertSuccess(T.decode({ a: 1, rec: { b: 2, rec: {} } }), { rec: { rec: {} } })
})

it('should strip additional properties (mutually recursive)', () => {
type C1 = t.PartialC<{ a: R2 }>
type C2 = t.PartialC<{ b: R1 }>
type R1 = t.ExactC<t.RecursiveType<C1, t.TypeOf<C1>, t.OutputOf<C1>>>
type R2 = t.ExactC<t.RecursiveType<C2, t.TypeOf<C2>, t.OutputOf<C2>>>
const T1: R1 = t.exact(t.recursion('T1', () => t.partial({ a: T2 })))
const T2: R2 = t.exact(t.recursion('T2', () => t.partial({ b: T1 })))
assertSuccess(T1.decode({ x: 1, a: { y: 2, b: { z: 3, a: {} } } }), { a: { b: { a: {} } } })
})
})

describe('encode', () => {
Expand Down