Skip to content

Ability to perform custom calculations on referenced fields  #1807

@mgildea

Description

@mgildea

Describe the problem you are trying to fix (provide as much context as possible)

I am trying to validate the min and max number of elements based on the value of a referenced field, where the min is greater than or equal the value of the referenced field, and the max is less than or equal to TWICE the value of the referenced field.

It's possible I'm missing something in the APIs which would achieve this functionality; I have not figured out how to to do this.

Which API (or modification of the current API) do you suggest to solve that problem ?

for this specific problem, one might modify or overload the min and max functions in the array api, but I believe a feature like this should be applied to all/most "comparing" functions

It would be nice if these methods also accepted a function to perform a custom calculation that resolved to the originally expected input type.

Example might be:

schema : {
        daysPerWeek: Joi.number()
          .integer()
          .min(3)
          .max(7),
        movements: Joi.array()
          .min(Joi.ref("daysPerWeek"))
          .max(Joi.ref("daysPerWeek"), value => {
            return value * 2;
          })
          .items(Joi.string())
}

I believe this enable functionality to validate against some external resource as well.

Are you ready to work on a pull request if your suggestion is accepted ?

Yes

Metadata

Metadata

Assignees

Labels

featureNew functionality or improvement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions