Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5947057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:54:38+00:00 2026-05-22T16:54:38+00:00

What is the best way to test the integrity of a complex object in

  • 0

What is the best way to test the integrity of a complex object in JavaScript?

My object has a bunch of different variables, some optional, some required. The correct structure is vital to the code’s functionality, but if I make a mistake during the definition, finding the exact value that caused the problem could get very tedious. Especially with error messages that tell me no more than “Somwehere in the code you’re using the wrong variable type!”.

My object could look something like this, for example:

{
  name:"Test",
  categories:{
    A:{
      depth:1,
      groups:{
        main:[
          {myFunction:funcA, arg:[1,2]},
          {myFunction:funcB}
        ]
      }
    },
    B:{
      groups{
        main:[
          {myFunction:funcC}
        ],
        secondary:[
          {myFunction:funcD}           
        ]
      }
    }
  }
}

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:54:39+00:00Added an answer on May 22, 2026 at 4:54 pm

    Okay, this is how I’ve solved it: I create an object that defines what my complex objects should look like. A blueprint, in a way.

    var structure = {
      property1: {id:"name", type:"string", required:false},
      property2: {
        id:"categories", type:"object", required:true,
        content:{
          property1:{
            type:"object", min:1,
            content:{
              property1:{id:"depth", type:"positiveinteger", required:false},
              property2:{
                id:"groups", type:"object", required:true,
                content:{
                  property1:{
                    type:"array", min:1,
                    content:{
                      type:"object", min:1,
                      content:{
                        property1:{id:"myFunction", type:"function", required:true},
                        property2:{id:"args", type:"array", required:false},
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    

    Then I run a function that compares the blueprint (“struct”) with the object to be tested (“def”):

    function compareStructure(struct, def){
      if(isArray(def)){
        if(isDefined(struct.min) && def.length < struct.min){ alert("Error in structur check: " + " min is " + struct.min + "."); return false}
        if(isDefined(struct.max) && def.length > struct.max){ alert("Error in structur check: " + " max is " + struct.max + "."); return false}
        for(var i = 0; i < def.length; i++){
          compareStructure(struct.content, def[i]);
        }
      } else {
        for(var k in struct){
          if(struct[k].id){
            propFound = false;
            for(var m in def) if(m == struct[k].id) propFound = m;
            if(!propFound && struct[k].required){ alert("Error in structure check:  " + struct[k].id + " not defined."); return false}
            if(propFound && !verifyThis(struct[k], def[propFound], propFound)) return false;
            if(propFound && struct[k].content) compareStructure(struct[k].content, def[propFound]);
          } else {
            for(var m in def){
              if(!verifyThis(struct[k], def[m], m)) return false;
              if(struct[k].content) compareStructure(struct[k].content, def[m]);
            }
          }
        }
      }
    }
    
    function verifyThis(struct, def, prop){
      // This is where the checks for types and values are made.
    }
    

    The comparison function is still work in progress, but that’s the concept I’m going with for now.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What's the best way to test a bunch of different test cases with RSpec?
Can anyone tell me what's the best way to test JavaScript code (particularly linking
What is the best way to test if an NSArray contains an object of
What is the best way to test web service using NUnit. I want it
What is the best way to test an implementation of a mutex is indeed
Just wondering whats the best way to test Python CGI while developing a site?
Whats the best/easiest way to test for administrative rights in a PowerShell script? I
What's the best practices way to test that a model is valid in rails?
What is the best way to unit test heavily database dependant .NET middleware? E.g.
What is the best way to integration test EJBs and REST-Resources?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.