简体   繁体   English

如何在电视上引发TV4错误?

[英]How do I throw a tv4 error with a message?

The data schema below has an error in it and there is an object present for the test that fails validation. 下面的数据模式中有一个错误,并且存在一个未通过验证的测试对象。 I can throw the error but there's no error message. 我可以抛出错误,但是没有错误消息。

var tv4 = require('tv4')

var schema = {
 "items": {
   "type": "boolean"
 }
}

tv4.setErrorReporter(function (error, data, schema) {
  return "Custom error message"
})

var data1 = [true, false]
var data2 = [true, 123]

console.log(tv4.validate(data1, schema))
if (tv4.error) throw tv4.error
console.log(tv4.validate(data2, schema))
if (tv4.error) throw tv4.error

The message still has no error message. 该消息仍然没有错误消息。

Error
    at new ValidationError (/project/node_modules/tv4/tv4.js:1461:12)
    at ValidatorContext.createError (/project/node_modules/tv4/tv4.js:359:14)
    at ValidatorContext.validateType (/project/node_modules/tv4/tv4.js:751:14)
    at ValidatorContext.validateBasic (/project/node_modules/tv4/tv4.js:721:19)
    at ValidatorContext.validateAll (/project/node_modules/tv4/tv4.js:599:19)
    at ValidatorContext.validateArrayItems (/project/node_modules/tv4/tv4.js:947:21)
    at ValidatorContext.validateArray (/project/node_modules/tv4/tv4.js:880:11)
    at ValidatorContext.validateAll (/project/node_modules/tv4/tv4.js:602:11)
    at Object.api.validate (/project/node_modules/tv4/tv4.js:1573:24)
    at Object.<anonymous> (tv4-test.js:20:17)

How can I throw an invalid schema with a message? 如何抛出带有消息的无效模式?

Thank you this saved almost half of my lyf :), Thank you people and for others always use these 3 lines for schema test: 谢谢,这节省了我一半的lyf :),谢谢大家,对于其他人,请始终使用以下3行进行模式测试:

  console.log(" Schema Valid " +JSON.stringify(tv4.validate(jsonData ,schema)));
  console.log("Validation errors: "+ tv4.error);
  console.log(tv4.error.dataPath); 

and output will be like : 输出将是这样的:

LOGGS 日志

false
Validation errors: ValidationError: Invalid type: string (expected integer)
/guardVerificationUrl

In addition if you want to know what type of error use: 另外,如果您想知道哪种错误类型,请使用:

console.log(tv4.error.schemaPath); 

Output : 输出:

/properties/guardVerificationUrl/type

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM