ajv自定义关键字或自定义验证

时间:2019-09-29 13:32:59

标签: ajv

我有一个模式(用于说明目的):

Schema-link

{
  "subjects":[
    {
      "type":"english",
      "days":{
       "mon":"yes",
       "tue":"yes"  //any value
       }
    },
    {
      "type":"french",
      "mon": "yes"  //any value
    }
]}

有没有一种方法可以使用ajv自定义关键字验证程序来提取以下值:

// 1'subjects [0] .days.mon'

// 2'subjects 1。mon'

如果// 1有一个值,那么我应该为// 2闪烁一个错误,因为它不能有任何值,包括空格。

使用伪代码:

//assuming the json object and the respective keys are there
if(subjects[0] !== undefined && subjects[0].days.mon == "yes"){
  if(subjects[1].mon == "yes"){
    //JSON Schema error ..
  }
}else{
    if(subjects[1].mon == ""){
      //JSON Schema error ..
    }else{
      //Everything is good
    }
}

我从ajv自定义验证的角度看更多。

P.S:ajv自定义关键字

ajv.addKeyword('myCustomValidation', {
  validate: function(schema, data, dataPath, parentData, parentDataProperty){
   //But here I cannot get the entire array here to do a check.
   //I know this can be handle when I have a JSON at nodejs server side, but anything with ajv custom keyword will be nice
}}

0 个答案:

没有答案