Lodash - 深度类型验证

问题描述 投票:-1回答:1

是否有一个lodash方法(或简单的javascript中的好方法)用于根据模式定义验证嵌套对象。

例如,给定一个模式定义对象:

{ 
  profile: {
    application: {
      dates:{
        startDate: String,
        endDate: String
      },
      status: String,
      jobs: [{
        type: Number,
        title: String
      }]
    }
  }
}

我想根据定义键入验证此示例对象:

{
  "profile": {
    "application": {
      "dates": {
        "startDate": "2011-09-20T15:00:00.000Z",
        "endDate": "2018-10-05T15:00:00.000Z"
      },
      "status": "PENDING",
      "jobs": [
        {
          "type": 5,
          "title": "Waiter"
        },
        {
          "type": 1,
          "title": "Engineer"
        },
        {
          "type": 33,
          "title": "Artist"
        },
      ]
    }
  }
}
node.js validation types lodash
1个回答
-1
投票

如果你正在使用像标签描述的Node.js,你可以使用一个模式验证库,那里有一些。一个例子是jsonschema

简单的例子:

var Validator = require('jsonschema').Validator;
var v = new Validator();
var instance = 4;
var schema = {"type": "number"};
console.log(v.validate(instance, schema));
© www.soinside.com 2019 - 2024. All rights reserved.