Skip to content

Conversation

@pmuellr
Copy link
Member

@pmuellr pmuellr commented Aug 7, 2019

The current (new!) implementation of schema.nullable() ends up having
a problem - it only works right for schema.nullable(schema.string()).
For other types, like schema.number() and schema.boolean(), a null
value passed in does not validate.

I poked around a bit, but "it's complicated". So, went with the
suggested approach of using a schema.maybe([V, null]) validation,
which works for number and boolean.

Added some additional tests on the nullable type, could probably
add more, if we think we need some.

The current (new!) implementation of `schema.nullable()` ends up having
a problem - it only works right for `schema.nullable(schema.string())`.
For other types, like `schema.number()` and `schema.boolean()`, a `null`
value passed in does not validate.

I poked around a bit, but "it's complicated".  So, went with the
suggested approach of using a `schema.maybe([V, null])` validation,
which works for number and boolean.

Added some additional tests on the `nullable` type, could probably
add more, if we think we need some.
@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@pmuellr pmuellr added the Team:Core Platform Core services: plugins, logging, config, saved objects, http, ES client, i18n, etc t// label Aug 7, 2019
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-platform

@pmuellr pmuellr added release_note:skip Skip the PR/issue when compiling release notes v7.4.0 v8.0.0 labels Aug 7, 2019
@pmuellr pmuellr requested review from azasypkin and mshustov August 7, 2019 23:44
@pmuellr pmuellr marked this pull request as ready for review August 7, 2019 23:44
expect(type.validate(null)).toEqual(null);
});

test('returns null if null for boolean', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add a couple of tests validating it returns value for other types different from null?

const type = schema.nullable(schema.boolean());
expect(type.validate(true)).toBe(true);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added some additional tests - validating positively for more basic types, and negatively for a property in an object type

@elasticmachine
Copy link
Contributor

💔 Build Failed

@pmuellr
Copy link
Member Author

pmuellr commented Aug 9, 2019

retest

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@pmuellr
Copy link
Member Author

pmuellr commented Aug 9, 2019

Thanks for the comments @restrry ; @azasypkin, any other thoughts? I'd like to get this merged by end of the week, next week, no big hurry ...

Copy link
Member

@azasypkin azasypkin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just one minor nit.


function nullable<V>(type: Type<V>): Type<V | null> {
return new NullableType(type);
return schema.oneOf([type, schema.literal(null)], { defaultValue: () => null });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can it just be defaultValue: null?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I tried that first, it didn't work, and you suggested the function invocation version, which did work. Let me try again ...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, only reference I can find is you suggesting this change a long time ago to me. Weird thing is, no idea where the function version came from, I don't think I would have figured that one out myself; I have a feeling maybe it was needed in a previous version of the combinator.

So, commit incoming with that change ...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh, yeah, I believe that was me who initially suggested that function syntax 🙈 Just didn't think long enough to suggest the simplest solution, sorry for the confusion.

@pmuellr
Copy link
Member Author

pmuellr commented Aug 13, 2019

Double-checked that the most recent version of schema.nullable() works for latest version of actions, and it does :-) \o/

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@pmuellr pmuellr merged commit 19fa802 into elastic:master Aug 14, 2019
pmuellr added a commit to pmuellr/kibana that referenced this pull request Aug 14, 2019
The previous implementation of `schema.nullable()` ends up having
a problem - it only works right for `schema.nullable(schema.string())`.
For other types, like `schema.number()` and `schema.boolean()`, a `null`
value passed in does not validate.

I poked around a bit, but "it's complicated".  So, went with the
suggested approach of using a `schema.maybe([V, null])` validation,
which works for number and boolean.

Added some additional tests on the `nullable` type.
pmuellr added a commit that referenced this pull request Aug 14, 2019
The previous implementation of `schema.nullable()` ends up having
a problem - it only works right for `schema.nullable(schema.string())`.
For other types, like `schema.number()` and `schema.boolean()`, a `null`
value passed in does not validate.

I poked around a bit, but "it's complicated".  So, went with the
suggested approach of using a `schema.maybe([V, null])` validation,
which works for number and boolean.

Added some additional tests on the `nullable` type.
@pmuellr pmuellr deleted the config-schema/fix-nullable branch August 19, 2019 14:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backported release_note:skip Skip the PR/issue when compiling release notes Team:Core Platform Core services: plugins, logging, config, saved objects, http, ES client, i18n, etc t// v7.4.0 v8.0.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants