对象数组的AJV模式验证

时间:2017-05-23 11:23:42

标签: javascript node.js ajv

我正在尝试使用AJV模式验证来验证对象数组。以下是示例代码

var Ajv = require('ajv');
var schemaValidator = Ajv();

var innerSchema = {
"type" : "object",
"properties" : {
    "c" :  {
        "type" : "string"
    },
    "d" : {
        "type" : "number"
    }
},
"required" : ["c"]
}

var innerArraySchema = {
"type": "array",
"items" : {
    "#ref": innerSchema
}
}

var schema = {
"type" : "object",
"properties" : {
    "a" :  {
        "type" : "string"
    },
    "b" : {
        "type" : "string"
    },
    "obj" : innerArraySchema
},
"required" : ["a"]
}

var testSchemaValidator = schemaValidator.compile(schema);

var data = {"a": "123","b" : "abc", "obj" : [{
"d" : "ankit"
}]}


var valid = testSchemaValidator(data);

console.log(valid);

if(!valid) {
    console.log(testSchemaValidator.errors);
}

我在这里缺少什么东西。我不想在数组定义本身中添加属性对象。

1 个答案:

答案 0 :(得分:7)

使用以下方法解决了该问题:

 $http.get(base_url+"user/feach_one")
    .then(function (response) {$scope.json = response.data;
     $scope.name=$scope.json.name;
    });
 $scope.basic={
        name:// I want to get $scope.json.name here  
    };