Objection.js 忽略无法识别的 属性 类型的验证?

Objection.js ignoring validation of not recognized property type?

我在我的反对模型中使用这个 jsonSchema:

  static get jsonSchema() {
    return {
      type: 'object',
      required: ['firstName', 'lastName'],

      properties: {
        id: { type: 'integer' },
        parentId: { type: ['integer', 'null'] },
        firstName: { type: 'string', minLength: 1, maxLength: 255 },
        lastName: { type: 'string', minLength: 1, maxLength: 255 },
        date: {type: 'unexisting-type'},
        age: { type: 'number' },
      }
    };
  }

您可以看到 date 是 AJV 验证无法识别的类型(Objection.js 使用)。但是,插入以下数据时插入操作 return 成功:

{
    "firstName":"sample",
    "lastName":"sample",
    "date":"2018-12-21 10:20"
}

为什么 AJV 没有 return 在模式创建时出现验证错误?

我在一个最小的 JSFiddle 示例中进行了测试,它 return 出错了 (AJV v6.6.2) https://jsfiddle.net/ads80y5j/

我正在使用 Objection v1.4.0(使用 AJV v^6.1.1)

当 运行 下面的代码时,我确实得到了以下异常。 (如果我将代码包装在您的 JSFiddle 中,我也会得到类似的结果。)

schema is invalid: data.properties['date'].type should be equal to one of the allowed values, data.properties['date'].type should be array, data.properties['date'].type should match some schema in anyOf

var ajv = new Ajv({
  allErrors: true
});

var schema = {
  type: 'object',
  properties: {
    date: {
      type: 'unexisting-type'
    }
  }
};

try {
  var validate = ajv.compile(schema);
} catch (e) {
  console.log(e.message);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/ajv/6.6.2/ajv.min.js"></script>

可能是在您的代码中的某处,您默默地吞下了异常,或者您以某种方式关闭了架构本身的验证?

来自 compile 的文档:

The schema passed to this method will be validated against meta-schema unless validateSchema option is false. If schema is invalid, an error will be thrown.

反对本身正在向 Ajv 传递关闭模式验证的选项,因此在 Ajv 模式编译时不会抛出任何错误。

目前,Objection v1 将保持这种行为,因为它是一个破坏性的变化。可以在 v2 中添加。

https://github.com/Vincit/objection.js/issues/1182