no sparse array by default. (#58212)

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
This commit is contained in:
Mikhail Shustov 2020-02-24 16:40:20 +01:00 committed by GitHub
parent 581f1bd6e9
commit 25dc7c03cb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 10 deletions

View file

@ -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', () => {

View file

@ -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':