Home > Back-end >  Validate if object is empty from values in an array - Javascript
Validate if object is empty from values in an array - Javascript

Time:03-16

I have a data for businesses like below:

{
    "business": {
        "type": [
            "LLC",
            "Corporation"
        ],
        "LLC": {
            "status": "active",
            "profits": 1000000,
            "period": "yearly"
        },
        "corporation": {},
        "partnership": {}
    }
}

How do I validate the "corporation" object, so if the "type" array contains the string "Corporation", the "corporation" object cannot be empty?

I've tried using validate.js to check if it is empty, it works but I can't specifically put the strings inside the type array to validate it.

const validate = require('validate.js');
if (validate.isEmpty(business.LLC)) {
    return wrapper.error('fail', 'Object is empty');
}

Thank you very much for the help

  • Related