Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Validate if object is empty from values in an array – Javascript

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Thank you very much for the help 🙏🏻

>Solution :

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

Use includes to check the elements inside array,

object.business.type.includes("Corporation")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading