-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
89c4159
commit 843e989
Showing
5 changed files
with
95 additions
and
1 deletion.
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 } } | ||
); | ||
|
||
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