简体   繁体   中英

Joi Validation does not work on the below mentioned schema

{
    "email": "{{$randomExampleEmail}}",
    "dateofbirth": "2000-01-09",
    "personal_document":{
        "document_front":"url",
        "document_back":"url",
        "document_type":"CITIZENSHIP"
    },
    "company": {
        "address":{
            "city":"CITY",
            "state":"STATE",
        },
        "company_name":"${{$randomCompanyName}}",
        "company_document": {
            "companyRegistrationDoc":"example_url",
            "documentType": "vat",
            "registationNumber": "1232223"
        }
    },
    "isCompanyRegistration":false,
    "role": "VERIFIED_SELLER"
}

When client sends isCompanyRegsitration: true and role: VERIFIED_SELLER then i need to make sure that company property exists otherwise not and when isCompanyRegistration:false and role: SELLER then personal_document is required

My Joi Schema

const schema = Joi.object({
    email: Joi.string().trim().email().required(),
    company: Joi.object({
        company_name: Joi.string().trim().min(3).max(50).required(),
        address: Joi.object({
            city: Joi.string().trim().min(3).max(50).required(),
        }),

        company_document: Joi.object({
            documentType: Joi.any().required().valid("pan", "vat"),
            companyRegistrationDoc: Joi.string().required(),
            registationNumber: Joi.string().required(),
        }),
    }),
    personal_document: Joi.object({
        document_type: Joi.any()
            .required()
            .valid(...Object.keys(DOCUMENT_TYPE)),
        document_front: Joi.string().required(),
        document_back: Joi.string().required(),
    }),

    isCompanyRegistration: Joi.boolean().required(),
    role: Joi.any()
        .required()
        .valid(...Object.keys(ACCOUNT_TYPE)), //account type is VERIFIED_SELLER or SELLER
});

I tried doing below mentioned thing but it doesnot seems to work. i also tried with xor and and but i cannot see if the value is true of false


when("role", {
            is: Joi.string().valid(ACCOUNT_TYPE.SELLER),
            then: Joi.object({
                document_type: Joi.any()
                    .required()
                    .valid(...Object.keys(DOCUMENT_TYPE)),
                document_front: Joi.string().required(),
                document_back: Joi.string().required(),
            }),
        }),

But this doesnot works and seems redundant

Try following

const schema = Joi.object({
    email: Joi.string().trim().email().required(),
    company: Joi.object({
        company_name: Joi.string().trim().min(3).max(50).required(),
        address: Joi.object({
            city: Joi.string().trim().min(3).max(50).required(),
        }),

        company_document: Joi.object({
            documentType: Joi.any().required().valid("pan", "vat"),
            companyRegistrationDoc: Joi.string().required(),
            registationNumber: Joi.string().required(),
        }),
    }).when('role', { is: 'VERIFIED_SELLER', then: Joi.required() }),
    personal_document: Joi.object({
        document_type: Joi.any()
            .required()
            .valid(...Object.keys(DOCUMENT_TYPE)),
        document_front: Joi.string().required(),
        document_back: Joi.string().required(),
    }).when('role', { is: 'SELLER', then: Joi.required() }),

    isCompanyRegistration: Joi.boolean().required(),
    role: Joi.any()
        .required()
        .valid(...Object.keys(ACCOUNT_TYPE)), //account type is VERIFIED_SELLER or SELLER
});

Basically you need to add inline condition to each object

personal_document: Joi.object({...}).when('role', { is: 'SELLER', then: Joi.required() })

company: Joi.object({...}).when('role', { is: 'VERIFIED_SELLER', then: Joi.required() })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM