Hide input value from kbn-config-schema error messages (#58843)

* use inline snapshots instead of snapshots

* hide input value from error messages

* update core snapshots

* update xpack snapshots

* fix ftr assertions

* fix new snapshots

* hide values for byte_size and duration

* update new snapshots

* remove another byte_size value reference

* fix yet another value references in error messages

* update xpack snapshots

* update xpack ftr assertions
This commit is contained in:
Pierre Gayvallet 2020-03-06 17:47:28 +01:00 committed by GitHub
parent f4f956dfeb
commit 45fb6f38b8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
66 changed files with 532 additions and 609 deletions

View file

@ -1,11 +0,0 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`#constructor throws if number of bytes is negative 1`] = `"Value in bytes is expected to be a safe positive integer, but provided [-1024]."`;
exports[`#constructor throws if number of bytes is not safe 1`] = `"Value in bytes is expected to be a safe positive integer, but provided [NaN]."`;
exports[`#constructor throws if number of bytes is not safe 2`] = `"Value in bytes is expected to be a safe positive integer, but provided [Infinity]."`;
exports[`#constructor throws if number of bytes is not safe 3`] = `"Value in bytes is expected to be a safe positive integer, but provided [9007199254740992]."`;
exports[`parsing units throws an error when unsupported unit specified 1`] = `"Failed to parse [1tb] as byte value. Value must be either number of bytes, or follow the format <count>[b|kb|mb|gb] (e.g., '1024kb', '200mb', '1gb'), where the number is a safe positive integer."`;

View file

@ -42,19 +42,29 @@ describe('parsing units', () => {
});
test('throws an error when unsupported unit specified', () => {
expect(() => ByteSizeValue.parse('1tb')).toThrowErrorMatchingSnapshot();
expect(() => ByteSizeValue.parse('1tb')).toThrowErrorMatchingInlineSnapshot(
`"Failed to parse value as byte value. Value must be either number of bytes, or follow the format <count>[b|kb|mb|gb] (e.g., '1024kb', '200mb', '1gb'), where the number is a safe positive integer."`
);
});
});
describe('#constructor', () => {
test('throws if number of bytes is negative', () => {
expect(() => new ByteSizeValue(-1024)).toThrowErrorMatchingSnapshot();
expect(() => new ByteSizeValue(-1024)).toThrowErrorMatchingInlineSnapshot(
`"Value in bytes is expected to be a safe positive integer."`
);
});
test('throws if number of bytes is not safe', () => {
expect(() => new ByteSizeValue(NaN)).toThrowErrorMatchingSnapshot();
expect(() => new ByteSizeValue(Infinity)).toThrowErrorMatchingSnapshot();
expect(() => new ByteSizeValue(Math.pow(2, 53))).toThrowErrorMatchingSnapshot();
expect(() => new ByteSizeValue(NaN)).toThrowErrorMatchingInlineSnapshot(
`"Value in bytes is expected to be a safe positive integer."`
);
expect(() => new ByteSizeValue(Infinity)).toThrowErrorMatchingInlineSnapshot(
`"Value in bytes is expected to be a safe positive integer."`
);
expect(() => new ByteSizeValue(Math.pow(2, 53))).toThrowErrorMatchingInlineSnapshot(
`"Value in bytes is expected to be a safe positive integer."`
);
});
test('accepts 0', () => {

View file

@ -38,7 +38,7 @@ export class ByteSizeValue {
const number = Number(text);
if (typeof number !== 'number' || isNaN(number)) {
throw new Error(
`Failed to parse [${text}] as byte value. Value must be either number of bytes, or follow the format <count>[b|kb|mb|gb] ` +
`Failed to parse value as byte value. Value must be either number of bytes, or follow the format <count>[b|kb|mb|gb] ` +
`(e.g., '1024kb', '200mb', '1gb'), where the number is a safe positive integer.`
);
}
@ -53,9 +53,7 @@ export class ByteSizeValue {
constructor(private readonly valueInBytes: number) {
if (!Number.isSafeInteger(valueInBytes) || valueInBytes < 0) {
throw new Error(
`Value in bytes is expected to be a safe positive integer, but provided [${valueInBytes}].`
);
throw new Error(`Value in bytes is expected to be a safe positive integer.`);
}
}

View file

@ -28,7 +28,7 @@ function stringToDuration(text: string) {
const number = Number(text);
if (typeof number !== 'number' || isNaN(number)) {
throw new Error(
`Failed to parse [${text}] as time value. Value must be a duration in milliseconds, or follow the format ` +
`Failed to parse value as time value. Value must be a duration in milliseconds, or follow the format ` +
`<count>[ms|s|m|h|d|w|M|Y] (e.g. '70ms', '5s', '3d', '1Y'), where the duration is a safe positive integer.`
);
}
@ -43,9 +43,7 @@ function stringToDuration(text: string) {
function numberToDuration(numberMs: number) {
if (!Number.isSafeInteger(numberMs) || numberMs < 0) {
throw new Error(
`Value in milliseconds is expected to be a safe positive integer, but provided [${numberMs}].`
);
throw new Error(`Value in milliseconds is expected to be a safe positive integer.`);
}
return momentDuration(numberMs);

View file

@ -1,5 +0,0 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`includes namespace in failure 1`] = `"[foo-namespace]: expected value of type [any] but got [undefined]"`;
exports[`is required by default 1`] = `"expected value of type [any] but got [undefined]"`;

View file

@ -1,15 +0,0 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`includes namespace in failure 1`] = `"[foo-namespace]: expected value of type [boolean] but got [undefined]"`;
exports[`is required by default 1`] = `"expected value of type [boolean] but got [undefined]"`;
exports[`returns error when not boolean 1`] = `"expected value of type [boolean] but got [number]"`;
exports[`returns error when not boolean 2`] = `"expected value of type [boolean] but got [Array]"`;
exports[`returns error when not boolean 3`] = `"expected value of type [boolean] but got [string]"`;
exports[`returns error when not boolean 4`] = `"expected value of type [boolean] but got [number]"`;
exports[`returns error when not boolean 5`] = `"expected value of type [boolean] but got [string]"`;

View file

@ -1,11 +0,0 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`includes namespace in failure 1`] = `"[foo-namespace]: expected value of type [Buffer] but got [undefined]"`;
exports[`is required by default 1`] = `"expected value of type [Buffer] but got [undefined]"`;
exports[`returns error when not a buffer 1`] = `"expected value of type [Buffer] but got [number]"`;
exports[`returns error when not a buffer 2`] = `"expected value of type [Buffer] but got [Array]"`;
exports[`returns error when not a buffer 3`] = `"expected value of type [Buffer] but got [string]"`;

View file

@ -1,61 +0,0 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`#defaultValue can be a ByteSizeValue 1`] = `
ByteSizeValue {
"valueInBytes": 1024,
}
`;
exports[`#defaultValue can be a number 1`] = `
ByteSizeValue {
"valueInBytes": 1024,
}
`;
exports[`#defaultValue can be a string 1`] = `
ByteSizeValue {
"valueInBytes": 1024,
}
`;
exports[`#defaultValue can be a string-formatted number 1`] = `
ByteSizeValue {
"valueInBytes": 1024,
}
`;
exports[`#max returns error when larger 1`] = `"Value is [1mb] ([1048576b]) but it must be equal to or less than [1kb]"`;
exports[`#max returns value when smaller 1`] = `
ByteSizeValue {
"valueInBytes": 1,
}
`;
exports[`#min returns error when smaller 1`] = `"Value is [1b] ([1b]) but it must be equal to or greater than [1kb]"`;
exports[`#min returns value when larger 1`] = `
ByteSizeValue {
"valueInBytes": 1024,
}
`;
exports[`includes namespace in failure 1`] = `"[foo-namespace]: expected value of type [ByteSize] but got [undefined]"`;
exports[`is required by default 1`] = `"expected value of type [ByteSize] but got [undefined]"`;
exports[`returns error when not valid string or positive safe integer 1`] = `"Value in bytes is expected to be a safe positive integer, but provided [-123]."`;
exports[`returns error when not valid string or positive safe integer 2`] = `"Value in bytes is expected to be a safe positive integer, but provided [NaN]."`;
exports[`returns error when not valid string or positive safe integer 3`] = `"Value in bytes is expected to be a safe positive integer, but provided [Infinity]."`;
exports[`returns error when not valid string or positive safe integer 4`] = `"Value in bytes is expected to be a safe positive integer, but provided [9007199254740992]."`;
exports[`returns error when not valid string or positive safe integer 5`] = `"expected value of type [ByteSize] but got [Array]"`;
exports[`returns error when not valid string or positive safe integer 6`] = `"expected value of type [ByteSize] but got [RegExp]"`;
exports[`returns error when not valid string or positive safe integer 7`] = `"Failed to parse [123foo] as byte value. Value must be either number of bytes, or follow the format <count>[b|kb|mb|gb] (e.g., '1024kb', '200mb', '1gb'), where the number is a safe positive integer."`;
exports[`returns error when not valid string or positive safe integer 8`] = `"Failed to parse [123 456] as byte value. Value must be either number of bytes, or follow the format <count>[b|kb|mb|gb] (e.g., '1024kb', '200mb', '1gb'), where the number is a safe positive integer."`;

View file

@ -1,45 +0,0 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`correctly handles missing references 1`] = `"[value]: expected value of type [string] but got [number]"`;
exports[`includes namespace into failures 1`] = `"[mega-namespace.value]: expected value of type [string] but got [number]"`;
exports[`includes namespace into failures 2`] = `"[mega-namespace.value]: expected value of type [number] but got [string]"`;
exports[`properly handles conditionals within objects 1`] = `"[value]: expected value of type [string] but got [number]"`;
exports[`properly handles conditionals within objects 2`] = `"[value]: expected value of type [number] but got [string]"`;
exports[`properly handles schemas with incompatible types 1`] = `"expected value of type [string] but got [boolean]"`;
exports[`properly handles schemas with incompatible types 2`] = `"expected value of type [boolean] but got [string]"`;
exports[`properly validates types according chosen schema 1`] = `"value is [a] but it must have a minimum length of [2]."`;
exports[`properly validates types according chosen schema 2`] = `"value is [ab] but it must have a maximum length of [1]."`;
exports[`properly validates when compares with "null" literal Schema 1`] = `"value is [a] but it must have a minimum length of [2]."`;
exports[`properly validates when compares with "null" literal Schema 2`] = `"value is [ab] but it must have a minimum length of [3]."`;
exports[`properly validates when compares with Schema 1`] = `"value is [a] but it must have a minimum length of [2]."`;
exports[`properly validates when compares with Schema 2`] = `"value is [ab] but it must have a minimum length of [3]."`;
exports[`required by default 1`] = `"expected value of type [string] but got [undefined]"`;
exports[`works with both context and sibling references 1`] = `"[value]: expected value of type [string] but got [number]"`;
exports[`works with both context and sibling references 2`] = `"[value]: expected value of type [number] but got [string]"`;
exports[`works within \`oneOf\` 1`] = `
"types that failed validation:
- [0]: expected value of type [string] but got [number]
- [1]: expected value of type [array] but got [number]"
`;
exports[`works within \`oneOf\` 2`] = `
"types that failed validation:
- [0]: expected value of type [string] but got [boolean]
- [1]: expected value of type [array] but got [boolean]"
`;

View file

@ -1,29 +0,0 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`#defaultValue can be a moment.Duration 1`] = `"PT1H"`;
exports[`#defaultValue can be a number 1`] = `"PT0.6S"`;
exports[`#defaultValue can be a string 1`] = `"PT1H"`;
exports[`#defaultValue can be a string-formatted number 1`] = `"PT0.6S"`;
exports[`includes namespace in failure 1`] = `"[foo-namespace]: expected value of type [moment.Duration] but got [undefined]"`;
exports[`is required by default 1`] = `"expected value of type [moment.Duration] but got [undefined]"`;
exports[`returns error when not valid string or non-safe positive integer 1`] = `"Value in milliseconds is expected to be a safe positive integer, but provided [-123]."`;
exports[`returns error when not valid string or non-safe positive integer 2`] = `"Value in milliseconds is expected to be a safe positive integer, but provided [NaN]."`;
exports[`returns error when not valid string or non-safe positive integer 3`] = `"Value in milliseconds is expected to be a safe positive integer, but provided [Infinity]."`;
exports[`returns error when not valid string or non-safe positive integer 4`] = `"Value in milliseconds is expected to be a safe positive integer, but provided [9007199254740992]."`;
exports[`returns error when not valid string or non-safe positive integer 5`] = `"expected value of type [moment.Duration] but got [Array]"`;
exports[`returns error when not valid string or non-safe positive integer 6`] = `"expected value of type [moment.Duration] but got [RegExp]"`;
exports[`returns error when not valid string or non-safe positive integer 7`] = `"Failed to parse [123foo] as time value. Value must be a duration in milliseconds, or follow the format <count>[ms|s|m|h|d|w|M|Y] (e.g. '70ms', '5s', '3d', '1Y'), where the duration is a safe positive integer."`;
exports[`returns error when not valid string or non-safe positive integer 8`] = `"Failed to parse [123 456] as time value. Value must be a duration in milliseconds, or follow the format <count>[ms|s|m|h|d|w|M|Y] (e.g. '70ms', '5s', '3d', '1Y'), where the duration is a safe positive integer."`;

View file

@ -1,13 +0,0 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`includes namespace in failure 1`] = `"[foo-namespace]: expected value to equal [test] but got [foo]"`;
exports[`returns error when not correct 1`] = `"expected value to equal [test] but got [foo]"`;
exports[`returns error when not correct 2`] = `"expected value to equal [true] but got [false]"`;
exports[`returns error when not correct 3`] = `"expected value to equal [test] but got [1,2,3]"`;
exports[`returns error when not correct 4`] = `"expected value to equal [123] but got [abc]"`;
exports[`returns error when not correct 5`] = `"expected value to equal [null] but got [42]"`;

View file

@ -1,9 +0,0 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`fails if null 1`] = `"expected value of type [string] but got [null]"`;
exports[`includes namespace in failure 1`] = `"[foo-namespace]: expected value of type [string] but got [null]"`;
exports[`validates basic type 1`] = `"expected value of type [string] but got [number]"`;
exports[`validates contained type 1`] = `"value is [foo] but it must have a maximum length of [1]."`;

View file

@ -1,13 +0,0 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`throws on any value set 1`] = `"a value wasn't expected to be present"`;
exports[`throws on any value set 2`] = `"a value wasn't expected to be present"`;
exports[`throws on any value set 3`] = `"a value wasn't expected to be present"`;
exports[`throws on any value set 4`] = `"a value wasn't expected to be present"`;
exports[`throws on value set as object property 1`] = `"[name]: a value wasn't expected to be present"`;
exports[`works for conditional types 1`] = `"[name]: a value wasn't expected to be present"`;

