Not_required_when validator in adonis

I there a way to extend the validator to create own validation rule, or using existing validator to achieve my objective.

My scenario:
I have three roles; say role_id=1, role_id=2, role_id=2
i have one basic registration rule for all roles and another rule object for their specific fields. I need to make single rule object for all with conditions
here is my approach;

basic_registration : {
        display_picture: 'required',
        first_name: 'required',
        last_name: 'required',
        role_id: 'required',  
        phone_number: 'required|max:15', 
        email: 'required|email|unique:users,email',
        address: 'required',
        state: 'required',
        city: 'required',
        zip_code: 'required',  
        password: 'required',
        password_confirmation: 'required_if:password|same:password',
    },
role1_specific_registration_rules : {
        specific_type_id: 'required',
        other_picture: 'required'
},

role2_specific_registration_rules : {
        discount_code: 'required',
        notification: 'required',
},
role3_specific_registration_rules : {
        vehicle_id: 'required',
        make: 'required',
},

then I am calling validation every time for every rule, is there a way to use

not_required_when:role_id,1 // not required when role_id is 1
not_required_when:role_id,2 // not required when role_id is 2
not_required_when:role_id,1,3 // not required when role_id is 1 and 3

or any approach to use
required_when:role_id,1,2 // required when role_id is 1 or 2

1 Like

There are such rules:

requiredIf
requiredWhen
requiredWithAll
requiredWithAny
requiredWithoutAll
requiredWithoutAny

Documentation about those are in here: https://indicative.adonisjs.com/validations/master/requiredIf
and
https://indicative-v5.adonisjs.com/docs/requiredif
If I remember correctly indicative-v5 was used in AdonisJS version 4.x while first one was for AdonisJS v5, but I might remember it wrong