-
Notifications
You must be signed in to change notification settings - Fork 8.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[OAS] Support lazy runtime types #184000
Merged
jloleysens
merged 14 commits into
elastic:main
from
jloleysens:kbn-config/support-recursive-types
May 23, 2024
Merged
[OAS] Support lazy runtime types #184000
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
843e989
support lazy runtime types
jloleysens 1f2c7c3
refactor to use built in Joi ID
jloleysens 3998f93
fix use of IDs
jloleysens f09817b
large ish refactor to rather use the built in joi-to-json reference h…
jloleysens 5e6cc11
refactored tests after changes
jloleysens 7b3abff
added parser test
jloleysens d7d8a80
updated unit test after removing ref ID from meta
jloleysens 41fe990
comments
jloleysens 6510265
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine e681cd0
added OAS test
jloleysens beea249
do not export lazy function directly
jloleysens a0a3885
fix other use of "lazy"
jloleysens b42e0e7
remove explicit typing
jloleysens acb66f1
address nit
jloleysens File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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, lazy } 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: lazy<RecursiveType>(id), // this declaration should fail | ||
}, | ||
{ meta: { id } } | ||
); | ||
} | ||
|
||
describe('lazy', () => { | ||
const id = 'recursive'; | ||
const object: Type<RecursiveType> = schema.object( | ||
{ | ||
name: schema.string(), | ||
self: lazy<RecursiveType>(id), | ||
}, | ||
{ meta: { id } } | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a bit verbose, I would have liked to do something like Zod but with the way |
||
|
||
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,16 @@ | ||
/* | ||
* 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'; | ||
|
||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed: the explicit
Type<RecursiveType>
typing can be removed 🚀