Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ describe('Saved Objects type validation helper', () => {
kibanaVersion: defaultVersion,
});
const validationError = new Error(
'[attributes.count]: definition for this key is missing: Bad Request'
"[attributes.count]: Additional properties are not allowed ('count' was unexpected): Bad Request"
);
const data = createMockObject(typeC, { attributes: { foo: 'hi', count: 1 } });
expect(() => helper.validateObjectForCreate(typeC, data)).toThrowError(validationError);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ describe('validateAndConvertAggregations', () => {
expect(() => {
validateAndConvertAggregations(['foo'], aggregations, mockMappings);
}).toThrowErrorMatchingInlineSnapshot(
`"[someAgg.max.script]: definition for this key is missing"`
`"[someAgg.max.script]: Additional properties are not allowed ('script' was unexpected)"`
);
});

Expand All @@ -474,7 +474,7 @@ describe('validateAndConvertAggregations', () => {
expect(() => {
validateAndConvertAggregations(['foo'], aggregations, mockMappings);
}).toThrowErrorMatchingInlineSnapshot(
`"[someAgg.aggs.nested.max.script]: definition for this key is missing"`
`"[someAgg.aggs.nested.max.script]: Additional properties are not allowed ('script' was unexpected)"`
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ describe('convertModelVersionBackwardConversionSchema', () => {
const converted = convertModelVersionBackwardConversionSchema(conversionSchema);

expect(() => converted(doc)).toThrowErrorMatchingInlineSnapshot(
`"[hello]: definition for this key is missing"`
`"[hello]: Additional properties are not allowed ('hello' was unexpected)"`
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ describe(`assertTelemetryPayload`, () => {
expect(() =>
// @ts-expect-error: root doesn't match expected types
assertTelemetryPayload({ root: {}, plugins: { properties: {} } }, { version: 'some-version' })
).toThrow('[version]: definition for this key is missing. Received `"some-version"`');
).toThrow(
'[version]: Additional properties are not allowed (\'version\' was unexpected). Received `"some-version"`'
);
});
test('stats has nested-fields not defined in the schema => fail', () => {
expect(() =>
Expand All @@ -34,7 +36,7 @@ describe(`assertTelemetryPayload`, () => {
{ an_array: [{ docs: { missing: 1 } }] }
)
).toThrow(
'[an_array]: definition for this key is missing. Received `[{"docs":{"missing":1}}]`'
'[an_array]: Additional properties are not allowed (\'an_array\' was unexpected). Received `[{"docs":{"missing":1}}]`'
);
expect(() =>
assertTelemetryPayload(
Expand All @@ -53,7 +55,9 @@ describe(`assertTelemetryPayload`, () => {
},
{ an_array: [{ docs: { missing: 1 } }] }
)
).toThrow('[an_array.0.docs]: definition for this key is missing. Received `{"missing":1}`');
).toThrow(
'[an_array.0.docs]: Additional properties are not allowed (\'docs\' was unexpected). Received `{"missing":1}`'
);
expect(() =>
assertTelemetryPayload(
{
Expand All @@ -75,7 +79,9 @@ describe(`assertTelemetryPayload`, () => {
},
{ an_array: [{ docs: { missing: 1 } }] }
)
).toThrow('[an_array.0.docs.missing]: definition for this key is missing. Received `1`');
).toThrow(
"[an_array.0.docs.missing]: Additional properties are not allowed ('missing' was unexpected). Received `1`"
);
});
test('stats has nested-fields defined in the schema, but with wrong type => fail', () => {
expect(() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,11 @@ export function assertTelemetryPayload(
try {
ossTelemetryValidationSchema.validate(stats);
} catch (err) {
// "[path.to.key]: definition for this key is missing"
const [, pathToKey] = err.message.match(/^\[(.*)\]\: definition for this key is missing/) ?? [];
// "[path.to.key]: Additional properties are not allowed ('key' was unexpected)"
const [, pathToKey] =
err.message.match(
/^\[(.*)\]\: Additional properties are not allowed \('[^']+' was unexpected\)/
) ?? [];
if (pathToKey) {
err.message += `. Received \`${JSON.stringify(get(stats, pathToKey))}\``;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ describe('schema_runtime_fields', () => {

// When/Then
expect(() => runtimeFieldSchema.validate(runtimeField)).toThrow(
/definition for this key is missing/
/Additional properties are not allowed \('[^']+' was unexpected\)/
);
});
});
Expand All @@ -233,7 +233,7 @@ describe('schema_runtime_fields', () => {

// When/Then
expect(() => runtimeFieldSchema.validate(runtimeField)).toThrow(
/definition for this key is missing/
/Additional properties are not allowed \('[^']+' was unexpected\)/
);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,9 @@ describe('#extendsDeep', () => {
const forbidSchema = type.extendsDeep({ unknowns: 'forbid' });
expect(() =>
forbidSchema.validate([{ foo: 'test', bar: 'test' }])
).toThrowErrorMatchingInlineSnapshot(`"[0.bar]: definition for this key is missing"`);
).toThrowErrorMatchingInlineSnapshot(
`"[0.bar]: Additional properties are not allowed ('bar' was unexpected)"`
);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,9 @@ describe('#extendsDeep', () => {
type
.extendsDeep({ unknowns: 'forbid' })
.validate({ foo: 'test', test: { bar: 'test', baz: 'test' } })
).toThrowErrorMatchingInlineSnapshot(`"[test.baz]: definition for this key is missing"`);
).toThrowErrorMatchingInlineSnapshot(
`"[test.baz]: Additional properties are not allowed ('baz' was unexpected)"`
);
});
});

Expand Down Expand Up @@ -473,7 +475,9 @@ describe('#extendsDeep', () => {
const forbidSchema = type.extendsDeep({ unknowns: 'forbid' });
expect(() =>
forbidSchema.validate({ foo: 'not-test', test: { bar: 'test', baz: 'test' } })
).toThrowErrorMatchingInlineSnapshot(`"[test.baz]: definition for this key is missing"`);
).toThrowErrorMatchingInlineSnapshot(
`"[test.baz]: Additional properties are not allowed ('baz' was unexpected)"`
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ describe('DiscriminatedUnionType', () => {
const input = { type: 'str', string: 'test' };
expect(() =>
forbidSchema.validate({ ...input, unknown: 'thing' })
).toThrowErrorMatchingInlineSnapshot(`"[unknown]: definition for this key is missing"`);
).toThrowErrorMatchingInlineSnapshot(
`"[unknown]: Additional properties are not allowed ('unknown' was unexpected)"`
);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,9 @@ describe('#extendsDeep', () => {
const forbidSchema = type.extendsDeep({ unknowns: 'forbid' });
expect(() =>
forbidSchema.validate({ key: { foo: 'test', bar: 'test' } })
).toThrowErrorMatchingInlineSnapshot(`"[key.bar]: definition for this key is missing"`);
).toThrowErrorMatchingInlineSnapshot(
`"[key.bar]: Additional properties are not allowed ('bar' was unexpected)"`
);
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ describe('#extendsDeep', () => {
const forbidSchema = type.extendsDeep({ unknowns: 'forbid' });
expect(() =>
forbidSchema.validate({ foo: 'test', bar: 'test' })
).toThrowErrorMatchingInlineSnapshot(`"[bar]: definition for this key is missing"`);
).toThrowErrorMatchingInlineSnapshot(
`"[bar]: Additional properties are not allowed ('bar' was unexpected)"`
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ test('fails if key does not exist in schema', () => {
};

expect(() => type.validate(value)).toThrowErrorMatchingInlineSnapshot(
`"[bar]: definition for this key is missing"`
`"[bar]: Additional properties are not allowed ('bar' was unexpected)"`
);
});

Expand Down Expand Up @@ -358,7 +358,9 @@ test('unknowns = `allow` affects only own keys', () => {
baz: 'baz',
},
})
).toThrowErrorMatchingInlineSnapshot(`"[foo.baz]: definition for this key is missing"`);
).toThrowErrorMatchingInlineSnapshot(
`"[foo.baz]: Additional properties are not allowed ('baz' was unexpected)"`
);
});

test('does not allow unknown keys when unknowns = `forbid`', () => {
Expand All @@ -370,7 +372,9 @@ test('does not allow unknown keys when unknowns = `forbid`', () => {
type.validate({
bar: 'baz',
})
).toThrowErrorMatchingInlineSnapshot(`"[bar]: definition for this key is missing"`);
).toThrowErrorMatchingInlineSnapshot(
`"[bar]: Additional properties are not allowed ('bar' was unexpected)"`
);
});

test('allow and remove unknown keys when unknowns = `ignore`', () => {
Expand Down Expand Up @@ -421,7 +425,9 @@ test('unknowns = `ignore` respects local preferences in sub-keys', () => {
baz: 'baz',
},
})
).toThrowErrorMatchingInlineSnapshot(`"[foo.baz]: definition for this key is missing"`);
).toThrowErrorMatchingInlineSnapshot(
`"[foo.baz]: Additional properties are not allowed ('baz' was unexpected)"`
);
});

describe('nested unknowns', () => {
Expand Down Expand Up @@ -458,7 +464,9 @@ describe('nested unknowns', () => {
},
},
})
).toThrowErrorMatchingInlineSnapshot(`"[myObj.foo.baz]: definition for this key is missing"`);
).toThrowErrorMatchingInlineSnapshot(
`"[myObj.foo.baz]: Additional properties are not allowed ('baz' was unexpected)"`
);
});

test('does not allow unknown keys when unknowns = `forbid`', () => {
Expand All @@ -474,7 +482,9 @@ describe('nested unknowns', () => {
bar: 'baz',
},
})
).toThrowErrorMatchingInlineSnapshot(`"[myObj.bar]: definition for this key is missing"`);
).toThrowErrorMatchingInlineSnapshot(
`"[myObj.bar]: Additional properties are not allowed ('bar' was unexpected)"`
);
});

