1

JavaScript で複雑なオブジェクトの整合性をテストする最良の方法は何ですか?

私のオブジェクトには、いくつかのオプション、いくつかの必須のさまざまな変数があります。コードの機能には正しい構造が不可欠ですが、定義を間違えると、問題の原因となった正確な値を見つけるのが非常に面倒になります。特に、「コードのどこかで間違った変数の型を使用しています!」というエラー メッセージしか表示されません。

私のオブジェクトは、たとえば次のようになります。

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

ありがとう!

4

4 に答える 4

4

オブジェクトを入力として受け取り、それが「正しい」構造であることを検証する関数を記述する以外に、これを行う良い方法はありません。

function isValid(obj)
{
    if (!o) return false;
    if (typeof o.name !== 'string') return false;
    if (typeof o.categories !== 'object') return false;
    if (typeof o.categories.a !== 'object') return false;
    if (typeof o.categories.b !== 'object') return false;
    // etc...

    return true;
}

一方、オブジェクトを適切に構築するために必要な引数を取るコンストラクターを定義できます。

function MyObj(name, categoryNames /* other args */)
{
    this.name = name;
    this.categories = {};

    for (var i=0; i<categoryNames.length; i++)
    {
        this.categories[categoryNames[i]] =
        {
            groups: {main: []}
        };
    }

    // etc
}

// use it like this:
var foo = new MyObj('Test', ['A', 'B'] /* other args */);
于 2011-05-13T15:04:49.883 に答える
0

さて、これが私が解決した方法です。複雑なオブジェクトがどのように見えるべきかを定義するオブジェクトを作成します。ある意味、設計図。

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},
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

次に、設計図 ("struct") とテスト対象のオブジェクト ("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.
}

比較機能はまだ開発中ですが、それが今のコンセプトです。

于 2011-05-20T15:14:07.707 に答える
-1

JSONスキーマ検証を試してみてください。JSONでは無効な関数も使用できるという事実を説明するために、いくつかの変更が必要になる場合があります。

于 2011-05-13T15:08:09.557 に答える
-1

JSON スキーマに対してオブジェクトの検証を試みることができます

しかし、これはやり過ぎかもしれません。

于 2011-05-13T15:02:08.920 に答える