ow icon indicating copy to clipboard operation
ow copied to clipboard

Show all possible errors exhaustively (option?)

Open karlhorky opened this issue 2 years ago • 0 comments

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'
  }
}

karlhorky avatar May 24 '22 12:05 karlhorky