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

More user friendly ErrorObject TypeScript type #1090

Closed
torifat opened this issue Sep 21, 2019 · 4 comments
Closed

More user friendly ErrorObject TypeScript type #1090

torifat opened this issue Sep 21, 2019 · 4 comments

Comments

@torifat
Copy link

torifat commented Sep 21, 2019

What problem do you want to solve?
In its current state ErrorObject is not very user friendly in certain scenarios. For example:

function useError(ajvError: ErrorObject) {
  switch(ajvError.keyword) {
    case 'enum': {
      // ErrorParameters
      ajvError.params
    }
  }
}

It would be really useful if we could get EnumParams here, instead of ErrorParameters. It's possible to use type-guards to achieve something similar but the code gets a bit messier.

What do you think is the correct solution to problem?
IMO it can be improved by using discriminated unions. If we do something like:

interface IErrorObject<T, U> {
  keyword: T,
  params: U,
  ...
}

type EnumErrorObject = IErrorObject<'enum', EnumParams>;
type RequiredErrorObject = IErrorObject<'required', RequiredParams>;
...

export type ErrorObject = EnumErrorObject | RequiredErrorObject | ...;

Then we will get EnumParams inside the enum case.

function useError(ajvError: ErrorObject) {
  switch(ajvError.keyword) {
    case 'enum': {
      // EnumParams
      ajvError.params
    }
  }
}

Will you be able to implement it?
Yes

@torifat
Copy link
Author

torifat commented Oct 15, 2019

@epoberezkin any thoughts?

@epoberezkin epoberezkin added this to the 7.0.0-beta milestone Sep 15, 2020
@epoberezkin
Copy link
Member

@torifat Sorry for ignoring it... Looking at it now - it is probably the right approach.

Now that it is re-written in typescript - probably error type should be defined by each keyword locally (although given that it is returned via generated code it won't be connected to the actual code, just visually)

@epoberezkin
Copy link
Member

The only problem I see with this approach is that ErrorObject should be an open union, to allow users extending it when they define custom keywords without modifying Ajv code base - trying to figure out how to do it in typescript... @torifat any suggestion would be fantastic.

@epoberezkin
Copy link
Member

in v7-alpha

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

No branches or pull requests

2 participants