test('allow and remove unknown keys when unknowns = `ignore`', () => {
Expand Down Expand Up @@ -540,7 +550,9 @@ describe('nested unknowns', () => {
},
},
})
).toThrowErrorMatchingInlineSnapshot(`"[myObj.foo.baz]: definition for this key is missing"`);
).toThrowErrorMatchingInlineSnapshot(
`"[myObj.foo.baz]: Additional properties are not allowed ('baz' was unexpected)"`
);
});

test('parent `allow`, child `ignore` should be honored', () => {
Expand Down Expand Up @@ -789,7 +801,9 @@ describe('#extends', () => {

expect(() => {
extended.validate({ string: 'foo', number: 12 });
}).toThrowErrorMatchingInlineSnapshot(`"[number]: definition for this key is missing"`);
}).toThrowErrorMatchingInlineSnapshot(
`"[number]: Additional properties are not allowed ('number' was unexpected)"`
);

expect(() => {
extended.validate({ string: 'foo' });
Expand Down Expand Up @@ -981,6 +995,8 @@ describe('#extendsDeep', () => {
const forbidSchema = type.extendsDeep({ unknowns: 'forbid' });
expect(() =>
forbidSchema.validate({ test: { foo: 'test', bar: 'test' } })
).toThrowErrorMatchingInlineSnapshot(`"[test.bar]: definition for this key is missing"`);
).toThrowErrorMatchingInlineSnapshot(
`"[test.bar]: Additional properties are not allowed ('bar' was unexpected)"`
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -212,15 +212,17 @@ export class ObjectType<P extends Props = any> extends Type<ObjectResultType<P>>
return new ObjectType(extendedProps, extendedOptions);
}

protected handleError(type: string, { reason, value }: Record<string, any>) {
protected handleError(type: string, { reason, value, child }: Record<string, any>) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we please ensure the OAS generation isn't effected by this change.
I don't see how it could, but better to confirm than be surprised later.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looked into this, the OAS generation uses schema conversion (kbn-router-to-openapispec) and does not call handleError; this only affects runtime validation errors.

Additionally I had cursor run the OAS generation script locally for both serverless and traditional (via node scripts/capture_oas_snapshot ... with the same include path list CI uses) and it succeeded.

switch (type) {
case 'any.required':
case 'object.base':
return `expected a plain object value, but found [${typeDetect(value)}] instead.`;
case 'object.parse':
return `could not parse object value from json input`;
case 'object.unknown':
return `definition for this key is missing`;
return child
? `Additional properties are not allowed ('${child}' was unexpected)`
: `Additional properties are not allowed (an unexpected property was found)`;
case 'object.child':
return reason[0];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ describe('#extendsDeep', () => {
const forbidSchema = type.extendsDeep({ unknowns: 'forbid' });
expect(() =>
forbidSchema.validate({ key: { foo: 'test', bar: 'test' } })
).toThrowErrorMatchingInlineSnapshot(`"[key.bar]: definition for this key is missing"`);
).toThrowErrorMatchingInlineSnapshot(
`"[key.bar]: Additional properties are not allowed ('bar' was unexpected)"`
);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ describe('#extendsDeep', () => {
const forbidSchema = type.extendsDeep({ unknowns: 'forbid' });
expect(() =>
forbidSchema.validate({ foo: 'test', bar: 'test' })
).toThrowErrorMatchingInlineSnapshot(`"[bar]: definition for this key is missing"`);
).toThrowErrorMatchingInlineSnapshot(
`"[bar]: Additional properties are not allowed ('bar' was unexpected)"`
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ describe('RPC -> bulkGet()', () => {
},
{
input: { ...validInput, unknown: 'foo' },
expectedError: '[unknown]: definition for this key is missing',
expectedError:
"[unknown]: Additional properties are not allowed ('unknown' was unexpected)",
},
].forEach(({ input, expectedError }) => {
const error = validate(input, inputSchema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ describe('RPC -> create()', () => {
},
{
input: { ...validInput, unknown: 'foo' },
expectedError: '[unknown]: definition for this key is missing',
expectedError:
"[unknown]: Additional properties are not allowed ('unknown' was unexpected)",
},
].forEach(({ input, expectedError }) => {
const error = validate(input, inputSchema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ describe('RPC -> delete()', () => {
},
{
input: { ...validInput, unknown: 'foo' },
expectedError: '[unknown]: definition for this key is missing',
expectedError:
"[unknown]: Additional properties are not allowed ('unknown' was unexpected)",
},
{
input: { ...validInput, id: '' }, // id must have min 1 char
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ describe('RPC -> get()', () => {
},
{
input: { ...validInput, unknown: 'foo' },
expectedError: '[unknown]: definition for this key is missing',
expectedError:
"[unknown]: Additional properties are not allowed ('unknown' was unexpected)",
},
{
input: { ...validInput, id: '' }, // id must have min 1 char
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ describe('RPC -> mSearch()', () => {
},
{
input: { ...validInput, unknown: 'foo' },
expectedError: '[unknown]: definition for this key is missing',
expectedError:
"[unknown]: Additional properties are not allowed ('unknown' was unexpected)",
},
].forEach(({ input, expectedError }) => {
const error = validate(input, inputSchema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ describe('RPC -> search()', () => {
},
{
input: { ...validInput, unknown: 'foo' },
expectedError: '[unknown]: definition for this key is missing',
expectedError:
"[unknown]: Additional properties are not allowed ('unknown' was unexpected)",
},
].forEach(({ input, expectedError }) => {
const error = validate(input, inputSchema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ describe('RPC -> update()', () => {
},
{
input: { ...validInput, unknown: 'foo' },
expectedError: '[unknown]: definition for this key is missing',
expectedError:
"[unknown]: Additional properties are not allowed ('unknown' was unexpected)",
},
].forEach(({ input, expectedError }) => {
const error = validate(input, inputSchema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ describe('labelsSchema', () => {
test('does not allow unknowns', () => {
expect(() => {
labelsSchema.validate({ foo: 'bar' });
}).toThrowErrorMatchingInlineSnapshot(`"[foo]: definition for this key is missing"`);
}).toThrowErrorMatchingInlineSnapshot(
`"[foo]: Additional properties are not allowed ('foo' was unexpected)"`
);
});
});
Loading
Loading