emsifa / validasaur

Deno validation library
MIT License
45 stars 9 forks source link

Proposal: Be able to validate top-level arrays as well #38

Open Tandashi opened 2 years ago

Tandashi commented 2 years ago

Currently, it is only possible to validate top-level objects using validate(). However, there are some use-cases where validating a single value like an array would be needed handy.

Example: Imagine you have a RESTful API where you have an Endpoint that updates a Resource. The body for this request looks as follows:

[
  "value 1",
  "value 2",
  // ...
]

What's currently 'possible' Currently, there is no easy way to validate the body of the request. A workaround would be to use validateValue but then you would need to implement a lot around that yourselves since it returns a InvalidPayload[] rather than a ValidationResult. validateData is also not useable since it requires the input to be of type InputData which is not compatible with array.

What would be nice to have To have something like this:

validateTopLevelArray(input: any[], rules: Rule[]): Promise<ValidationResult>

validateTopLevelArray(['value 1', 'value 2'], validateArray([isString]))