Skip to content

Commit

Permalink
no sparse array by default. (#58212)
Browse files Browse the repository at this point in the history
Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
mshustov and elasticmachine authored Feb 24, 2020
1 parent 581f1bd commit 25dc7c0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
37 changes: 31 additions & 6 deletions packages/kbn-config-schema/src/types/array_type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,29 @@ test('fails if mixed types of content in array', () => {
);
});

test('returns empty array if input is empty but type has default value', () => {
const type = schema.arrayOf(schema.string({ defaultValue: 'test' }));
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('returns empty array if input is empty even if type is required', () => {
const type = schema.arrayOf(schema.string());
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', () => {
Expand All @@ -102,9 +117,19 @@ test('fails for null values if optional', () => {
);
});

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([]);
});

test('returns empty array if input is empty even if type is required', () => {
const type = schema.arrayOf(schema.string());
expect(type.validate([])).toEqual([]);
});

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', () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/kbn-config-schema/src/types/array_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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':
Expand Down

0 comments on commit 25dc7c0

Please sign in to comment.