Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into kbn-503xx-expose-…
Browse files Browse the repository at this point in the history
…register-type-so-api
  • Loading branch information
pgayvallet committed Feb 24, 2020
2 parents c3efbb1 + 6b735c9 commit 15e7646
Show file tree
Hide file tree
Showing 15 changed files with 468 additions and 363 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
206 changes: 0 additions & 206 deletions packages/kbn-optimizer/src/optimizer/cache_keys.test.ts

This file was deleted.

53 changes: 53 additions & 0 deletions src/plugins/expressions/common/ast/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
* under the License.
*/

import { ExpressionValue, ExpressionValueError } from '../expression_types';
import { ExpressionFunction } from '../../public';

export type ExpressionAstNode =
| ExpressionAstExpression
| ExpressionAstFunction
Expand All @@ -31,6 +34,56 @@ export interface ExpressionAstFunction {
type: 'function';
function: string;
arguments: Record<string, ExpressionAstArgument[]>;

/**
* Debug information added to each function when expression is executed in *debug mode*.
*/
debug?: ExpressionAstFunctionDebug;
}

export interface ExpressionAstFunctionDebug {
/**
* True if function successfully returned output, false if function threw.
*/
success: boolean;

/**
* Reference to the expression function this AST node represents.
*/
fn: ExpressionFunction;

/**
* Input that expression function received as its first argument.
*/
input: ExpressionValue;

/**
* Map of resolved arguments expression function received as its second argument.
*/
args: Record<string, ExpressionValue>;

/**
* Result returned by the expression function. Including an error result
* if it was returned by the function (not thrown).
*/
output?: ExpressionValue;

/**
* Error that function threw normalized to `ExpressionValueError`.
*/
error?: ExpressionValueError;

/**
* Raw error that was thrown by the function, if any.
*/
rawError?: any | Error;

/**
* Time in milliseconds it took to execute the function. Duration can be
* `undefined` if error happened during argument resolution, because function
* timing starts after the arguments have been resolved.
*/
duration: number | undefined;
}

export type ExpressionAstArgument = string | boolean | number | ExpressionAstExpression;
Loading

0 comments on commit 15e7646

Please sign in to comment.