Skip to content

Commit 214c384

Browse files
committed
test: 💍 add validation test
1 parent 52fdd9f commit 214c384

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/constants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* @module
3+
* @todo Move to `src/validator/`.
4+
*/
5+
16
/**
27
* Validation error codes.
38
*

src/type/__tests__/validateTestSuite.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {type Type, t} from '..';
2+
import {ValidationError, ValidationErrorMessage} from '../../constants';
23
import {TypeSystem} from '../../system/TypeSystem';
34

45
export const validateTestSuite = (validate: (type: Type, value: unknown) => void) => {
@@ -384,6 +385,22 @@ export const validateTestSuite = (validate: (type: Type, value: unknown) => void
384385
expect(() => validate(type, [1])).toThrowErrorMatchingInlineSnapshot(`"STR"`);
385386
});
386387

388+
test('object nested in array', () => {
389+
const type = t.obj
390+
.prop('foo', t.array(t.obj.prop('bar', t.str)))
391+
validate(type, {foo: [{bar: 'baz'}]});
392+
const validator = type.validator('object');
393+
const res = validator({foo: [{bar: 'baz'}]});
394+
expect(res).toBe(null);
395+
const res2 = validator({foo: {bar: 'baz'}});
396+
expect(res2).toEqual({
397+
code: ValidationError[ValidationError.ARR],
398+
errno: ValidationError.ARR,
399+
message: ValidationErrorMessage[ValidationError.ARR],
400+
path: [ 'foo' ]
401+
});
402+
});
403+
387404
describe('size bounds', () => {
388405
test('respects min and max', () => {
389406
const type = t.arr.options({min: 2, max: 4});

0 commit comments

Comments
 (0)