-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Introduce recordOf schema. Remove redundant declarations.
#26952
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
Changes from 1 commit
d0ab2dc
87c712d
5860636
6668201
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -274,6 +274,54 @@ export const internals = Joi.extend([ | |
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'record', | ||
| pre(value: any, state: State, options: ValidationOptions) { | ||
| if (!isPlainObject(value)) { | ||
| return this.createError('record.base', { value }, state, options); | ||
| } | ||
|
|
||
| return value as any; | ||
| }, | ||
| rules: [ | ||
| anyCustomRule, | ||
| { | ||
| name: 'entries', | ||
| params: { key: Joi.object().schema(), value: Joi.object().schema() }, | ||
|
Contributor
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. I'm confused, here we define params as an object with key and value properties, but when people call
Contributor
Author
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. I know it's super weird, but their alternative with
Contributor
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. Alright, that's fine since it's the API setup by Joi, but yeah, not a fan of that design. |
||
| validate(params, value, state, options) { | ||
| const result = {} as Record<string, any>; | ||
| for (const [entryKey, entryValue] of Object.entries(value)) { | ||
| const { value: validatedEntryKey, error: keyError } = Joi.validate( | ||
| entryKey, | ||
| params.key | ||
| ); | ||
|
|
||
| if (keyError) { | ||
| return this.createError('record.key', { entryKey, reason: keyError }, state, options); | ||
| } | ||
|
|
||
| const { value: validatedEntryValue, error: valueError } = Joi.validate( | ||
| entryValue, | ||
| params.value | ||
| ); | ||
|
|
||
| if (valueError) { | ||
| return this.createError( | ||
| 'record.value', | ||
| { entryKey, reason: valueError }, | ||
| state, | ||
| options | ||
| ); | ||
| } | ||
|
|
||
| result[validatedEntryKey] = validatedEntryValue; | ||
| } | ||
|
|
||
| return result as any; | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'array', | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,7 +18,7 @@ | |
| */ | ||
|
|
||
| import typeDetect from 'type-detect'; | ||
| import { SchemaTypeError } from '../errors'; | ||
| import { SchemaTypeError, SchemaTypesError } from '../errors'; | ||
| import { internals } from '../internals'; | ||
| import { Type, TypeOptions } from './type'; | ||
|
|
||
|
|
@@ -53,7 +53,9 @@ export class MapOfType<K, V> extends Type<Map<K, V>> { | |
| const childPathWithIndex = reason.path.slice(); | ||
| childPathWithIndex.splice(path.length, 0, entryKey.toString()); | ||
|
|
||
| return new SchemaTypeError(reason.message, childPathWithIndex); | ||
| return reason instanceof SchemaTypesError | ||
|
Contributor
Author
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. note: while working on this PR I've noticed that we miss "composite" errors in |
||
| ? new SchemaTypesError(reason, childPathWithIndex, reason.errors) | ||
| : new SchemaTypeError(reason, childPathWithIndex); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| /* | ||
| * 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('handles object as input', () => { | ||
| const type = schema.recordOf(schema.string(), schema.string()); | ||
| const value = { | ||
| name: 'foo', | ||
| }; | ||
| expect(type.validate(value)).toEqual({ name: 'foo' }); | ||
| }); | ||
|
|
||
| test('fails when not receiving expected value type', () => { | ||
| const type = schema.recordOf(schema.string(), schema.string()); | ||
| const value = { | ||
| name: 123, | ||
| }; | ||
|
|
||
| expect(() => type.validate(value)).toThrowErrorMatchingInlineSnapshot( | ||
| `"[name]: expected value of type [string] but got [number]"` | ||
| ); | ||
| }); | ||
|
|
||
| test('fails when not receiving expected key type', () => { | ||
| const type = schema.recordOf( | ||
| schema.oneOf([schema.literal('nickName'), schema.literal('lastName')]), | ||
| schema.string() | ||
| ); | ||
|
|
||
| const value = { | ||
| name: 'foo', | ||
| }; | ||
|
|
||
| expect(() => type.validate(value)).toThrowErrorMatchingInlineSnapshot(` | ||
| "[name]: types that failed validation: | ||
| - [0]: expected value to equal [nickName] but got [name] | ||
| - [1]: expected value to equal [lastName] but got [name]" | ||
| `); | ||
| }); | ||
|
|
||
| test('includes namespace in failure when wrong top-level type', () => { | ||
| const type = schema.recordOf(schema.string(), schema.string()); | ||
| expect(() => type.validate([], {}, 'foo-namespace')).toThrowErrorMatchingInlineSnapshot( | ||
| `"[foo-namespace]: expected value of type [object] but got [Array]"` | ||
| ); | ||
| }); | ||
|
|
||
| test('includes namespace in failure when wrong value type', () => { | ||
| const type = schema.recordOf(schema.string(), schema.string()); | ||
| const value = { | ||
| name: 123, | ||
| }; | ||
|
|
||
| expect(() => type.validate(value, {}, 'foo-namespace')).toThrowErrorMatchingInlineSnapshot( | ||
| `"[foo-namespace.name]: expected value of type [string] but got [number]"` | ||
| ); | ||
| }); | ||
|
|
||
| test('includes namespace in failure when wrong key type', () => { | ||
| const type = schema.recordOf(schema.string({ minLength: 10 }), schema.string()); | ||
| const value = { | ||
| name: 'foo', | ||
| }; | ||
|
|
||
| expect(() => type.validate(value, {}, 'foo-namespace')).toThrowErrorMatchingInlineSnapshot( | ||
| `"[foo-namespace.name]: value is [name] but it must have a minimum length of [10]."` | ||
|
Contributor
Author
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. note: the generic
Contributor
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 looks like we can modify the value of
Contributor
Author
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. Yeah, let's try this format 👍 |
||
| ); | ||
| }); | ||
|
|
||
| test('returns default value if undefined', () => { | ||
| const obj = { foo: 'bar' }; | ||
|
|
||
| const type = schema.recordOf(schema.string(), schema.string(), { | ||
| defaultValue: obj, | ||
| }); | ||
|
|
||
| expect(type.validate(undefined)).toEqual(obj); | ||
| }); | ||
|
|
||
| test('recordOf within recordOf', () => { | ||
| const type = schema.recordOf(schema.string(), schema.recordOf(schema.string(), schema.number())); | ||
| const value = { | ||
| foo: { | ||
| bar: 123, | ||
| }, | ||
| }; | ||
|
|
||
| expect(type.validate(value)).toEqual({ foo: { bar: 123 } }); | ||
| }); | ||
|
|
||
| test('object within recordOf', () => { | ||
| const type = schema.recordOf( | ||
| schema.string(), | ||
| schema.object({ | ||
| bar: schema.number(), | ||
| }) | ||
| ); | ||
| const value = { | ||
| foo: { | ||
| bar: 123, | ||
| }, | ||
| }; | ||
|
|
||
| expect(type.validate(value)).toEqual({ foo: { bar: 123 } }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /* | ||
| * 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 typeDetect from 'type-detect'; | ||
| import { SchemaTypeError, SchemaTypesError } from '../errors'; | ||
| import { internals } from '../internals'; | ||
| import { Type, TypeOptions } from './type'; | ||
|
|
||
| export type RecordOfOptions<K extends string, V> = TypeOptions<Record<K, V>>; | ||
|
|
||
| export class RecordOfType<K extends string, V> extends Type<Record<K, V>> { | ||
| constructor(keyType: Type<K>, valueType: Type<V>, options: RecordOfOptions<K, V> = {}) { | ||
| const schema = internals.record().entries(keyType.getSchema(), valueType.getSchema()); | ||
|
|
||
| super(schema, options); | ||
| } | ||
|
|
||
| protected handleError( | ||
| type: string, | ||
| { entryKey, reason, value }: Record<string, any>, | ||
| path: string[] | ||
| ) { | ||
| switch (type) { | ||
| case 'any.required': | ||
| case 'record.base': | ||
| return `expected value of type [object] but got [${typeDetect(value)}]`; | ||
| case 'record.key': | ||
| case 'record.value': | ||
| const childPathWithIndex = reason.path.slice(); | ||
| childPathWithIndex.splice(path.length, 0, entryKey.toString()); | ||
|
|
||
| return reason instanceof SchemaTypesError | ||
| ? new SchemaTypesError(reason, childPathWithIndex, reason.errors) | ||
| : new SchemaTypeError(reason, childPathWithIndex); | ||
| } | ||
| } | ||
| } |
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.
note: it's quite similar to what we do for
map, but I decided to not generalize here and let it live separately for a while.