-
Notifications
You must be signed in to change notification settings - Fork 1
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 #34 from paperhive/feature/discrimiated-union
add discriminatedUnion()
- Loading branch information
Showing
4 changed files
with
128 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { assert } from 'chai' | ||
|
||
import { branchError, leafError } from './errors' | ||
import { failure, success } from './result' | ||
import { discriminatedUnion } from './discriminated-union' | ||
import { number } from './number' | ||
import { optional } from './object' | ||
|
||
describe('discriminatedUnion()', () => { | ||
const validateVehicle = discriminatedUnion('type', { | ||
person: { age: number(), height: optional(number()) }, | ||
robot: { battery: number() }, | ||
}) | ||
|
||
it('should return an error if not an object', () => | ||
assert.deepStrictEqual( | ||
validateVehicle(1), | ||
failure(leafError(1, 'Not an object.')) | ||
)) | ||
|
||
it('should return an error if type does not exist.', () => { | ||
const value = { type: 'cat', age: 12 } | ||
assert.deepStrictEqual( | ||
validateVehicle(value), | ||
failure( | ||
branchError(value, [ | ||
{ key: 'type', error: leafError('cat', 'Not one of person, robot.') }, | ||
]) | ||
) | ||
) | ||
}) | ||
|
||
it('should return an error if key does not exist for type.', () => { | ||
const value = { type: 'robot', age: 12 } | ||
assert.deepStrictEqual( | ||
validateVehicle(value), | ||
failure(leafError(value, 'Properties not allowed: age.')) | ||
) | ||
}) | ||
|
||
it('should return an error if key does not validate.', () => { | ||
const value = { type: 'person', age: 'foo' } | ||
assert.deepStrictEqual( | ||
validateVehicle(value), | ||
failure( | ||
branchError(value, [ | ||
{ key: 'age', error: leafError('foo', 'Not a number.') }, | ||
]) | ||
) | ||
) | ||
}) | ||
|
||
it('should validate a person and a robot', () => { | ||
assert.deepStrictEqual( | ||
validateVehicle({ type: 'person', age: 2 }), | ||
success({ type: 'person' as const, age: 2 }) | ||
) | ||
assert.deepStrictEqual( | ||
validateVehicle({ type: 'person', age: 2, height: 180 }), | ||
success({ type: 'person' as const, age: 2, height: 180 }) | ||
) | ||
assert.deepStrictEqual( | ||
validateVehicle({ type: 'robot', battery: 99 }), | ||
success({ type: 'robot' as const, battery: 99 }) | ||
) | ||
}) | ||
}) |
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,50 @@ | ||
import { enumerate } from './enumerate' | ||
import { FefeError } from './errors' | ||
import { object, ObjectDefinition, ObjectOptions, ObjectResult } from './object' | ||
import { isFailure } from './result' | ||
import { Validator } from './transformer' | ||
|
||
export type DiscriminatedUnionDefinition = Record<string, ObjectDefinition> | ||
|
||
export type DiscriminatedUnionResult< | ||
K extends string, | ||
D extends DiscriminatedUnionDefinition | ||
> = { | ||
[k in keyof D]: ObjectResult<D[k]> & { [l in K]: k } | ||
}[keyof D] | ||
|
||
export function discriminatedUnion< | ||
K extends string, | ||
D extends DiscriminatedUnionDefinition | ||
>( | ||
key: K, | ||
definition: D, | ||
options: ObjectOptions = {} | ||
): Validator<DiscriminatedUnionResult<K, D>, FefeError> { | ||
const prevalidate = object( | ||
{ | ||
[key]: enumerate(...Object.keys(definition)), | ||
}, | ||
{ allowExcessProperties: true } | ||
) as Validator<{ [l in K]: keyof D }> | ||
|
||
const validators = Object.fromEntries( | ||
Object.entries(definition).map(([k, v]) => [ | ||
k, | ||
object( | ||
{ | ||
[key]: enumerate(k), | ||
...v, | ||
}, | ||
options | ||
), | ||
]) | ||
) as { [k in keyof D]: Validator<ObjectResult<D[k]> & { [l in K]: k }> } | ||
|
||
return (value: unknown) => { | ||
const prevalidated = prevalidate(value) | ||
if (isFailure(prevalidated)) return prevalidated | ||
const v = prevalidated.right[key] as keyof D | ||
return validators[v](value) | ||
} | ||
} |
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