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

Show all possible errors exhaustively (option?) #237

Open
karlhorky opened this issue May 24, 2022 · 0 comments
Open

Show all possible errors exhaustively (option?) #237

karlhorky opened this issue May 24, 2022 · 0 comments

Comments

@karlhorky
Copy link

karlhorky commented May 24, 2022

Hi there, thanks for ow, looks like a super simple, minimal validator!

In searching the repo for a way to get all of the errors out of ow, I found #5 (implemented in #192), which talks about "multiple errors". However, this doesn't implement multiple errors for all cases when ow would have multiple problems with a validation.

It would be great to have a way to get all errors in all possible cases.

One of those cases off the top of my head is (although there are surely more):

  1. If multiple keys in the object don't match some validation

If this shouldn't run all the time, maybe this should be some option such as exhaustive: boolean?

Current behavior:

> ow({a: 2, b: 'a'}, ow.object.exactShape({a: ow.string, b: ow.number}))
Uncaught:
ArgumentError: Expected property `a` to be of type `string` but received type `number` in object
    at ow (/Users/k/p/courses/node_modules/ow/dist/index.js:29:28) {
  validationErrors: Map(1) {
    'object' => Set(1) {
      'Expected property `a` to be of type `string` but received type `number` in object'
    }
  }
}

> ow({a: '2', b: 'a'}, ow.object.exactShape({a: ow.string, b: ow.number}))
Uncaught:
ArgumentError: Expected property `b` to be of type `number` but received type `string` in object
    at ow (/Users/k/p/courses/node_modules/ow/dist/index.js:29:28) {
  validationErrors: Map(1) {
    'object' => Set(1) {
      'Expected property `b` to be of type `number` but received type `string` in object'
    }
  }
}

Suggested behavior:

> ow({a: 2, b: 'a'}, ow.object.exactShape({a: ow.string, b: ow.number}))
Uncaught:
ArgumentError: Expected property `a` to be of type `string` but received type `number` in object
Expected property `b` to be of type `number` but received type `string` in object
    at ow (/Users/k/p/courses/node_modules/ow/dist/index.js:29:28) {
  validationErrors: Map(1) {
    'object' => Set(2) {
      'Expected property `a` to be of type `string` but received type `number` in object',
      'Expected property `b` to be of type `number` but received type `string` in object'
    }
  }
}

Even nicer would be an option to get the errors back in the shape of the original object:

> ow({a: 2, b: 'a'}, ow.object.exactShape({a: ow.string, b: ow.number}))
Uncaught:
ArgumentError: Expected property `a` to be of type `string` but received type `number` in object
Expected property `b` to be of type `number` but received type `string` in object
    at ow (/Users/k/p/courses/node_modules/ow/dist/index.js:29:28) {
  validationErrors: Object {
      a: 'Expected property `a` to be of type `string` but received type `number` in object',
      b: 'Expected property `b` to be of type `number` but received type `string` in object'
  }
}
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

No branches or pull requests

1 participant