generated from sapphiredev/sapphire-template
-
-
Notifications
You must be signed in to change notification settings - Fork 13
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
6 changed files
with
84 additions
and
2 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
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,40 @@ | ||
import type { IConstraint } from '../constraints/base/IConstraint'; | ||
import { ValidationError } from '../lib/errors/ValidationError'; | ||
import { Result } from '../lib/Result'; | ||
import { BaseValidator } from './imports'; | ||
|
||
export class MapValidator<K, V> extends BaseValidator<Map<K, V>> { | ||
private readonly keyValidator: BaseValidator<K>; | ||
private readonly valueValidator: BaseValidator<V>; | ||
|
||
public constructor(keyValidator: BaseValidator<K>, valueValidator: BaseValidator<V>, constraints: readonly IConstraint<Map<K, V>>[] = []) { | ||
super(constraints); | ||
this.keyValidator = keyValidator; | ||
this.valueValidator = valueValidator; | ||
} | ||
|
||
protected override clone(): this { | ||
return Reflect.construct(this.constructor, [this.keyValidator, this.valueValidator, this.constraints]); | ||
} | ||
|
||
protected handle(values: unknown): Result<Map<K, V>, ValidationError | AggregateError> { | ||
if (!(values instanceof Map)) { | ||
return Result.err(new ValidationError('MapValidator', 'Expected a map', values)); | ||
} | ||
|
||
const errors: Error[] = []; | ||
const transformed = new Map<K, V>(); | ||
|
||
for (const [key, value] of values.entries()) { | ||
const keyResult = this.keyValidator.run(key); | ||
const valueResult = this.valueValidator.run(value); | ||
const results = [keyResult, valueResult].filter((result) => result.isErr()); | ||
if (results.length === 0) transformed.set(keyResult.value!, valueResult.value!); | ||
else errors.push(...results.map((result) => result.error!)); | ||
} | ||
|
||
return errors.length === 0 // | ||
? Result.ok(transformed) | ||
: Result.err(new AggregateError(errors, 'Failed to validate at least one entry')); | ||
} | ||
} |
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,35 @@ | ||
import { s, ValidationError } from '../../src'; | ||
|
||
describe('MapValidator', () => { | ||
const value = new Map(); | ||
value.set('a', 1); | ||
value.set('b', 2); | ||
const predicate = s.map(s.string, s.number); | ||
|
||
test('GIVEN a non-map THEN throws ValidationError', () => { | ||
expect(() => predicate.parse(false)).toThrow(new ValidationError('MapValidator', 'Expected a map', false)); | ||
}); | ||
|
||
test('GIVEN a matching map THEN returns a map', () => { | ||
expect(predicate.parse(value)).toStrictEqual(value); | ||
}); | ||
|
||
test('GIVEN a non-matching map THEN throws AggregateError', () => { | ||
const map = new Map(); | ||
map.set('a', 1); | ||
map.set('foo', 'bar'); | ||
map.set(2, 'fizz'); | ||
map.set(3, 'buzz'); | ||
expect(() => predicate.parse(map)).toThrow( | ||
new AggregateError( | ||
[ | ||
new ValidationError('NumberValidator', 'Expected a number primitive', 'bar'), | ||
new ValidationError('StringValidator', 'Expected a string primitive', 1), | ||
new ValidationError('StringValidator', 'Expected a string primitive', 3), | ||
new ValidationError('NumberValidator', 'Expected a number primitive', 'buzz') | ||
], | ||
'Failed to validate at least one entry' | ||
) | ||
); | ||
}); | ||
}); |