Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com> Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
This commit is contained in:
parent
7ada510a29
commit
897099e23f
2 changed files with 37 additions and 10 deletions
|
@ -85,6 +85,38 @@ test('fails if mixed types of content in array', () => {
|
|||
);
|
||||
});
|
||||
|
||||
test('fails if sparse content in array', () => {
|
||||
const type = schema.arrayOf(schema.string());
|
||||
expect(type.validate([])).toEqual([]);
|
||||
expect(() => type.validate([undefined])).toThrowErrorMatchingInlineSnapshot(
|
||||
`"[0]: sparse array are not allowed"`
|
||||
);
|
||||
});
|
||||
|
||||
test('fails if sparse content in array if optional', () => {
|
||||
const type = schema.arrayOf(schema.maybe(schema.string()));
|
||||
expect(type.validate([])).toEqual([]);
|
||||
expect(() => type.validate([undefined])).toThrowErrorMatchingInlineSnapshot(
|
||||
`"[0]: sparse array are not allowed"`
|
||||
);
|
||||
});
|
||||
|
||||
test('fails if sparse content in array if nullable', () => {
|
||||
const type = schema.arrayOf(schema.nullable(schema.string()));
|
||||
expect(type.validate([])).toEqual([]);
|
||||
expect(type.validate([null])).toEqual([null]);
|
||||
expect(() => type.validate([undefined])).toThrowErrorMatchingInlineSnapshot(
|
||||
`"[0]: sparse array are not allowed"`
|
||||
);
|
||||
});
|
||||
|
||||
test('fails for null values if optional', () => {
|
||||
const type = schema.arrayOf(schema.maybe(schema.string()));
|
||||
expect(() => type.validate([null])).toThrowErrorMatchingInlineSnapshot(
|
||||
`"[0]: expected value of type [string] but got [null]"`
|
||||
);
|
||||
});
|
||||
|
||||
test('returns empty array if input is empty but type has default value', () => {
|
||||
const type = schema.arrayOf(schema.string({ defaultValue: 'test' }));
|
||||
expect(type.validate([])).toEqual([]);
|
||||
|
@ -95,16 +127,9 @@ test('returns empty array if input is empty even if type is required', () => {
|
|||
expect(type.validate([])).toEqual([]);
|
||||
});
|
||||
|
||||
test('fails for null values if optional', () => {
|
||||
const type = schema.arrayOf(schema.maybe(schema.string()));
|
||||
expect(() => type.validate([null])).toThrowErrorMatchingInlineSnapshot(
|
||||
`"[0]: expected value of type [string] but got [null]"`
|
||||
);
|
||||
});
|
||||
|
||||
test('handles default values for undefined values', () => {
|
||||
const type = schema.arrayOf(schema.string({ defaultValue: 'foo' }));
|
||||
expect(type.validate([undefined])).toEqual(['foo']);
|
||||
const type = schema.arrayOf(schema.string(), { defaultValue: ['foo'] });
|
||||
expect(type.validate(undefined)).toEqual(['foo']);
|
||||
});
|
||||
|
||||
test('array within array', () => {
|
||||
|
|
|
@ -31,7 +31,7 @@ export class ArrayType<T> extends Type<T[]> {
|
|||
let schema = internals
|
||||
.array()
|
||||
.items(type.getSchema().optional())
|
||||
.sparse();
|
||||
.sparse(false);
|
||||
|
||||
if (options.minSize !== undefined) {
|
||||
schema = schema.min(options.minSize);
|
||||
|
@ -49,6 +49,8 @@ export class ArrayType<T> extends Type<T[]> {
|
|||
case 'any.required':
|
||||
case 'array.base':
|
||||
return `expected value of type [array] but got [${typeDetect(value)}]`;
|
||||
case 'array.sparse':
|
||||
return `sparse array are not allowed`;
|
||||
case 'array.parse':
|
||||
return `could not parse array value from [${value}]`;
|
||||
case 'array.min':
|
||||
|
|
Loading…
Reference in a new issue