View file

@ -1,25 +0,0 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`includes namespace in failure 1`] = `
"[foo-namespace]: types that failed validation:
- [foo-namespace.0]: value is [foo] but it must have a maximum length of [1].
- [foo-namespace.1]: expected value to equal [null] but got [foo]"
`;
exports[`validates basic type 1`] = `
"types that failed validation:
- [0]: expected value of type [string] but got [number]
- [1]: expected value to equal [null] but got [666]"
`;
exports[`validates contained type 1`] = `
"types that failed validation:
- [0]: value is [foo] but it must have a maximum length of [1].
- [1]: expected value to equal [null] but got [foo]"
`;
exports[`validates type errors in object 1`] = `
"[foo]: types that failed validation:
- [foo.0]: value is [ab] but it must have a maximum length of [1].
- [foo.1]: expected value to equal [null] but got [ab]"
`;

View file

@ -1,17 +0,0 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`#max returns error when larger number 1`] = `"Value is [3] but it must be equal to or lower than [2]."`;
exports[`#min returns error when smaller number 1`] = `"Value is [3] but it must be equal to or greater than [4]."`;
exports[`fails if number is \`NaN\` 1`] = `"expected value of type [number] but got [number]"`;
exports[`includes namespace in failure 1`] = `"[foo-namespace]: expected value of type [number] but got [undefined]"`;
exports[`is required by default 1`] = `"expected value of type [number] but got [undefined]"`;
exports[`returns error when not number or numeric string 1`] = `"expected value of type [number] but got [string]"`;
exports[`returns error when not number or numeric string 2`] = `"expected value of type [number] but got [Array]"`;
exports[`returns error when not number or numeric string 3`] = `"expected value of type [number] but got [RegExp]"`;

View file

@ -1,32 +0,0 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`fails if not matching literal 1`] = `
"types that failed validation:
- [0]: expected value to equal [foo] but got [bar]"
`;
exports[`fails if not matching multiple types 1`] = `
"types that failed validation:
- [0]: expected value of type [string] but got [boolean]
- [1]: expected value of type [number] but got [boolean]"
`;
exports[`fails if not matching type 1`] = `
"types that failed validation:
- [0]: expected value of type [string] but got [boolean]"
`;
exports[`fails if not matching type 2`] = `
"types that failed validation:
- [0]: expected value of type [string] but got [number]"
`;
exports[`handles object with wrong type 1`] = `
"types that failed validation:
- [0.age]: expected value of type [number] but got [string]"
`;
exports[`includes namespace in failure 1`] = `
"[foo-namespace]: types that failed validation:
- [foo-namespace.0.age]: expected value of type [number] but got [string]"
`;

View file

@ -1,11 +0,0 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`includes namespace in failure 1`] = `"[foo-namespace]: expected value of type [Stream] but got [undefined]"`;
exports[`is required by default 1`] = `"expected value of type [Buffer] but got [undefined]"`;
exports[`returns error when not a stream 1`] = `"expected value of type [Stream] but got [number]"`;
exports[`returns error when not a stream 2`] = `"expected value of type [Stream] but got [Array]"`;
exports[`returns error when not a stream 3`] = `"expected value of type [Stream] but got [string]"`;

View file

@ -1,35 +0,0 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`#hostname returns error when empty string 1`] = `"any.empty"`;
exports[`#hostname returns error when value is not a valid hostname 1`] = `"value is [host:name] but it must be a valid hostname (see RFC 1123)."`;
exports[`#hostname returns error when value is not a valid hostname 2`] = `"value is [localhost:5601] but it must be a valid hostname (see RFC 1123)."`;
exports[`#hostname returns error when value is not a valid hostname 3`] = `"value is [-] but it must be a valid hostname (see RFC 1123)."`;
exports[`#hostname returns error when value is not a valid hostname 4`] = `"value is [0:?:0:0:0:0:0:1] but it must be a valid hostname (see RFC 1123)."`;
exports[`#hostname returns error when value is not a valid hostname 5`] = `"value is [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa] but it must be a valid hostname (see RFC 1123)."`;
exports[`#hostname supports string validation rules 1`] = `"value is [www.example.com] but it must have a maximum length of [3]."`;
exports[`#maxLength returns error when longer string 1`] = `"value is [foo] but it must have a maximum length of [2]."`;
exports[`#minLength returns error when empty string 1`] = `"value is [] but it must have a minimum length of [2]."`;
exports[`#minLength returns error when shorter string 1`] = `"value is [foo] but it must have a minimum length of [4]."`;
exports[`#validate throw when empty string 1`] = `"validator failure"`;
exports[`#validate throws when returns string 1`] = `"validator failure"`;
exports[`includes namespace in failure 1`] = `"[foo-namespace]: expected value of type [string] but got [undefined]"`;
exports[`is required by default 1`] = `"expected value of type [string] but got [undefined]"`;
exports[`returns error when not string 1`] = `"expected value of type [string] but got [number]"`;
exports[`returns error when not string 2`] = `"expected value of type [string] but got [Array]"`;
exports[`returns error when not string 3`] = `"expected value of type [string] but got [RegExp]"`;

View file

@ -1,25 +0,0 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`#scheme returns error when shorter string 1`] = `"expected URI with scheme [http|https] but got [ftp://elastic.co]."`;
exports[`#scheme returns error when shorter string 2`] = `"expected URI with scheme [http|https] but got [file:///kibana.log]."`;
exports[`#validate throws when returns string 1`] = `"validator failure"`;
exports[`is required by default 1`] = `"expected value of type [string] but got [undefined]."`;
exports[`returns error when not string 1`] = `"expected value of type [string] but got [number]."`;
exports[`returns error when not string 2`] = `"expected value of type [string] but got [Array]."`;
exports[`returns error when not string 3`] = `"expected value of type [string] but got [RegExp]."`;
exports[`returns error when value is not a URI 1`] = `"value is [3domain.local] but it must be a valid URI (see RFC 3986)."`;
exports[`returns error when value is not a URI 2`] = `"value is [http://8010:0:0:0:9:500:300C:200A] but it must be a valid URI (see RFC 3986)."`;
exports[`returns error when value is not a URI 3`] = `"value is [-] but it must be a valid URI (see RFC 3986)."`;
exports[`returns error when value is not a URI 4`] = `"value is [https://example.com?baz[]=foo&baz[]=bar] but it must be a valid URI (see RFC 3986)."`;
exports[`returns error when value is not a URI 5`] = `"value is [http://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa] but it must be a valid URI (see RFC 3986)."`;

View file

