From 25dc7c03cb1e3a9b167f31dd8fc804c97e29ec21 Mon Sep 17 00:00:00 2001 From: Mikhail Shustov Date: Mon, 24 Feb 2020 16:40:20 +0100 Subject: [PATCH] no sparse array by default. (#58212) Co-authored-by: Elastic Machine --- .../src/types/array_type.test.ts | 43 +++++++++++++++---- .../kbn-config-schema/src/types/array_type.ts | 4 +- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/packages/kbn-config-schema/src/types/array_type.test.ts b/packages/kbn-config-schema/src/types/array_type.test.ts index 73661ef849cf..66b72096a593 100644 --- a/packages/kbn-config-schema/src/types/array_type.test.ts +++ b/packages/kbn-config-schema/src/types/array_type.test.ts @@ -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', () => { diff --git a/packages/kbn-config-schema/src/types/array_type.ts b/packages/kbn-config-schema/src/types/array_type.ts index ad74f375588a..a0353e8348dd 100644 --- a/packages/kbn-config-schema/src/types/array_type.ts +++ b/packages/kbn-config-schema/src/types/array_type.ts @@ -31,7 +31,7 @@ export class ArrayType extends Type { 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 extends Type { 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':