1

I'm making an API and I'm having trouble finding the right validation rule when making a request with a parameter indicating the order.

The order in which they could ask for the results can be of a given number of values. Let's say as an example that the possible values are: name, postalcode and age

And that order could be ascending or descending, and could result in requests like these:

https://www.example.com/api/v1/users?sort=name,-postalcode,-age

I'm trying to find the right validation rule but so far I haven't succeeded.

The problem I am facing is the possible concatenation of the - character and the , together with the fact that the fields must have a suitable value among some possible values. That is, I don't know how to put the rules together:

$validation = Validator::make($request->all(),[
    'sort' => 'sometimes|regex:/(^-?+(name|postalcode|age)+(,)?+(-)?){1,}/',
]);

I guess the rule would be something like:

  • There can be an optional - character
  • followed by one of these values: name, postalcode or age.
  • Followed by a character , (only if there is a character after it)
  • And all this can be repeated one or more times.

Please could you help me to find out what the appropriate validation rule should be, or at least give me some guidance.

Thank you very much in advance.

5
  • What about using an array instead: https://www.example.com/api/v1/users?sort[]=name&sort[]=-postalcodesort[]=-age That would simplify the validation a lot Commented May 27, 2021 at 11:23
  • Personally I wouldn't bother validating the way you are currently. I would have a list of fields that are acceptable and just ignore any options that are sent which are not in the list. Commented May 27, 2021 at 11:49
  • @shaedrich Thanks for the note but I want to make it so that users can enter these parameters as conveniently and easily as possible. Commented May 28, 2021 at 7:25
  • @Peppermintology Thanks for the tip but I don't think this is a good idea. Think that queries can concatenate several values, in different writing order and with the possible minus sign. In total the number of combinations would be very high. Commented May 28, 2021 at 7:27
  • @cooper You misunderstand. I am not suggesting you create a list of all possible combinations, just a list of accepted field names which you use to compare the incoming against. If it is not in the list, ignore it rather than throwing a validation error. Commented May 28, 2021 at 7:50

1 Answer 1

3

You can use

$validation = Validator::make($request->all(),[
    'sort' => [
        'sometimes',
        'regex:/^(-?(?:name|postalcode|age))(?:,(?1))*$/'
     ],
]);

See the regex demo.

Details:

  • ^ - start of string
  • (-?(?:name|postalcode|age)) - Group 1:
    • -? - an optional -
    • (?:name|postalcode|age) - one of the words listed
  • (?:,(?1))* - zero or more repetitions of a comma and Group 1 pattern
  • $ - end of string.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.