-
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.
* support null for literal type * support schemas withing right operand of conditional type * add never type to ban usage * remove never options. add when needed * add tests for Reference.isReference
- Loading branch information
Showing
12 changed files
with
281 additions
and
6 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
61 changes: 61 additions & 0 deletions
61
packages/kbn-config-schema/src/references/reference.test.ts
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,61 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { Reference } from './reference'; | ||
import { schema } from '../'; | ||
|
||
describe('Reference.isReference', () => { | ||
it('handles primitives', () => { | ||
expect(Reference.isReference(undefined)).toBe(false); | ||
expect(Reference.isReference(null)).toBe(false); | ||
expect(Reference.isReference(true)).toBe(false); | ||
expect(Reference.isReference(1)).toBe(false); | ||
expect(Reference.isReference('a')).toBe(false); | ||
expect(Reference.isReference({})).toBe(false); | ||
}); | ||
|
||
it('handles schemas', () => { | ||
expect( | ||
Reference.isReference( | ||
schema.string({ | ||
defaultValue: 'value', | ||
}) | ||
) | ||
).toBe(false); | ||
|
||
expect( | ||
Reference.isReference( | ||
schema.conditional( | ||
schema.contextRef('context_value_1'), | ||
schema.contextRef('context_value_2'), | ||
schema.string(), | ||
schema.string() | ||
) | ||
) | ||
).toBe(false); | ||
}); | ||
|
||
it('handles context references', () => { | ||
expect(Reference.isReference(schema.contextRef('ref_1'))).toBe(true); | ||
}); | ||
|
||
it('handles sibling references', () => { | ||
expect(Reference.isReference(schema.siblingRef('ref_1'))).toBe(true); | ||
}); | ||
}); |
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
8 changes: 8 additions & 0 deletions
8
packages/kbn-config-schema/src/types/__snapshots__/conditional_type.test.ts.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
2 changes: 2 additions & 0 deletions
2
packages/kbn-config-schema/src/types/__snapshots__/literal_type.test.ts.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
packages/kbn-config-schema/src/types/__snapshots__/never_type.test.ts.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,75 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
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(undefined)).not.toThrow(); | ||
}); | ||
|
||
test('throws on value set as object property', () => { | ||
const type = schema.object({ | ||
name: schema.never(), | ||
status: schema.string(), | ||
}); | ||
|
||
expect(() => | ||
type.validate({ name: 'name', status: 'in progress' }) | ||
).toThrowErrorMatchingSnapshot(); | ||
|
||
expect(() => type.validate({ status: 'in progress' })).not.toThrow(); | ||
expect(() => type.validate({ name: undefined, status: 'in progress' })).not.toThrow(); | ||
}); | ||
|
||
test('works for conditional types', () => { | ||
const type = schema.object({ | ||
name: schema.conditional( | ||
schema.contextRef('context_value_1'), | ||
schema.contextRef('context_value_2'), | ||
schema.string(), | ||
schema.never() | ||
), | ||
}); | ||
|
||
expect( | ||
type.validate( | ||
{ name: 'a' }, | ||
{ | ||
context_value_1: 0, | ||
context_value_2: 0, | ||
} | ||
) | ||
).toEqual({ name: 'a' }); | ||
|
||
expect(() => | ||
type.validate( | ||
{ name: 'a' }, | ||
{ | ||
context_value_1: 0, | ||
context_value_2: 1, | ||
} | ||
) | ||
).toThrowErrorMatchingSnapshot(); | ||
}); |
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,34 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { internals } from '../internals'; | ||
import { Type } from './type'; | ||
|
||
export class NeverType extends Type<never> { | ||
constructor() { | ||
super(internals.any().forbidden()); | ||
} | ||
|
||
protected handleError(type: string) { | ||
switch (type) { | ||
case 'any.unknown': | ||
return "a value wasn't expected to be present"; | ||
} | ||
} | ||
} |