Skip to content

Commit

Permalink
Respect refiner in dynamic and lazy (#966)
Browse files Browse the repository at this point in the history
* Respect refiner in `dynamic`

Provide a refiner in the result of the `dynamic` utility that is a proxy to the
results of the dynamically returned struct.

Fixes #830

* Respect refiner in `lazy`

Provide a refiner in the struct returned from the `lazy` utility. It proxies the
call to the lazily resolved struct.

Related to #830
  • Loading branch information
Gelio committed Mar 2, 2022
1 parent 6615f4c commit e2775d9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/structs/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ export function dynamic<T>(
const struct = fn(value, ctx)
return struct.coercer(value, ctx)
},
refiner(value, ctx) {
const struct = fn(value, ctx)
return struct.refiner(value, ctx)
},
})
}

Expand Down Expand Up @@ -145,6 +149,10 @@ export function lazy<T>(fn: () => Struct<T, any>): Struct<T, null> {
struct ??= fn()
return struct.coercer(value, ctx)
},
refiner(value, ctx) {
struct ??= fn()
return struct.refiner(value, ctx)
},
})
}

Expand Down
15 changes: 15 additions & 0 deletions test/validation/dynamic/with-refiners.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { dynamic, string, nonempty } from '../../..'

export const Struct = dynamic(() => nonempty(string()))

export const data = ''

export const failures = [
{
value: data,
type: 'string',
refinement: 'nonempty',
path: [],
branch: [data],
},
]
15 changes: 15 additions & 0 deletions test/validation/lazy/with-refiners.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { lazy, nonempty, string } from '../../..'

export const Struct = lazy(() => nonempty(string()))

export const data = ''

export const failures = [
{
value: data,
type: 'string',
refinement: 'nonempty',
path: [],
branch: [data],
},
]

0 comments on commit e2775d9

Please sign in to comment.