joiful-ts / joiful

TypeScript Declarative Validation for Joi
239 stars 18 forks source link

unique() does not allow nested duplication check of array of objects #208

Open ujwal-setlur opened 3 years ago

ujwal-setlur commented 3 years ago

I have the same problem as in https://stackoverflow.com/questions/53243306/validating-unique-key-pairs-in-a-nested-object-with-joi-and-nodejs

I essentially have an array of objects, and I want to validate that a certain key/value pair is not duplicated amongst the array elements of objects.

This can be solved in joi with providing a comparator to the unique() function.

However, in joiful, I cannot pass a comparator to unique().

Is there a different way to do this?

ujwal-setlur commented 3 years ago

Will try with custom()

laurence-myers commented 3 years ago

Looks like joiful is accidentally missing this functionality. Should be an easy fix.

https://github.com/joiful-ts/joiful/blob/master/src/decorators/array.ts#L109

Change unique to accept the same args: either a comparator function; or a string and an optional "options" object. @types/joi can help us work out what the arg types should be. Then update the interface ArraySchemaModifiers, and add tests.

ujwal-setlur commented 1 year ago

Any update?

laurence-myers commented 1 year ago

No update from me, sorry. I'd welcome a Pull Request. 😁