@ -28,13 +28,17 @@ test('works for any value', () => {
});
test('is required by default', () => {
expect(() => schema.any().validate(undefined)).toThrowErrorMatchingSnapshot();
expect(() => schema.any().validate(undefined)).toThrowErrorMatchingInlineSnapshot(
`"expected value of type [any] but got [undefined]"`
);
});
test('includes namespace in failure', () => {
expect(() =>
schema.any().validate(undefined, {}, 'foo-namespace')
).toThrowErrorMatchingSnapshot();
).toThrowErrorMatchingInlineSnapshot(
`"[foo-namespace]: expected value of type [any] but got [undefined]"`
);
});
describe('#defaultValue', () => {

View file

@ -39,7 +39,7 @@ test('fails if wrong input type', () => {
test('fails if string input cannot be parsed', () => {
const type = schema.arrayOf(schema.string());
expect(() => type.validate('test')).toThrowErrorMatchingInlineSnapshot(
`"could not parse array value from [test]"`
`"could not parse array value from json input"`
);
});
@ -53,7 +53,7 @@ test('fails with correct type if parsed input is not an array', () => {
test('includes namespace in failure when wrong top-level type', () => {
const type = schema.arrayOf(schema.string());
expect(() => type.validate('test', {}, 'foo-namespace')).toThrowErrorMatchingInlineSnapshot(
`"[foo-namespace]: could not parse array value from [test]"`
`"[foo-namespace]: could not parse array value from json input"`
);
});

View file

@ -52,7 +52,7 @@ export class ArrayType<T> extends Type<T[]> {
case 'array.sparse':
return `sparse array are not allowed`;
case 'array.parse':
return `could not parse array value from [${value}]`;
return `could not parse array value from json input`;
case 'array.min':
return `array size is [${value.length}], but cannot be smaller than [${limit}]`;
case 'array.max':

View file

@ -35,13 +35,17 @@ test('handles boolean strings', () => {
});
test('is required by default', () => {
expect(() => schema.boolean().validate(undefined)).toThrowErrorMatchingSnapshot();
expect(() => schema.boolean().validate(undefined)).toThrowErrorMatchingInlineSnapshot(
`"expected value of type [boolean] but got [undefined]"`
);
});
test('includes namespace in failure', () => {
expect(() =>
schema.boolean().validate(undefined, {}, 'foo-namespace')
).toThrowErrorMatchingSnapshot();
).toThrowErrorMatchingInlineSnapshot(
`"[foo-namespace]: expected value of type [boolean] but got [undefined]"`
);
});
describe('#defaultValue', () => {
@ -55,13 +59,23 @@ describe('#defaultValue', () => {
});
test('returns error when not boolean', () => {
expect(() => schema.boolean().validate(123)).toThrowErrorMatchingSnapshot();
expect(() => schema.boolean().validate(123)).toThrowErrorMatchingInlineSnapshot(
`"expected value of type [boolean] but got [number]"`
);
expect(() => schema.boolean().validate([1, 2, 3])).toThrowErrorMatchingSnapshot();
expect(() => schema.boolean().validate([1, 2, 3])).toThrowErrorMatchingInlineSnapshot(
`"expected value of type [boolean] but got [Array]"`
);
expect(() => schema.boolean().validate('abc')).toThrowErrorMatchingSnapshot();
expect(() => schema.boolean().validate('abc')).toThrowErrorMatchingInlineSnapshot(
`"expected value of type [boolean] but got [string]"`
);
expect(() => schema.boolean().validate(0)).toThrowErrorMatchingSnapshot();
expect(() => schema.boolean().validate(0)).toThrowErrorMatchingInlineSnapshot(
`"expected value of type [boolean] but got [number]"`
);
expect(() => schema.boolean().validate('no')).toThrowErrorMatchingSnapshot();
expect(() => schema.boolean().validate('no')).toThrowErrorMatchingInlineSnapshot(
`"expected value of type [boolean] but got [string]"`
);
});

View file

@ -25,13 +25,17 @@ test('returns value by default', () => {
});
test('is required by default', () => {
expect(() => schema.buffer().validate(undefined)).toThrowErrorMatchingSnapshot();
expect(() => schema.buffer().validate(undefined)).toThrowErrorMatchingInlineSnapshot(
`"expected value of type [Buffer] but got [undefined]"`
);
});
test('includes namespace in failure', () => {
expect(() =>
schema.buffer().validate(undefined, {}, 'foo-namespace')
).toThrowErrorMatchingSnapshot();
).toThrowErrorMatchingInlineSnapshot(
`"[foo-namespace]: expected value of type [Buffer] but got [undefined]"`
);
});
describe('#defaultValue', () => {
@ -49,9 +53,15 @@ describe('#defaultValue', () => {
});
test('returns error when not a buffer', () => {
expect(() => schema.buffer().validate(123)).toThrowErrorMatchingSnapshot();
expect(() => schema.buffer().validate(123)).toThrowErrorMatchingInlineSnapshot(
`"expected value of type [Buffer] but got [number]"`
);
expect(() => schema.buffer().validate([1, 2, 3])).toThrowErrorMatchingSnapshot();
expect(() => schema.buffer().validate([1, 2, 3])).toThrowErrorMatchingInlineSnapshot(
`"expected value of type [Buffer] but got [Array]"`
);
expect(() => schema.buffer().validate('abc')).toThrowErrorMatchingSnapshot();
expect(() => schema.buffer().validate('abc')).toThrowErrorMatchingInlineSnapshot(
`"expected value of type [Buffer] but got [string]"`
);
});

View file

@ -35,11 +35,17 @@ test('handles numbers', () => {
});
test('is required by default', () => {
expect(() => byteSize().validate(undefined)).toThrowErrorMatchingSnapshot();
expect(() => byteSize().validate(undefined)).toThrowErrorMatchingInlineSnapshot(
`"expected value of type [ByteSize] but got [undefined]"`
);
});
test('includes namespace in failure', () => {
expect(() => byteSize().validate(undefined, {}, 'foo-namespace')).toThrowErrorMatchingSnapshot();
expect(() =>
byteSize().validate(undefined, {}, 'foo-namespace')
).toThrowErrorMatchingInlineSnapshot(
`"[foo-namespace]: expected value of type [ByteSize] but got [undefined]"`
);
});
describe('#defaultValue', () => {
@ -48,7 +54,11 @@ describe('#defaultValue', () => {
byteSize({
defaultValue: ByteSizeValue.parse('1kb'),
}).validate(undefined)
).toMatchSnapshot();
).toMatchInlineSnapshot(`
ByteSizeValue {
"valueInBytes": 1024,
}
`);
});
test('can be a string', () => {
@ -56,7 +66,11 @@ describe('#defaultValue', () => {
byteSize({
defaultValue: '1kb',
}).validate(undefined)
).toMatchSnapshot();
).toMatchInlineSnapshot(`
ByteSizeValue {
"valueInBytes": 1024,
}
`);
});
test('can be a string-formatted number', () => {
@ -64,7 +78,11 @@ describe('#defaultValue', () => {
byteSize({
defaultValue: '1024',
}).validate(undefined)
).toMatchSnapshot();
).toMatchInlineSnapshot(`
ByteSizeValue {
"valueInBytes": 1024,
}
`);
});
test('can be a number', () => {
@ -72,7 +90,11 @@ describe('#defaultValue', () => {
byteSize({
defaultValue: 1024,
}).validate(undefined)
).toMatchSnapshot();
).toMatchInlineSnapshot(`
ByteSizeValue {
"valueInBytes": 1024,
}
`);
});
});
@ -82,7 +104,11 @@ describe('#min', () => {
byteSize({
min: '1b',
}).validate('1kb')
).toMatchSnapshot();
).toMatchInlineSnapshot(`
ByteSizeValue {
"valueInBytes": 1024,
}
`);
});
test('returns error when smaller', () => {
@ -90,34 +116,56 @@ describe('#min', () => {
byteSize({
min: '1kb',
}).validate('1b')
).toThrowErrorMatchingSnapshot();
).toThrowErrorMatchingInlineSnapshot(`"Value must be equal to or greater than [1kb]"`);
});
});
describe('#max', () => {
test('returns value when smaller', () => {
expect(byteSize({ max: '1kb' }).validate('1b')).toMatchSnapshot();
expect(byteSize({ max: '1kb' }).validate('1b')).toMatchInlineSnapshot(`
ByteSizeValue {
"valueInBytes": 1,
}
`);
});
test('returns error when larger', () => {
expect(() => byteSize({ max: '1kb' }).validate('1mb')).toThrowErrorMatchingSnapshot();
expect(() => byteSize({ max: '1kb' }).validate('1mb')).toThrowErrorMatchingInlineSnapshot(
`"Value must be equal to or less than [1kb]"`
);
});
});
test('returns error when not valid string or positive safe integer', () => {
expect(() => byteSize().validate(-123)).toThrowErrorMatchingSnapshot();
expect(() => byteSize().validate(-123)).toThrowErrorMatchingInlineSnapshot(
`"Value in bytes is expected to be a safe positive integer."`
);
expect(() => byteSize().validate(NaN)).toThrowErrorMatchingSnapshot();
expect(() => byteSize().validate(NaN)).toThrowErrorMatchingInlineSnapshot(
`"Value in bytes is expected to be a safe positive integer."`
);
expect(() => byteSize().validate(Infinity)).toThrowErrorMatchingSnapshot();
expect(() => byteSize().validate(Infinity)).toThrowErrorMatchingInlineSnapshot(
`"Value in bytes is expected to be a safe positive integer."`
);
expect(() => byteSize().validate(Math.pow(2, 53))).toThrowErrorMatchingSnapshot();
expect(() => byteSize().validate(Math.pow(2, 53))).toThrowErrorMatchingInlineSnapshot(
`"Value in bytes is expected to be a safe positive integer."`
);
expect(() => byteSize().validate([1, 2, 3])).toThrowErrorMatchingSnapshot();
expect(() => byteSize().validate([1, 2, 3])).toThrowErrorMatchingInlineSnapshot(
`"expected value of type [ByteSize] but got [Array]"`
);
expect(() => byteSize().validate(/abc/)).toThrowErrorMatchingSnapshot();
expect(() => byteSize().validate(/abc/)).toThrowErrorMatchingInlineSnapshot(
`"expected value of type [ByteSize] but got [RegExp]"`
);
expect(() => byteSize().validate('123foo')).toThrowErrorMatchingSnapshot();
expect(() => byteSize().validate('123foo')).toThrowErrorMatchingInlineSnapshot(
`"Failed to parse value as byte value. Value must be either number of bytes, or follow the format <count>[b|kb|mb|gb] (e.g., '1024kb', '200mb', '1gb'), where the number is a safe positive integer."`
);
expect(() => byteSize().validate('123 456')).toThrowErrorMatchingSnapshot();
expect(() => byteSize().validate('123 456')).toThrowErrorMatchingInlineSnapshot(
`"Failed to parse value as byte value. Value must be either number of bytes, or follow the format <count>[b|kb|mb|gb] (e.g., '1024kb', '200mb', '1gb'), where the number is a safe positive integer."`
);
});

View file

@ -61,13 +61,9 @@ export class ByteSizeType extends Type<ByteSizeValue> {
case 'bytes.parse':
return new SchemaTypeError(message, path);
case 'bytes.min':
return `Value is [${value.toString()}] ([${value.toString(
'b'
)}]) but it must be equal to or greater than [${limit.toString()}]`;
return `Value must be equal to or greater than [${limit.toString()}]`;
case 'bytes.max':
return `Value is [${value.toString()}] ([${value.toString(
'b'
)}]) but it must be equal to or less than [${limit.toString()}]`;
return `Value must be equal to or less than [${limit.toString()}]`;
}
}
}

View file

@ -32,7 +32,7 @@ test('required by default', () => {
context_value_1: 0,
context_value_2: 0,
})
).toThrowErrorMatchingSnapshot();
).toThrowErrorMatchingInlineSnapshot(`"expected value of type [string] but got [undefined]"`);
});
test('returns default', () => {
@ -90,7 +90,9 @@ test('properly validates types according chosen schema', () => {
context_value_1: 0,
context_value_2: 0,
})
).toThrowErrorMatchingSnapshot();
).toThrowErrorMatchingInlineSnapshot(
`"value has length [1] but it must have a minimum length of [2]."`
);
expect(
type.validate('ab', {
@ -104,7 +106,9 @@ test('properly validates types according chosen schema', () => {
context_value_1: 0,
context_value_2: 1,
})
).toThrowErrorMatchingSnapshot();
).toThrowErrorMatchingInlineSnapshot(
`"value has length [2] but it must have a maximum length of [1]."`
);
expect(
type.validate('a', {
@ -126,7 +130,9 @@ test('properly validates when compares with Schema', () => {
type.validate('a', {
context_value_1: 0,
})
).toThrowErrorMatchingSnapshot();
).toThrowErrorMatchingInlineSnapshot(
`"value has length [1] but it must have a minimum length of [2]."`
);
expect(
type.validate('ab', {
@ -138,7 +144,9 @@ test('properly validates when compares with Schema', () => {
type.validate('ab', {
context_value_1: 'b',
})
).toThrowErrorMatchingSnapshot();
).toThrowErrorMatchingInlineSnapshot(
`"value has length [2] but it must have a minimum length of [3]."`
);
expect(
type.validate('abc', {
@ -159,7 +167,9 @@ test('properly validates when compares with "null" literal Schema', () => {
type.validate('a', {
context_value_1: null,
})
).toThrowErrorMatchingSnapshot();
).toThrowErrorMatchingInlineSnapshot(
`"value has length [1] but it must have a minimum length of [2]."`
);
expect(
type.validate('ab', {
@ -171,7 +181,9 @@ test('properly validates when compares with "null" literal Schema', () => {
type.validate('ab', {
context_value_1: 'b',
})
).toThrowErrorMatchingSnapshot();
).toThrowErrorMatchingInlineSnapshot(
`"value has length [2] but it must have a minimum length of [3]."`
);
expect(
type.validate('abc', {
@ -193,7 +205,7 @@ test('properly handles schemas with incompatible types', () => {
context_value_1: 0,
context_value_2: 0,
})
).toThrowErrorMatchingSnapshot();
).toThrowErrorMatchingInlineSnapshot(`"expected value of type [string] but got [boolean]"`);
expect(
type.validate('a', {
@ -207,7 +219,7 @@ test('properly handles schemas with incompatible types', () => {
context_value_1: 0,
context_value_2: 1,
})
).toThrowErrorMatchingSnapshot();
).toThrowErrorMatchingInlineSnapshot(`"expected value of type [boolean] but got [string]"`);
expect(
type.validate(true, {
@ -223,14 +235,18 @@ test('properly handles conditionals within objects', () => {
value: schema.conditional(schema.siblingRef('key'), 'number', schema.number(), schema.string()),
});
expect(() => type.validate({ key: 'string', value: 1 })).toThrowErrorMatchingSnapshot();
expect(() => type.validate({ key: 'string', value: 1 })).toThrowErrorMatchingInlineSnapshot(
`"[value]: expected value of type [string] but got [number]"`
);
expect(type.validate({ key: 'string', value: 'a' })).toEqual({
key: 'string',
value: 'a',
});
expect(() => type.validate({ key: 'number', value: 'a' })).toThrowErrorMatchingSnapshot();
expect(() => type.validate({ key: 'number', value: 'a' })).toThrowErrorMatchingInlineSnapshot(
`"[value]: expected value of type [number] but got [string]"`
);
expect(type.validate({ key: 'number', value: 1 })).toEqual({
key: 'number',
@ -269,7 +285,9 @@ test('works with both context and sibling references', () => {
expect(() =>
type.validate({ key: 'string', value: 1 }, { context_key: 'number' })
).toThrowErrorMatchingSnapshot();
).toThrowErrorMatchingInlineSnapshot(
`"[value]: expected value of type [string] but got [number]"`
);
expect(type.validate({ key: 'string', value: 'a' }, { context_key: 'number' })).toEqual({
key: 'string',
@ -278,7 +296,9 @@ test('works with both context and sibling references', () => {
expect(() =>
type.validate({ key: 'number', value: 'a' }, { context_key: 'number' })
).toThrowErrorMatchingSnapshot();
).toThrowErrorMatchingInlineSnapshot(
`"[value]: expected value of type [number] but got [string]"`
);
expect(type.validate({ key: 'number', value: 1 }, { context_key: 'number' })).toEqual({
key: 'number',
@ -294,11 +314,15 @@ test('includes namespace into failures', () => {
expect(() =>
type.validate({ key: 'string', value: 1 }, {}, 'mega-namespace')
).toThrowErrorMatchingSnapshot();
).toThrowErrorMatchingInlineSnapshot(
`"[mega-namespace.value]: expected value of type [string] but got [number]"`
);
expect(() =>
type.validate({ key: 'number', value: 'a' }, {}, 'mega-namespace')
).toThrowErrorMatchingSnapshot();
).toThrowErrorMatchingInlineSnapshot(
`"[mega-namespace.value]: expected value of type [number] but got [string]"`
);
});
test('correctly handles missing references', () => {
@ -311,7 +335,9 @@ test('correctly handles missing references', () => {
),
});
expect(() => type.validate({ value: 1 })).toThrowErrorMatchingSnapshot();
expect(() => type.validate({ value: 1 })).toThrowErrorMatchingInlineSnapshot(
`"[value]: expected value of type [string] but got [number]"`
);
expect(type.validate({ value: 'a' })).toEqual({ value: 'a' });
});
@ -332,8 +358,16 @@ test('works within `oneOf`', () => {
expect(type.validate(true, { type: 'boolean' })).toEqual(true);
expect(type.validate(['a', 'b'], { type: 'array' })).toEqual(['a', 'b']);
expect(() => type.validate(1, { type: 'string' })).toThrowErrorMatchingSnapshot();
expect(() => type.validate(true, { type: 'string' })).toThrowErrorMatchingSnapshot();
expect(() => type.validate(1, { type: 'string' })).toThrowErrorMatchingInlineSnapshot(`
"types that failed validation:
- [0]: expected value of type [string] but got [number]
- [1]: expected value of type [array] but got [number]"
`);
expect(() => type.validate(true, { type: 'string' })).toThrowErrorMatchingInlineSnapshot(`
"types that failed validation:
- [0]: expected value of type [string] but got [boolean]
- [1]: expected value of type [array] but got [boolean]"
`);
});
describe('#validate', () => {

View file

@ -35,11 +35,17 @@ test('handles number', () => {
});
test('is required by default', () => {
expect(() => duration().validate(undefined)).toThrowErrorMatchingSnapshot();
expect(() => duration().validate(undefined)).toThrowErrorMatchingInlineSnapshot(
`"expected value of type [moment.Duration] but got [undefined]"`
);
});
test('includes namespace in failure', () => {
expect(() => duration().validate(undefined, {}, 'foo-namespace')).toThrowErrorMatchingSnapshot();
expect(() =>
duration().validate(undefined, {}, 'foo-namespace')
).toThrowErrorMatchingInlineSnapshot(
`"[foo-namespace]: expected value of type [moment.Duration] but got [undefined]"`
);
});
describe('#defaultValue', () => {
@ -48,7 +54,7 @@ describe('#defaultValue', () => {
duration({
defaultValue: momentDuration(1, 'hour'),
}).validate(undefined)
).toMatchSnapshot();
).toMatchInlineSnapshot(`"PT1H"`);
});
test('can be a string', () => {
@ -56,7 +62,7 @@ describe('#defaultValue', () => {
duration({
defaultValue: '1h',
}).validate(undefined)
).toMatchSnapshot();
).toMatchInlineSnapshot(`"PT1H"`);
});
test('can be a string-formatted number', () => {
@ -64,7 +70,7 @@ describe('#defaultValue', () => {
duration({
defaultValue: '600',
}).validate(undefined)
).toMatchSnapshot();
).toMatchInlineSnapshot(`"PT0.6S"`);
});
test('can be a number', () => {
@ -72,7 +78,7 @@ describe('#defaultValue', () => {
duration({
defaultValue: 600,
}).validate(undefined)
).toMatchSnapshot();
).toMatchInlineSnapshot(`"PT0.6S"`);
});
test('can be a function that returns compatible type', () => {
@ -103,12 +109,12 @@ describe('#defaultValue', () => {
fromContext: duration({ defaultValue: contextRef('val') }),
}).validate({}, { val: momentDuration(700, 'ms') })
).toMatchInlineSnapshot(`
Object {
"fromContext": "PT0.7S",
"source": "PT0.6S",
"target": "PT0.6S",
}
`);
Object {
"fromContext": "PT0.7S",
"source": "PT0.6S",
"target": "PT0.6S",
}
`);
expect(
object({
@ -117,12 +123,12 @@ Object {
fromContext: duration({ defaultValue: contextRef('val') }),
}).validate({}, { val: momentDuration(2, 'hour') })
).toMatchInlineSnapshot(`
Object {
"fromContext": "PT2H",
"source": "PT1H",
"target": "PT1H",
}
`);
Object {
"fromContext": "PT2H",
"source": "PT1H",
"target": "PT1H",
}
`);
expect(
object({
@ -131,29 +137,45 @@ Object {
fromContext: duration({ defaultValue: contextRef('val') }),
}).validate({}, { val: momentDuration(2, 'hour') })
).toMatchInlineSnapshot(`
Object {
"fromContext": "PT2H",
"source": "PT1H",
"target": "PT1H",
}
`);
Object {
"fromContext": "PT2H",
"source": "PT1H",
"target": "PT1H",
}
`);
});
});
test('returns error when not valid string or non-safe positive integer', () => {
expect(() => duration().validate(-123)).toThrowErrorMatchingSnapshot();
expect(() => duration().validate(-123)).toThrowErrorMatchingInlineSnapshot(
`"Value in milliseconds is expected to be a safe positive integer."`
);
expect(() => duration().validate(NaN)).toThrowErrorMatchingSnapshot();
expect(() => duration().validate(NaN)).toThrowErrorMatchingInlineSnapshot(
`"Value in milliseconds is expected to be a safe positive integer."`
);
expect(() => duration().validate(Infinity)).toThrowErrorMatchingSnapshot();
expect(() => duration().validate(Infinity)).toThrowErrorMatchingInlineSnapshot(
`"Value in milliseconds is expected to be a safe positive integer."`
);
expect(() => duration().validate(Math.pow(2, 53))).toThrowErrorMatchingSnapshot();
expect(() => duration().validate(Math.pow(2, 53))).toThrowErrorMatchingInlineSnapshot(
`"Value in milliseconds is expected to be a safe positive integer."`
);
expect(() => duration().validate([1, 2, 3])).toThrowErrorMatchingSnapshot();
expect(() => duration().validate([1, 2, 3])).toThrowErrorMatchingInlineSnapshot(
`"expected value of type [moment.Duration] but got [Array]"`
);
expect(() => duration().validate(/abc/)).toThrowErrorMatchingSnapshot();
expect(() => duration().validate(/abc/)).toThrowErrorMatchingInlineSnapshot(
`"expected value of type [moment.Duration] but got [RegExp]"`
);
expect(() => duration().validate('123foo')).toThrowErrorMatchingSnapshot();
expect(() => duration().validate('123foo')).toThrowErrorMatchingInlineSnapshot(
`"Failed to parse value as time value. Value must be a duration in milliseconds, or follow the format <count>[ms|s|m|h|d|w|M|Y] (e.g. '70ms', '5s', '3d', '1Y'), where the duration is a safe positive integer."`
);
expect(() => duration().validate('123 456')).toThrowErrorMatchingSnapshot();
expect(() => duration().validate('123 456')).toThrowErrorMatchingInlineSnapshot(
`"Failed to parse value as time value. Value must be a duration in milliseconds, or follow the format <count>[ms|s|m|h|d|w|M|Y] (e.g. '70ms', '5s', '3d', '1Y'), where the duration is a safe positive integer."`
);
});

View file

@ -38,17 +38,29 @@ test('handles null', () => {
});
test('returns error when not correct', () => {
expect(() => literal('test').validate('foo')).toThrowErrorMatchingSnapshot();
expect(() => literal('test').validate('foo')).toThrowErrorMatchingInlineSnapshot(
`"expected value to equal [test]"`
);
expect(() => literal(true).validate(false)).toThrowErrorMatchingSnapshot();
expect(() => literal(true).validate(false)).toThrowErrorMatchingInlineSnapshot(
`"expected value to equal [true]"`
);
expect(() => literal('test').validate([1, 2, 3])).toThrowErrorMatchingSnapshot();
expect(() => literal('test').validate([1, 2, 3])).toThrowErrorMatchingInlineSnapshot(
`"expected value to equal [test]"`
);
expect(() => literal(123).validate('abc')).toThrowErrorMatchingSnapshot();
expect(() => literal(123).validate('abc')).toThrowErrorMatchingInlineSnapshot(
`"expected value to equal [123]"`
);
expect(() => literal(null).validate(42)).toThrowErrorMatchingSnapshot();
expect(() => literal(null).validate(42)).toThrowErrorMatchingInlineSnapshot(
`"expected value to equal [null]"`
);
});
test('includes namespace in failure', () => {
expect(() => literal('test').validate('foo', {}, 'foo-namespace')).toThrowErrorMatchingSnapshot();
expect(() =>
literal('test').validate('foo', {}, 'foo-namespace')
).toThrowErrorMatchingInlineSnapshot(`"[foo-namespace]: expected value to equal [test]"`);
});

View file

@ -29,7 +29,7 @@ export class LiteralType<T> extends Type<T> {
switch (type) {
case 'any.required':
case 'any.allowOnly':
return `expected value to equal [${expectedValue}] but got [${value}]`;
return `expected value to equal [${expectedValue}]`;
}
}
}

View file

@ -40,7 +40,7 @@ test('properly parse the value if input is a string', () => {
test('fails if string input cannot be parsed', () => {
const type = schema.mapOf(schema.string(), schema.string());
expect(() => type.validate(`invalidjson`)).toThrowErrorMatchingInlineSnapshot(
`"could not parse map value from [invalidjson]"`
`"could not parse map value from json input"`
);
});
@ -169,7 +169,7 @@ test('error preserves full path', () => {
expect(() =>
type.validate({ grandParentKey: { parentKey: { a: 'some-value' } } })
).toThrowErrorMatchingInlineSnapshot(
`"[grandParentKey.parentKey.key(\\"a\\")]: value is [a] but it must have a minimum length of [2]."`
`"[grandParentKey.parentKey.key(\\"a\\")]: value has length [1] but it must have a minimum length of [2]."`
);
expect(() =>

View file

@ -49,7 +49,7 @@ export class MapOfType<K, V> extends Type<Map<K, V>> {
case 'map.base':
return `expected value of type [Map] or [object] but got [${typeDetect(value)}]`;
case 'map.parse':
return `could not parse map value from [${value}]`;
return `could not parse map value from json input`;
case 'map.key':
case 'map.value':
const childPathWithIndex = path.slice();

View file

@ -42,23 +42,31 @@ test('returns undefined even if contained type has a default value', () => {
test('validates contained type', () => {
const type = schema.maybe(schema.string({ maxLength: 1 }));
expect(() => type.validate('foo')).toThrowErrorMatchingSnapshot();
expect(() => type.validate('foo')).toThrowErrorMatchingInlineSnapshot(
`"value has length [3] but it must have a maximum length of [1]."`
);
});
test('validates basic type', () => {
const type = schema.maybe(schema.string());
expect(() => type.validate(666)).toThrowErrorMatchingSnapshot();
expect(() => type.validate(666)).toThrowErrorMatchingInlineSnapshot(
`"expected value of type [string] but got [number]"`
);
});
test('fails if null', () => {
const type = schema.maybe(schema.string());
expect(() => type.validate(null)).toThrowErrorMatchingSnapshot();
expect(() => type.validate(null)).toThrowErrorMatchingInlineSnapshot(
`"expected value of type [string] but got [null]"`
);
});
test('includes namespace in failure', () => {
const type = schema.maybe(schema.string());
expect(() => type.validate(null, {}, 'foo-namespace')).toThrowErrorMatchingSnapshot();
expect(() => type.validate(null, {}, 'foo-namespace')).toThrowErrorMatchingInlineSnapshot(
`"[foo-namespace]: expected value of type [string] but got [null]"`
);
});
describe('maybe + object', () => {

View file

@ -22,10 +22,18 @@ import { schema } from '..';
test('throws on any value set', () => {
const type = schema.never();
expect(() => type.validate(1)).toThrowErrorMatchingSnapshot();
expect(() => type.validate('a')).toThrowErrorMatchingSnapshot();
expect(() => type.validate(null)).toThrowErrorMatchingSnapshot();
expect(() => type.validate({})).toThrowErrorMatchingSnapshot();
expect(() => type.validate(1)).toThrowErrorMatchingInlineSnapshot(
`"a value wasn't expected to be present"`
);
expect(() => type.validate('a')).toThrowErrorMatchingInlineSnapshot(
`"a value wasn't expected to be present"`
);
expect(() => type.validate(null)).toThrowErrorMatchingInlineSnapshot(
`"a value wasn't expected to be present"`
);
expect(() => type.validate({})).toThrowErrorMatchingInlineSnapshot(
`"a value wasn't expected to be present"`
);
expect(() => type.validate(undefined)).not.toThrow();
});
@ -37,7 +45,7 @@ test('throws on value set as object property', () => {
expect(() =>
type.validate({ name: 'name', status: 'in progress' })
).toThrowErrorMatchingSnapshot();
).toThrowErrorMatchingInlineSnapshot(`"[name]: a value wasn't expected to be present"`);
expect(() => type.validate({ status: 'in progress' })).not.toThrow();
expect(() => type.validate({ name: undefined, status: 'in progress' })).not.toThrow();
@ -71,5 +79,5 @@ test('works for conditional types', () => {
context_value_2: 1,
}
)
).toThrowErrorMatchingSnapshot();
).toThrowErrorMatchingInlineSnapshot(`"[name]: a value wasn't expected to be present"`);
});

View file

@ -94,13 +94,21 @@ test('returns null even if contained type has a default value', () => {
test('validates contained type', () => {
const type = schema.nullable(schema.string({ maxLength: 1 }));
expect(() => type.validate('foo')).toThrowErrorMatchingSnapshot();
expect(() => type.validate('foo')).toThrowErrorMatchingInlineSnapshot(`
"types that failed validation:
- [0]: value has length [3] but it must have a maximum length of [1].
- [1]: expected value to equal [null]"
`);
});
test('validates basic type', () => {
const type = schema.nullable(schema.string());
expect(() => type.validate(666)).toThrowErrorMatchingSnapshot();
expect(() => type.validate(666)).toThrowErrorMatchingInlineSnapshot(`
"types that failed validation:
- [0]: expected value of type [string] but got [number]
- [1]: expected value to equal [null]"
`);
});
test('validates type in object', () => {
@ -121,11 +129,19 @@ test('validates type errors in object', () => {
bar: schema.nullable(schema.boolean()),
});
expect(() => type.validate({ foo: 'ab' })).toThrowErrorMatchingSnapshot();
expect(() => type.validate({ foo: 'ab' })).toThrowErrorMatchingInlineSnapshot(`
"[foo]: types that failed validation:
- [foo.0]: value has length [2] but it must have a maximum length of [1].
- [foo.1]: expected value to equal [null]"
`);
});
test('includes namespace in failure', () => {
const type = schema.nullable(schema.string({ maxLength: 1 }));
expect(() => type.validate('foo', {}, 'foo-namespace')).toThrowErrorMatchingSnapshot();
expect(() => type.validate('foo', {}, 'foo-namespace')).toThrowErrorMatchingInlineSnapshot(`
"[foo-namespace]: types that failed validation:
- [foo-namespace.0]: value has length [3] but it must have a maximum length of [1].
- [foo-namespace.1]: expected value to equal [null]"
`);
});

View file

@ -32,17 +32,23 @@ test('handles numeric strings with floats', () => {
});
test('fails if number is `NaN`', () => {
expect(() => schema.number().validate(NaN)).toThrowErrorMatchingSnapshot();
expect(() => schema.number().validate(NaN)).toThrowErrorMatchingInlineSnapshot(
`"expected value of type [number] but got [number]"`
);
});
test('is required by default', () => {
expect(() => schema.number().validate(undefined)).toThrowErrorMatchingSnapshot();
expect(() => schema.number().validate(undefined)).toThrowErrorMatchingInlineSnapshot(
`"expected value of type [number] but got [undefined]"`
);
});
test('includes namespace in failure', () => {
expect(() =>
schema.number().validate(undefined, {}, 'foo-namespace')
).toThrowErrorMatchingSnapshot();
).toThrowErrorMatchingInlineSnapshot(
`"[foo-namespace]: expected value of type [number] but got [undefined]"`
);
});
describe('#min', () => {
@ -51,7 +57,9 @@ describe('#min', () => {
});
test('returns error when smaller number', () => {
expect(() => schema.number({ min: 4 }).validate(3)).toThrowErrorMatchingSnapshot();
expect(() => schema.number({ min: 4 }).validate(3)).toThrowErrorMatchingInlineSnapshot(
`"Value must be equal to or greater than [4]."`
);
});
});
@ -61,7 +69,9 @@ describe('#max', () => {
});
test('returns error when larger number', () => {
expect(() => schema.number({ max: 2 }).validate(3)).toThrowErrorMatchingSnapshot();
expect(() => schema.number({ max: 2 }).validate(3)).toThrowErrorMatchingInlineSnapshot(
`"Value must be equal to or lower than [2]."`
);
});
});
@ -76,9 +86,15 @@ describe('#defaultValue', () => {
});
test('returns error when not number or numeric string', () => {
expect(() => schema.number().validate('test')).toThrowErrorMatchingSnapshot();
expect(() => schema.number().validate('test')).toThrowErrorMatchingInlineSnapshot(
`"expected value of type [number] but got [string]"`
);
expect(() => schema.number().validate([1, 2, 3])).toThrowErrorMatchingSnapshot();
expect(() => schema.number().validate([1, 2, 3])).toThrowErrorMatchingInlineSnapshot(
`"expected value of type [number] but got [Array]"`
);
expect(() => schema.number().validate(/abc/)).toThrowErrorMatchingSnapshot();
expect(() => schema.number().validate(/abc/)).toThrowErrorMatchingInlineSnapshot(
`"expected value of type [number] but got [RegExp]"`
);
});

View file

@ -46,9 +46,9 @@ export class NumberType extends Type<number> {
case 'number.base':
return `expected value of type [number] but got [${typeDetect(value)}]`;
case 'number.min':
return `Value is [${value}] but it must be equal to or greater than [${limit}].`;
return `Value must be equal to or greater than [${limit}].`;
case 'number.max':
return `Value is [${value}] but it must be equal to or lower than [${limit}].`;
return `Value must be equal to or lower than [${limit}].`;
}
}
}

View file

@ -49,7 +49,7 @@ test('fails if string input cannot be parsed', () => {
name: schema.string(),
});
expect(() => type.validate(`invalidjson`)).toThrowErrorMatchingInlineSnapshot(
`"could not parse object value from [invalidjson]"`
`"could not parse object value from json input"`
);
});
@ -181,7 +181,7 @@ test('called with wrong type', () => {
const type = schema.object({});
expect(() => type.validate('foo')).toThrowErrorMatchingInlineSnapshot(
`"could not parse object value from [foo]"`
`"could not parse object value from json input"`
);
expect(() => type.validate(123)).toThrowErrorMatchingInlineSnapshot(
`"expected a plain object value, but found [number] instead."`

View file

@ -62,7 +62,7 @@ export class ObjectType<P extends Props = any> extends Type<ObjectResultType<P>>
case 'object.base':
return `expected a plain object value, but found [${typeDetect(value)}] instead.`;
case 'object.parse':
return `could not parse object value from [${value}]`;
return `could not parse object value from json input`;
case 'object.allowUnknown':
return `definition for this key is missing`;
case 'object.child':

View file

@ -75,13 +75,20 @@ test('handles object', () => {
test('handles object with wrong type', () => {
const type = schema.oneOf([schema.object({ age: schema.number() })]);
expect(() => type.validate({ age: 'foo' })).toThrowErrorMatchingSnapshot();
expect(() => type.validate({ age: 'foo' })).toThrowErrorMatchingInlineSnapshot(`
"types that failed validation:
- [0.age]: expected value of type [number] but got [string]"
`);
});
test('includes namespace in failure', () => {
const type = schema.oneOf([schema.object({ age: schema.number() })]);
expect(() => type.validate({ age: 'foo' }, {}, 'foo-namespace')).toThrowErrorMatchingSnapshot();
expect(() => type.validate({ age: 'foo' }, {}, 'foo-namespace'))
.toThrowErrorMatchingInlineSnapshot(`
"[foo-namespace]: types that failed validation:
- [foo-namespace.0.age]: expected value of type [number] but got [string]"
`);
});
test('handles multiple objects with same key', () => {
@ -110,20 +117,33 @@ test('handles maybe', () => {
test('fails if not matching type', () => {
const type = schema.oneOf([schema.string()]);
expect(() => type.validate(false)).toThrowErrorMatchingSnapshot();
expect(() => type.validate(123)).toThrowErrorMatchingSnapshot();
expect(() => type.validate(false)).toThrowErrorMatchingInlineSnapshot(`
"types that failed validation:
- [0]: expected value of type [string] but got [boolean]"
`);
expect(() => type.validate(123)).toThrowErrorMatchingInlineSnapshot(`
"types that failed validation:
- [0]: expected value of type [string] but got [number]"
`);
});
test('fails if not matching multiple types', () => {
const type = schema.oneOf([schema.string(), schema.number()]);
expect(() => type.validate(false)).toThrowErrorMatchingSnapshot();
expect(() => type.validate(false)).toThrowErrorMatchingInlineSnapshot(`
"types that failed validation:
- [0]: expected value of type [string] but got [boolean]
- [1]: expected value of type [number] but got [boolean]"
`);
});
test('fails if not matching literal', () => {
const type = schema.oneOf([schema.literal('foo')]);
expect(() => type.validate('bar')).toThrowErrorMatchingSnapshot();
expect(() => type.validate('bar')).toThrowErrorMatchingInlineSnapshot(`
"types that failed validation:
- [0]: expected value to equal [foo]"
`);
});
test('fails if nested union type fail', () => {
@ -138,7 +158,7 @@ test('fails if nested union type fail', () => {
- [0]: expected value of type [boolean] but got [string]
- [1]: types that failed validation:
- [0]: types that failed validation:
- [0]: could not parse object value from [aaa]
- [0]: could not parse object value from json input
- [1]: expected value of type [number] but got [string]"
`);
});

View file

@ -73,8 +73,8 @@ test('fails when not receiving expected key type', () => {
expect(() => type.validate(value)).toThrowErrorMatchingInlineSnapshot(`
"[key(\\"name\\")]: types that failed validation:
- [0]: expected value to equal [nickName] but got [name]
- [1]: expected value to equal [lastName] but got [name]"
- [0]: expected value to equal [nickName]
- [1]: expected value to equal [lastName]"
`);
});
@ -88,8 +88,8 @@ test('fails after parsing when not receiving expected key type', () => {
expect(() => type.validate(value)).toThrowErrorMatchingInlineSnapshot(`
"[key(\\"name\\")]: types that failed validation:
- [0]: expected value to equal [nickName] but got [name]
- [1]: expected value to equal [lastName] but got [name]"
- [0]: expected value to equal [nickName]
- [1]: expected value to equal [lastName]"
`);
});
@ -118,7 +118,7 @@ test('includes namespace in failure when wrong key type', () => {
};
expect(() => type.validate(value, {}, 'foo-namespace')).toThrowErrorMatchingInlineSnapshot(
`"[foo-namespace.key(\\"name\\")]: value is [name] but it must have a minimum length of [10]."`
`"[foo-namespace.key(\\"name\\")]: value has length [4] but it must have a minimum length of [10]."`
);
});
@ -169,7 +169,7 @@ test('error preserves full path', () => {
expect(() =>
type.validate({ grandParentKey: { parentKey: { a: 'some-value' } } })
).toThrowErrorMatchingInlineSnapshot(
`"[grandParentKey.parentKey.key(\\"a\\")]: value is [a] but it must have a minimum length of [2]."`
`"[grandParentKey.parentKey.key(\\"a\\")]: value has length [1] but it must have a minimum length of [2]."`
);
expect(() =>

View file

@ -41,7 +41,7 @@ export class RecordOfType<K extends string, V> extends Type<Record<K, V>> {
case 'record.base':
return `expected value of type [object] but got [${typeDetect(value)}]`;
case 'record.parse':
return `could not parse record value from [${value}]`;
return `could not parse record value from json input`;
case 'record.key':
case 'record.value':
const childPathWithIndex = path.slice();

View file

@ -41,13 +41,17 @@ test('Passthrough is valid', () => {
});
test('is required by default', () => {
expect(() => schema.buffer().validate(undefined)).toThrowErrorMatchingSnapshot();
expect(() => schema.buffer().validate(undefined)).toThrowErrorMatchingInlineSnapshot(
`"expected value of type [Buffer] but got [undefined]"`
);
});
test('includes namespace in failure', () => {
expect(() =>
schema.stream().validate(undefined, {}, 'foo-namespace')
).toThrowErrorMatchingSnapshot();
).toThrowErrorMatchingInlineSnapshot(
`"[foo-namespace]: expected value of type [Stream] but got [undefined]"`
);
});
describe('#defaultValue', () => {
@ -63,9 +67,15 @@ describe('#defaultValue', () => {
});
test('returns error when not a stream', () => {
expect(() => schema.stream().validate(123)).toThrowErrorMatchingSnapshot();
expect(() => schema.stream().validate(123)).toThrowErrorMatchingInlineSnapshot(
`"expected value of type [Stream] but got [number]"`
);
expect(() => schema.stream().validate([1, 2, 3])).toThrowErrorMatchingSnapshot();
expect(() => schema.stream().validate([1, 2, 3])).toThrowErrorMatchingInlineSnapshot(
`"expected value of type [Stream] but got [Array]"`
);
expect(() => schema.stream().validate('abc')).toThrowErrorMatchingSnapshot();
expect(() => schema.stream().validate('abc')).toThrowErrorMatchingInlineSnapshot(
`"expected value of type [Stream] but got [string]"`
);
});

View file

@ -28,13 +28,17 @@ test('allows empty strings', () => {
});
test('is required by default', () => {
expect(() => schema.string().validate(undefined)).toThrowErrorMatchingSnapshot();
expect(() => schema.string().validate(undefined)).toThrowErrorMatchingInlineSnapshot(
`"expected value of type [string] but got [undefined]"`
);
});
test('includes namespace in failure', () => {
expect(() =>
schema.string().validate(undefined, {}, 'foo-namespace')
).toThrowErrorMatchingSnapshot();
).toThrowErrorMatchingInlineSnapshot(
`"[foo-namespace]: expected value of type [string] but got [undefined]"`
);
});
describe('#minLength', () => {
@ -43,11 +47,17 @@ describe('#minLength', () => {
});
test('returns error when shorter string', () => {
expect(() => schema.string({ minLength: 4 }).validate('foo')).toThrowErrorMatchingSnapshot();
expect(() =>
schema.string({ minLength: 4 }).validate('foo')
).toThrowErrorMatchingInlineSnapshot(
`"value has length [3] but it must have a minimum length of [4]."`
);
});
test('returns error when empty string', () => {
expect(() => schema.string({ minLength: 2 }).validate('')).toThrowErrorMatchingSnapshot();
expect(() => schema.string({ minLength: 2 }).validate('')).toThrowErrorMatchingInlineSnapshot(
`"value has length [0] but it must have a minimum length of [2]."`
);
});
});
@ -57,7 +67,11 @@ describe('#maxLength', () => {
});
test('returns error when longer string', () => {
expect(() => schema.string({ maxLength: 2 }).validate('foo')).toThrowErrorMatchingSnapshot();
expect(() =>
schema.string({ maxLength: 2 }).validate('foo')
).toThrowErrorMatchingInlineSnapshot(
`"value has length [3] but it must have a maximum length of [2]."`
);
});
});
@ -84,23 +98,37 @@ describe('#hostname', () => {
test('returns error when value is not a valid hostname', () => {
const hostNameSchema = schema.string({ hostname: true });
expect(() => hostNameSchema.validate('host:name')).toThrowErrorMatchingSnapshot();
expect(() => hostNameSchema.validate('localhost:5601')).toThrowErrorMatchingSnapshot();
expect(() => hostNameSchema.validate('-')).toThrowErrorMatchingSnapshot();
expect(() => hostNameSchema.validate('0:?:0:0:0:0:0:1')).toThrowErrorMatchingSnapshot();
expect(() => hostNameSchema.validate('host:name')).toThrowErrorMatchingInlineSnapshot(
`"value must be a valid hostname (see RFC 1123)."`
);
expect(() => hostNameSchema.validate('localhost:5601')).toThrowErrorMatchingInlineSnapshot(
`"value must be a valid hostname (see RFC 1123)."`
);
expect(() => hostNameSchema.validate('-')).toThrowErrorMatchingInlineSnapshot(
`"value must be a valid hostname (see RFC 1123)."`
);
expect(() => hostNameSchema.validate('0:?:0:0:0:0:0:1')).toThrowErrorMatchingInlineSnapshot(
`"value must be a valid hostname (see RFC 1123)."`
);
const tooLongHostName = 'a'.repeat(256);
expect(() => hostNameSchema.validate(tooLongHostName)).toThrowErrorMatchingSnapshot();
expect(() => hostNameSchema.validate(tooLongHostName)).toThrowErrorMatchingInlineSnapshot(
`"value must be a valid hostname (see RFC 1123)."`
);
});
test('returns error when empty string', () => {
expect(() => schema.string({ hostname: true }).validate('')).toThrowErrorMatchingSnapshot();
expect(() => schema.string({ hostname: true }).validate('')).toThrowErrorMatchingInlineSnapshot(
`"any.empty"`
);
});
test('supports string validation rules', () => {
expect(() =>
schema.string({ hostname: true, maxLength: 3 }).validate('www.example.com')
).toThrowErrorMatchingSnapshot();
).toThrowErrorMatchingInlineSnapshot(
`"value has length [15] but it must have a maximum length of [3]."`
);
});
});
@ -146,20 +174,30 @@ describe('#validate', () => {
test('throws when returns string', () => {
const validate = () => 'validator failure';
expect(() => schema.string({ validate }).validate('foo')).toThrowErrorMatchingSnapshot();
expect(() => schema.string({ validate }).validate('foo')).toThrowErrorMatchingInlineSnapshot(
`"validator failure"`
);
});
test('throw when empty string', () => {
const validate = () => 'validator failure';
expect(() => schema.string({ validate }).validate('')).toThrowErrorMatchingSnapshot();
expect(() => schema.string({ validate }).validate('')).toThrowErrorMatchingInlineSnapshot(
`"validator failure"`
);
});
});
test('returns error when not string', () => {
expect(() => schema.string().validate(123)).toThrowErrorMatchingSnapshot();
expect(() => schema.string().validate(123)).toThrowErrorMatchingInlineSnapshot(
`"expected value of type [string] but got [number]"`
);
expect(() => schema.string().validate([1, 2, 3])).toThrowErrorMatchingSnapshot();
expect(() => schema.string().validate([1, 2, 3])).toThrowErrorMatchingInlineSnapshot(
`"expected value of type [string] but got [Array]"`
);
expect(() => schema.string().validate(/abc/)).toThrowErrorMatchingSnapshot();
expect(() => schema.string().validate(/abc/)).toThrowErrorMatchingInlineSnapshot(
`"expected value of type [string] but got [RegExp]"`
);
});

View file

@ -45,7 +45,7 @@ export class StringType extends Type<string> {
if (options.minLength !== undefined) {
schema = schema.custom(value => {
if (value.length < options.minLength!) {
return `value is [${value}] but it must have a minimum length of [${options.minLength}].`;
return `value has length [${value.length}] but it must have a minimum length of [${options.minLength}].`;
}
});
}
@ -53,7 +53,7 @@ export class StringType extends Type<string> {
if (options.maxLength !== undefined) {
schema = schema.custom(value => {
if (value.length > options.maxLength!) {
return `value is [${value}] but it must have a maximum length of [${options.maxLength}].`;
return `value has length [${value.length}] but it must have a maximum length of [${options.maxLength}].`;
}
});
}
@ -66,7 +66,7 @@ export class StringType extends Type<string> {
case 'any.required':
return `expected value of type [string] but got [${typeDetect(value)}]`;
case 'string.hostname':
return `value is [${value}] but it must be a valid hostname (see RFC 1123).`;
return `value must be a valid hostname (see RFC 1123).`;
}
}
}

View file

@ -20,7 +20,9 @@
import { schema } from '..';
test('is required by default', () => {
expect(() => schema.uri().validate(undefined)).toThrowErrorMatchingSnapshot();
expect(() => schema.uri().validate(undefined)).toThrowErrorMatchingInlineSnapshot(
`"expected value of type [string] but got [undefined]."`
);
});
test('returns value for valid URI as per RFC3986', () => {
@ -54,17 +56,23 @@ test('returns value for valid URI as per RFC3986', () => {
test('returns error when value is not a URI', () => {
const uriSchema = schema.uri();
expect(() => uriSchema.validate('3domain.local')).toThrowErrorMatchingSnapshot();
expect(() => uriSchema.validate('3domain.local')).toThrowErrorMatchingInlineSnapshot(
`"value must be a valid URI (see RFC 3986)."`
);
expect(() =>
uriSchema.validate('http://8010:0:0:0:9:500:300C:200A')
).toThrowErrorMatchingSnapshot();
expect(() => uriSchema.validate('-')).toThrowErrorMatchingSnapshot();
).toThrowErrorMatchingInlineSnapshot(`"value must be a valid URI (see RFC 3986)."`);
expect(() => uriSchema.validate('-')).toThrowErrorMatchingInlineSnapshot(
`"value must be a valid URI (see RFC 3986)."`
);
expect(() =>
uriSchema.validate('https://example.com?baz[]=foo&baz[]=bar')
).toThrowErrorMatchingSnapshot();
).toThrowErrorMatchingInlineSnapshot(`"value must be a valid URI (see RFC 3986)."`);
const tooLongUri = `http://${'a'.repeat(256)}`;
expect(() => uriSchema.validate(tooLongUri)).toThrowErrorMatchingSnapshot();
expect(() => uriSchema.validate(tooLongUri)).toThrowErrorMatchingInlineSnapshot(
`"value must be a valid URI (see RFC 3986)."`
);
});
describe('#scheme', () => {
@ -78,8 +86,12 @@ describe('#scheme', () => {
test('returns error when shorter string', () => {
const uriSchema = schema.uri({ scheme: ['http', 'https'] });
expect(() => uriSchema.validate('ftp://elastic.co')).toThrowErrorMatchingSnapshot();
expect(() => uriSchema.validate('file:///kibana.log')).toThrowErrorMatchingSnapshot();
expect(() => uriSchema.validate('ftp://elastic.co')).toThrowErrorMatchingInlineSnapshot(
`"expected URI with scheme [http|https]."`
);
expect(() => uriSchema.validate('file:///kibana.log')).toThrowErrorMatchingInlineSnapshot(
`"expected URI with scheme [http|https]."`
);
});
});
@ -131,14 +143,20 @@ describe('#validate', () => {
expect(() =>
schema.uri({ validate }).validate('http://kibana.local')
).toThrowErrorMatchingSnapshot();
).toThrowErrorMatchingInlineSnapshot(`"validator failure"`);
});
});
test('returns error when not string', () => {
expect(() => schema.uri().validate(123)).toThrowErrorMatchingSnapshot();
expect(() => schema.uri().validate(123)).toThrowErrorMatchingInlineSnapshot(
`"expected value of type [string] but got [number]."`
);
expect(() => schema.uri().validate([1, 2, 3])).toThrowErrorMatchingSnapshot();
expect(() => schema.uri().validate([1, 2, 3])).toThrowErrorMatchingInlineSnapshot(
`"expected value of type [string] but got [Array]."`
);
expect(() => schema.uri().validate(/abc/)).toThrowErrorMatchingSnapshot();
expect(() => schema.uri().validate(/abc/)).toThrowErrorMatchingInlineSnapshot(
`"expected value of type [string] but got [RegExp]."`
);
});

View file

@ -36,9 +36,9 @@ export class URIType extends Type<string> {
case 'string.base':
return `expected value of type [string] but got [${typeDetect(value)}].`;
case 'string.uriCustomScheme':
return `expected URI with scheme [${scheme}] but got [${value}].`;
return `expected URI with scheme [${scheme}].`;
case 'string.uri':
return `value is [${value}] but it must be a valid URI (see RFC 3986).`;
return `value must be a valid URI (see RFC 3986).`;
}
}
}

View file

@ -87,7 +87,7 @@ exports[`throws if basepath is missing prepended slash 1`] = `"[basePath]: must
exports[`throws if basepath is not specified, but rewriteBasePath is set 1`] = `"cannot use [rewriteBasePath] when [basePath] is not specified"`;
exports[`throws if invalid hostname 1`] = `"[host]: value is [asdf$%^] but it must be a valid hostname (see RFC 1123)."`;
exports[`throws if invalid hostname 1`] = `"[host]: value must be a valid hostname (see RFC 1123)."`;
exports[`with TLS throws if TLS is enabled but \`redirectHttpFromPort\` is equal to \`port\` 1`] = `"Kibana does not accept http traffic to [port] when ssl is enabled (only https is allowed), so [ssl.redirectHttpFromPort] cannot be configured to the same value. Both are [1234]."`;
@ -100,7 +100,7 @@ Array [
]
`;
exports[`with compression throws if invalid referrer whitelist 1`] = `"[compression.referrerWhitelist.0]: value is [asdf$%^] but it must be a valid hostname (see RFC 1123)."`;
exports[`with compression throws if invalid referrer whitelist 1`] = `"[compression.referrerWhitelist.0]: value must be a valid hostname (see RFC 1123)."`;
exports[`with compression throws if invalid referrer whitelist 2`] = `"[compression.referrerWhitelist]: array size is [0], but cannot be smaller than [1]"`;

View file

@ -293,16 +293,16 @@ describe('#sslSchema', () => {
expect(() => sslSchema.validate(singleUnknownProtocol)).toThrowErrorMatchingInlineSnapshot(`
"[supportedProtocols.0]: types that failed validation:
- [supportedProtocols.0.0]: expected value to equal [TLSv1] but got [SOMEv100500]
- [supportedProtocols.0.1]: expected value to equal [TLSv1.1] but got [SOMEv100500]
- [supportedProtocols.0.2]: expected value to equal [TLSv1.2] but got [SOMEv100500]"
- [supportedProtocols.0.0]: expected value to equal [TLSv1]
- [supportedProtocols.0.1]: expected value to equal [TLSv1.1]
- [supportedProtocols.0.2]: expected value to equal [TLSv1.2]"
`);
expect(() => sslSchema.validate(allKnownWithOneUnknownProtocols))
.toThrowErrorMatchingInlineSnapshot(`
"[supportedProtocols.3]: types that failed validation:
- [supportedProtocols.3.0]: expected value to equal [TLSv1] but got [SOMEv100500]
- [supportedProtocols.3.1]: expected value to equal [TLSv1.1] but got [SOMEv100500]
- [supportedProtocols.3.2]: expected value to equal [TLSv1.2] but got [SOMEv100500]"
- [supportedProtocols.3.0]: expected value to equal [TLSv1]
- [supportedProtocols.3.1]: expected value to equal [TLSv1.1]
- [supportedProtocols.3.2]: expected value to equal [TLSv1.2]"
`);
});
});

View file

@ -93,7 +93,7 @@ export default function({ getService }) {
.expect(400)
.then(resp => {
expect(resp.body.message).to.contain(
'[request query.look_back]: Value is [0] but it must be equal to or greater than [1].'
'[request query.look_back]: Value must be equal to or greater than [1].'
);
}));
});

View file

@ -76,7 +76,7 @@ describe('config validation', () => {
}).toThrowErrorMatchingInlineSnapshot(`
"error validating action type config: [index]: types that failed validation:
- [index.0]: expected value of type [string] but got [number]
- [index.1]: expected value to equal [null] but got [666]"
- [index.1]: expected value to equal [null]"
`);
});
});
@ -150,7 +150,7 @@ describe('params validation', () => {
expect(() => {
validateParams(actionType, { documents: ['should be an object'] });
}).toThrowErrorMatchingInlineSnapshot(
`"error validating action params: [documents.0]: could not parse record value from [should be an object]"`
`"error validating action params: [documents.0]: could not parse record value from json input"`
);
});
});

View file

@ -137,9 +137,9 @@ describe('validateParams()', () => {
validateParams(actionType, { eventAction: 'ackynollage' });
}).toThrowErrorMatchingInlineSnapshot(`
"error validating action params: [eventAction]: types that failed validation:
- [eventAction.0]: expected value to equal [trigger] but got [ackynollage]
- [eventAction.1]: expected value to equal [resolve] but got [ackynollage]
- [eventAction.2]: expected value to equal [acknowledge] but got [ackynollage]"
- [eventAction.0]: expected value to equal [trigger]
- [eventAction.1]: expected value to equal [resolve]
- [eventAction.2]: expected value to equal [acknowledge]"
`);
});
});

View file

@ -63,24 +63,24 @@ describe('validateParams()', () => {
validateParams(actionType, { message: 'x', level: 2 });
}).toThrowErrorMatchingInlineSnapshot(`
"error validating action params: [level]: types that failed validation:
- [level.0]: expected value to equal [trace] but got [2]
- [level.1]: expected value to equal [debug] but got [2]
- [level.2]: expected value to equal [info] but got [2]
- [level.3]: expected value to equal [warn] but got [2]
- [level.4]: expected value to equal [error] but got [2]
- [level.5]: expected value to equal [fatal] but got [2]"
- [level.0]: expected value to equal [trace]
- [level.1]: expected value to equal [debug]
- [level.2]: expected value to equal [info]
- [level.3]: expected value to equal [warn]
- [level.4]: expected value to equal [error]
- [level.5]: expected value to equal [fatal]"
`);
expect(() => {
validateParams(actionType, { message: 'x', level: 'foo' });
}).toThrowErrorMatchingInlineSnapshot(`
"error validating action params: [level]: types that failed validation:
- [level.0]: expected value to equal [trace] but got [foo]
- [level.1]: expected value to equal [debug] but got [foo]
- [level.2]: expected value to equal [info] but got [foo]
- [level.3]: expected value to equal [warn] but got [foo]
- [level.4]: expected value to equal [error] but got [foo]
- [level.5]: expected value to equal [fatal] but got [foo]"
- [level.0]: expected value to equal [trace]
- [level.1]: expected value to equal [debug]
- [level.2]: expected value to equal [info]
- [level.3]: expected value to equal [warn]
- [level.4]: expected value to equal [error]
- [level.5]: expected value to equal [fatal]"
`);
});
});

View file

@ -104,8 +104,8 @@ describe('config validation', () => {
validateConfig(actionType, config);
}).toThrowErrorMatchingInlineSnapshot(`
"error validating action type config: [method]: types that failed validation:
- [method.0]: expected value to equal [post] but got [https]
- [method.1]: expected value to equal [put] but got [https]"
- [method.0]: expected value to equal [post]
- [method.1]: expected value to equal [put]"
`);
});
@ -141,8 +141,8 @@ describe('config validation', () => {
validateConfig(actionType, config);
}).toThrowErrorMatchingInlineSnapshot(`
"error validating action type config: [headers]: types that failed validation:
- [headers.0]: could not parse record value from [application/json]
- [headers.1]: expected value to equal [null] but got [application/json]"
- [headers.0]: could not parse record value from json input
- [headers.1]: expected value to equal [null]"
`);
});

View file

@ -64,15 +64,15 @@ export function runTests(schema: ObjectType, defaultTypeParams: Record<string, a
params.index = '';
expect(onValidate()).toThrowErrorMatchingInlineSnapshot(`
"[index]: types that failed validation:
- [index.0]: value is [] but it must have a minimum length of [1].
- [index.1]: could not parse array value from []"
- [index.0]: value has length [0] but it must have a minimum length of [1].
- [index.1]: could not parse array value from json input"
`);
params.index = ['', 'a'];
expect(onValidate()).toThrowErrorMatchingInlineSnapshot(`
"[index]: types that failed validation:
- [index.0]: expected value of type [string] but got [Array]
- [index.1.0]: value is [] but it must have a minimum length of [1]."
- [index.1.0]: value has length [0] but it must have a minimum length of [1]."
`);
});
@ -89,7 +89,7 @@ export function runTests(schema: ObjectType, defaultTypeParams: Record<string, a
params.timeField = '';
expect(onValidate()).toThrowErrorMatchingInlineSnapshot(
`"[timeField]: value is [] but it must have a minimum length of [1]."`
`"[timeField]: value has length [0] but it must have a minimum length of [1]."`
);
});
@ -113,7 +113,7 @@ export function runTests(schema: ObjectType, defaultTypeParams: Record<string, a
params.aggField = '';
expect(onValidate()).toThrowErrorMatchingInlineSnapshot(
`"[aggField]: value is [] but it must have a minimum length of [1]."`
`"[aggField]: value has length [0] but it must have a minimum length of [1]."`
);
});
@ -126,7 +126,7 @@ export function runTests(schema: ObjectType, defaultTypeParams: Record<string, a
params.termField = '';
expect(onValidate()).toThrowErrorMatchingInlineSnapshot(
`"[termField]: value is [] but it must have a minimum length of [1]."`
`"[termField]: value has length [0] but it must have a minimum length of [1]."`
);
});
@ -145,7 +145,7 @@ export function runTests(schema: ObjectType, defaultTypeParams: Record<string, a
params.termSize = 0;
expect(onValidate()).toThrowErrorMatchingInlineSnapshot(
`"[termSize]: Value is [0] but it must be equal to or greater than [1]."`
`"[termSize]: Value must be equal to or greater than [1]."`
);
});
@ -157,7 +157,7 @@ export function runTests(schema: ObjectType, defaultTypeParams: Record<string, a
params.timeWindowSize = 0;
expect(onValidate()).toThrowErrorMatchingInlineSnapshot(
`"[timeWindowSize]: Value is [0] but it must be equal to or greater than [1]."`
`"[timeWindowSize]: Value must be equal to or greater than [1]."`
);
});

View file

@ -37,13 +37,13 @@ describe('config schema', () => {
expect(() =>
ConfigSchema.validate({ encryptionKey: 'foo' })
).toThrowErrorMatchingInlineSnapshot(
`"[encryptionKey]: value is [foo] but it must have a minimum length of [32]."`
`"[encryptionKey]: value has length [3] but it must have a minimum length of [32]."`
);
expect(() =>
ConfigSchema.validate({ encryptionKey: 'foo' }, { dist: true })
).toThrowErrorMatchingInlineSnapshot(
`"[encryptionKey]: value is [foo] but it must have a minimum length of [32]."`
`"[encryptionKey]: value has length [3] but it must have a minimum length of [32]."`
);
});
});

View file

@ -103,13 +103,13 @@ describe('config schema', () => {
expect(() =>
ConfigSchema.validate({ encryptionKey: 'foo' })
).toThrowErrorMatchingInlineSnapshot(
`"[encryptionKey]: value is [foo] but it must have a minimum length of [32]."`
`"[encryptionKey]: value has length [3] but it must have a minimum length of [32]."`
);
expect(() =>
ConfigSchema.validate({ encryptionKey: 'foo' }, { dist: true })
).toThrowErrorMatchingInlineSnapshot(
`"[encryptionKey]: value is [foo] but it must have a minimum length of [32]."`
`"[encryptionKey]: value has length [3] but it must have a minimum length of [32]."`
);
});

View file

@ -85,17 +85,17 @@ describe('Basic authentication routes', () => {
expect(() =>
bodyValidator.validate({ username: '', password: '' })
).toThrowErrorMatchingInlineSnapshot(
`"[username]: value is [] but it must have a minimum length of [1]."`
`"[username]: value has length [0] but it must have a minimum length of [1]."`
);
expect(() =>
bodyValidator.validate({ username: 'user', password: '' })
).toThrowErrorMatchingInlineSnapshot(
`"[password]: value is [] but it must have a minimum length of [1]."`
`"[password]: value has length [0] but it must have a minimum length of [1]."`
);
expect(() =>
bodyValidator.validate({ username: '', password: 'password' })
).toThrowErrorMatchingInlineSnapshot(
`"[username]: value is [] but it must have a minimum length of [1]."`
`"[username]: value has length [0] but it must have a minimum length of [1]."`
);
});

View file

@ -104,7 +104,7 @@ describe('Put payload schema', () => {
})
).toThrowErrorMatchingInlineSnapshot(`
"[kibana.0.spaces]: types that failed validation:
- [kibana.0.spaces.0.0]: expected value to equal [*] but got [foo-*]
- [kibana.0.spaces.0.0]: expected value to equal [*]
- [kibana.0.spaces.1.0]: must be lower case, a-z, 0-9, '_', and '-' are allowed"
`);
});
@ -116,7 +116,7 @@ describe('Put payload schema', () => {
})
).toThrowErrorMatchingInlineSnapshot(`
"[kibana.0.spaces]: types that failed validation:
- [kibana.0.spaces.0.1]: expected value to equal [*] but got [foo-space]
- [kibana.0.spaces.0.1]: expected value to equal [*]
- [kibana.0.spaces.1.0]: must be lower case, a-z, 0-9, '_', and '-' are allowed"
`);
});

View file

@ -124,7 +124,7 @@ describe('PUT role', () => {
expect(() =>
requestParamsSchema.validate({ name: '' }, {}, 'request params')
).toThrowErrorMatchingInlineSnapshot(
`"[request params.name]: value is [] but it must have a minimum length of [1]."`
`"[request params.name]: value has length [0] but it must have a minimum length of [1]."`
);
});
@ -132,7 +132,7 @@ describe('PUT role', () => {
expect(() =>
requestParamsSchema.validate({ name: 'a'.repeat(1025) }, {}, 'request params')
).toThrowErrorMatchingInlineSnapshot(
`"[request params.name]: value is [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa] but it must have a maximum length of [1024]."`
`"[request params.name]: value has length [1025] but it must have a maximum length of [1024]."`
);
});
});

View file

@ -82,12 +82,12 @@ describe('Change password', () => {
`"[username]: expected value of type [string] but got [undefined]"`
);
expect(() => paramsSchema.validate({ username: '' })).toThrowErrorMatchingInlineSnapshot(
`"[username]: value is [] but it must have a minimum length of [1]."`
`"[username]: value has length [0] but it must have a minimum length of [1]."`
);
expect(() =>
paramsSchema.validate({ username: 'a'.repeat(1025) })
).toThrowErrorMatchingInlineSnapshot(
`"[username]: value is [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa] but it must have a maximum length of [1024]."`
`"[username]: value has length [1025] but it must have a maximum length of [1024]."`
);
const bodySchema = (routeConfig.validate as any).body as ObjectType;
@ -95,12 +95,12 @@ describe('Change password', () => {
`"[newPassword]: expected value of type [string] but got [undefined]"`
);
expect(() => bodySchema.validate({ newPassword: '' })).toThrowErrorMatchingInlineSnapshot(
`"[newPassword]: value is [] but it must have a minimum length of [1]."`
`"[newPassword]: value has length [0] but it must have a minimum length of [1]."`
);
expect(() =>
bodySchema.validate({ newPassword: '123456', password: '' })
).toThrowErrorMatchingInlineSnapshot(
`"[password]: value is [] but it must have a minimum length of [1]."`
`"[password]: value has length [0] but it must have a minimum length of [1]."`
);
});

View file

@ -93,7 +93,7 @@ describe('#disabledFeatures', () => {
disabledFeatures: 'foo',
})
).toThrowErrorMatchingInlineSnapshot(
`"[disabledFeatures]: could not parse array value from [foo]"`
`"[disabledFeatures]: could not parse array value from json input"`
);
});

View file

@ -111,7 +111,7 @@ export default function indexTest({ getService }: FtrProviderContext) {
statusCode: 400,
error: 'Bad Request',
message:
'error validating action type config: [index]: types that failed validation:\n- [index.0]: expected value of type [string] but got [number]\n- [index.1]: expected value to equal [null] but got [666]',
'error validating action type config: [index]: types that failed validation:\n- [index.0]: expected value of type [string] but got [number]\n- [index.1]: expected value to equal [null]',
});
});
});

View file

@ -70,7 +70,7 @@ export default function({ getService }: FtrProviderContext) {
.get('/api/endpoint/alerts?page_size=0')
.set('kbn-xsrf', 'xxx')
.expect(400);
expect(body.message).to.contain('Value is [0] but it must be equal to or greater than [1]');
expect(body.message).to.contain('Value must be equal to or greater than [1]');
});
it('alerts api should return links to the next and previous pages using cursor-based pagination', async () => {

View file

@ -100,7 +100,7 @@ export default function({ getService }: FtrProviderContext) {
],
})
.expect(400);
expect(body.message).to.contain('Value is [0] but it must be equal to or greater than [1]');
expect(body.message).to.contain('Value must be equal to or greater than [1]');
});
it('metadata api should return page based on filters passed.', async () => {

View file

@ -78,7 +78,7 @@ export default function({ getService }) {
uri = `${BASE_URI}?${stringify(params, { sort: false })}`;
({ body } = await supertest.get(uri).expect(400));
expect(body.message).to.contain(
'[request query.meta_fields]: could not parse array value from [stringValue]'
'[request query.meta_fields]: could not parse array value from json input'
);
});