-
Notifications
You must be signed in to change notification settings - Fork 3
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
Showing
4 changed files
with
208 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* @flow */ | ||
|
||
import { intersection } from '../../src/contracts/intersection'; | ||
import { ValidationError } from '../../src/ValidationError'; | ||
|
||
const createError = (...types: $ReadOnlyArray<string>) => ( | ||
name: string, | ||
value: mixed, | ||
) => new ValidationError(name, value, types); | ||
|
||
describe('intersection', () => { | ||
describe('Creates new Contract for one or more other contracts or validation functions', () => { | ||
it('Return ValidationError if at least one Contract returns ValidationError', () => { | ||
const validate1 = jest.fn().mockReturnValue({ a: 1, b: 2 }); | ||
const validate2 = jest.fn().mockReturnValue({ a: 1, b: 2 }); | ||
const validate3 = jest.fn(createError('type3')); | ||
|
||
const result = intersection( | ||
validate1, | ||
validate2, | ||
validate3, | ||
)('valueName', { a: 1, b: 2 }); | ||
|
||
expect(result).toBeInstanceOf(ValidationError); | ||
expect(result.expectedTypes).toEqual(['Intersection']); | ||
expect(result.nested).toEqual([ | ||
new ValidationError('valueName', { a: 1, b: 2 }, 'type3'), | ||
]); | ||
|
||
expect(validate1).lastCalledWith('valueName', { a: 1, b: 2 }); | ||
expect(validate2).lastCalledWith('valueName', { a: 1, b: 2 }); | ||
expect(validate3).lastCalledWith('valueName', { a: 1, b: 2 }); | ||
}); | ||
|
||
it('Return value in other cases', () => { | ||
const validate1 = jest.fn().mockReturnValue({ a: 1, b: 2 }); | ||
const validate2 = jest.fn().mockReturnValue({ a: 1, b: 2 }); | ||
const validate3 = jest.fn().mockReturnValue({ a: 1, b: 2 }); | ||
|
||
expect( | ||
intersection( | ||
validate1, | ||
validate2, | ||
validate3, | ||
)('valueName', { a: 1, b: 2 }), | ||
).toEqual({ a: 1, b: 2 }); | ||
|
||
expect(validate1).lastCalledWith('valueName', { a: 1, b: 2 }); | ||
expect(validate2).lastCalledWith('valueName', { a: 1, b: 2 }); | ||
expect(validate3).lastCalledWith('valueName', { a: 1, b: 2 }); | ||
}); | ||
}); | ||
}); |
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,94 @@ | ||
/* @flow */ | ||
|
||
import { ValidationError } from '../ValidationError'; | ||
import * as contract from '../Contract'; | ||
|
||
export class IntersectionError extends ValidationError { | ||
constructor( | ||
valueName: string, | ||
value: mixed, | ||
errors: $ReadOnlyArray<ValidationError>, | ||
) { | ||
super(valueName, value, 'Intersection', errors); | ||
this.name = 'IntersectionError'; | ||
} | ||
} | ||
|
||
declare export function intersection<A>( | ||
a: (name: string, value: mixed) => ValidationError | A, | ||
): contract.Contract<A>; | ||
|
||
declare export function intersection<A, B>( | ||
a: (name: string, value: mixed) => ValidationError | A, | ||
b: (name: string, value: mixed) => ValidationError | B, | ||
): contract.Contract<A & B>; | ||
|
||
declare export function intersection<A, B, C>( | ||
a: (name: string, value: mixed) => ValidationError | A, | ||
b: (name: string, value: mixed) => ValidationError | B, | ||
c: (name: string, value: mixed) => ValidationError | C, | ||
): contract.Contract<A & B & C>; | ||
|
||
declare export function intersection<A, B, C, D>( | ||
a: (name: string, value: mixed) => ValidationError | A, | ||
b: (name: string, value: mixed) => ValidationError | B, | ||
c: (name: string, value: mixed) => ValidationError | C, | ||
d: (name: string, value: mixed) => ValidationError | D, | ||
): contract.Contract<A & B & C & D>; | ||
|
||
declare export function intersection<A, B, C, D, E>( | ||
a: (name: string, value: mixed) => ValidationError | A, | ||
b: (name: string, value: mixed) => ValidationError | B, | ||
c: (name: string, value: mixed) => ValidationError | C, | ||
d: (name: string, value: mixed) => ValidationError | D, | ||
e: (name: string, value: mixed) => ValidationError | E, | ||
): contract.Contract<A & B & C & D & E>; | ||
|
||
declare export function intersection<A, B, C, D, E, F>( | ||
a: (name: string, value: mixed) => ValidationError | A, | ||
b: (name: string, value: mixed) => ValidationError | B, | ||
c: (name: string, value: mixed) => ValidationError | C, | ||
d: (name: string, value: mixed) => ValidationError | D, | ||
e: (name: string, value: mixed) => ValidationError | E, | ||
f: (name: string, value: mixed) => ValidationError | F, | ||
): contract.Contract<A & B & C & D & E & F>; | ||
|
||
declare export function intersection<A, B, C, D, E, F, G>( | ||
a: (name: string, value: mixed) => ValidationError | A, | ||
b: (name: string, value: mixed) => ValidationError | B, | ||
c: (name: string, value: mixed) => ValidationError | C, | ||
d: (name: string, value: mixed) => ValidationError | D, | ||
e: (name: string, value: mixed) => ValidationError | E, | ||
f: (name: string, value: mixed) => ValidationError | F, | ||
g: (name: string, value: mixed) => ValidationError | G, | ||
): contract.Contract<A & B & C & D & E & F & G>; | ||
|
||
declare export function intersection<A, B, C, D, E, F, G, H>( | ||
a: (name: string, value: mixed) => ValidationError | A, | ||
b: (name: string, value: mixed) => ValidationError | B, | ||
c: (name: string, value: mixed) => ValidationError | C, | ||
d: (name: string, value: mixed) => ValidationError | D, | ||
e: (name: string, value: mixed) => ValidationError | E, | ||
f: (name: string, value: mixed) => ValidationError | F, | ||
g: (name: string, value: mixed) => ValidationError | G, | ||
h: (name: string, value: mixed) => ValidationError | H, | ||
): contract.Contract<A & B & C & D & E & F & G & H>; | ||
|
||
export function intersection<T>( | ||
...rules: $ReadOnlyArray<(name: string, value: mixed) => ValidationError | T> | ||
): contract.Contract<T> { | ||
const validators = rules; | ||
|
||
const intersectionContract = (name, value: any): ValidationError | T => { | ||
const errors = validators | ||
.map(validate => validate(name, value)) | ||
.reduce((scope, error) => { | ||
if (error instanceof ValidationError) scope.push(error); | ||
return scope; | ||
}, []); | ||
|
||
return errors.length ? new IntersectionError(name, value, errors) : value; | ||
}; | ||
|
||
return contract.of(intersectionContract); | ||
} |
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