-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[OAS] Support lazy runtime types (#184000)
## Summary Close #182910 Add the ability to declare recursive schemas. Updates `@kbn/config-schema` to support recursive types. This design follows the underlying pattern provided by Joi: https://joi.dev/api/?v=17.13.0#linkref: ```ts const id = 'recursive'; const recursiveSchema: Type<RecursiveType> = schema.object( { name: schema.string(), self: schema.lazy<RecursiveType>(id), }, { meta: { id } } ); ``` Since using the `.link` API we are also using `.id` which enables us to leverage this mechanism OOTB with `joi-to-json` for OAS generation (thus we could delete a lot of code there). I chose to avoid using `id` originally because I thought it would be simpler if we control more of the conversion in config-schema's case but for recursive schemas and references I think this is a favourable trade off. Open to other ideas! --------- Co-authored-by: kibanamachine <[email protected]>
- Loading branch information
1 parent
8550c2c
commit 1cc878f
Showing
26 changed files
with
397 additions
and
274 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { Type, schema } from '../..'; | ||
|
||
interface RecursiveType { | ||
name: string; | ||
self: undefined | RecursiveType; | ||
} | ||
|
||
// Test our recursive type inference | ||
{ | ||
const id = 'recursive'; | ||
// @ts-expect-error | ||
const testObject: Type<RecursiveType> = schema.object( | ||
{ | ||
name: schema.string(), | ||
notSelf: schema.lazy<RecursiveType>(id), // this declaration should fail | ||
}, | ||
{ meta: { id } } | ||
); | ||
} | ||
|
||
describe('lazy', () => { | ||
const id = 'recursive'; | ||
const object = schema.object( | ||
{ | ||
name: schema.string(), | ||
self: schema.lazy<RecursiveType>(id), | ||
}, | ||
{ meta: { id } } | ||
); | ||
|
||
it('allows recursive runtime types to be defined', () => { | ||
const self: RecursiveType = { | ||
name: 'self1', | ||
self: { | ||
name: 'self2', | ||
self: { | ||
name: 'self3', | ||
self: { | ||
name: 'self4', | ||
self: undefined, | ||
}, | ||
}, | ||
}, | ||
}; | ||
const { value, error } = object.getSchema().validate(self); | ||
expect(error).toBeUndefined(); | ||
expect(value).toEqual(self); | ||
}); | ||
|
||
it('detects invalid recursive types as expected', () => { | ||
const invalidSelf = { | ||
name: 'self1', | ||
self: { | ||
name: 123, | ||
self: undefined, | ||
}, | ||
}; | ||
|
||
const { error, value } = object.getSchema().validate(invalidSelf); | ||
expect(value).toEqual(invalidSelf); | ||
expect(error?.message).toBe('expected value of type [string] but got [number]'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { Type } from './type'; | ||
import { internals } from '../internals'; | ||
|
||
/** | ||
* Use this type to construct recursive runtime schemas. | ||
*/ | ||
export class Lazy<T> extends Type<T> { | ||
constructor(id: string) { | ||
super(internals.link(`#${id}`)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.