Skip to content
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

feat: respect nullable() with oneOf #1757

Merged
merged 1 commit into from
Aug 22, 2022
Merged

feat: respect nullable() with oneOf #1757

merged 1 commit into from
Aug 22, 2022

Conversation

jquense
Copy link
Owner

@jquense jquense commented Aug 22, 2022

closes: #768 #104

BREAKING CHANGE: previously oneOf required adding null explicitly to allowed values when using oneOf. Folks have found this confusing and unintuitive so I am deferring and adjusting the behavior

closes: #768 #104

BREAKING CHANGE: previously `oneOf` required adding `null` explicitly to allowed values when using oneOf. Folks have found this confusing and unintuitive so I am deferring and adjusting the behavior
@jquense jquense merged commit 61ec302 into master Aug 22, 2022
@jquense jquense deleted the oneof branch August 22, 2022 18:56
@karlhorky
Copy link
Contributor

Thanks for this @jquense !

How do you expect users to represent "only null" using mixed() now? My previous solution here does not seem to work anymore:

Screenshot 2022-11-12 at 10 46 35

  }).shape({
    files: mixed()
      .label('Files')
      .oneOf([null], ({ label }) => `${label} must be null`)
      .nullable()
      .defined(),
  });

Error message:

Type 'null' is not assignable to type 'Reference<unknown> | AnyPresentValue | undefined'.

@karlhorky
Copy link
Contributor

karlhorky commented Nov 12, 2022

Oh, maybe just moving .nullable() up one line? This seems to not fail:

  }).shape({
    files: mixed()
      .label('Files')
+     .nullable()
      .oneOf([null], ({ label }) => `${label} must be null`)
-     .nullable()
      .defined(),
  });

@jquense
Copy link
Owner Author

jquense commented Nov 13, 2022

simplest way to do this is const nullSchema = mixed((value) === null)

@karlhorky
Copy link
Contributor

karlhorky commented Nov 15, 2022

Hm, do you mean the following with a type predicate? This seems to throw a type error.

mixed((value): value is null => value === null) // 💥 Type 'null' is not assignable to type 'AnyPresentValue'.
  .nullable();

Full TypeScript error:

Argument of type '(value: any) => value is null' is not assignable to parameter of type 'MixedOptions<AnyPresentValue> | TypeGuard<AnyPresentValue> | undefined'.
  Type '(value: any) => value is null' is not assignable to type 'TypeGuard<AnyPresentValue>'.
    Type predicate 'value is null' is not assignable to 'value is AnyPresentValue'.
      Type 'null' is not assignable to type 'AnyPresentValue'.

Also does not work if passing in the generic parameter:

mixed<null>((value): value is null => value === null) // 💥 Type 'null' does not satisfy the constraint 'AnyPresentValue'.
  .nullable();

Once we have a recommended version, I'll add this to the readme as an example for mixed()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

OneOf and notOneOf do not handle null and undefined in the error message.
2 participants