【问题标题】:NodeJS Joi Vlidation - How to return JSON response instead of a string?Node JS Joi Validation - 如何返回 JSON 响应而不是字符串?
【发布时间】:2019-03-12 03:21:47
【问题描述】:

最近,我一直在使用 Joi validation library 来验证来自请求的数据(构建 RESTful API)。
我正在使用 .label() 方法生成字符串响应并将其发送回用户,但我找不到任何方法将 JSON 响应发送回用户?
尝试在字符串中发送预制 JSON,当然搜索文档和互联网 - 找不到任何提及它。
当前代码示例:

textField: Joi.string().required().max(4).label("This example field didn't pass the testing phase, please try again)"),

有什么想法吗?

【问题讨论】:

    标签: json node.js validation response joi


    【解决方案1】:

    如果您需要发送数据以防出错,请尝试.error()方法。你可以在其中传递错误。

    【讨论】:

    • 嗨 mehta-roha,这就是我得到的:AssertionError [ERR_ASSERTION]: Must provide a valid Error object or a function.我如何发送 JSON?
    • 使用error.message,你会得到自定义的错误信息。
    • 不起作用,因为您需要将每个函数值指定为它自己的值。有代码示例吗?
    • textField: Joi.string().required().max(4).error('{"testJSON":"This example field didn't pass the testing phase, please try again"}')
    猜你喜欢
    • 2016-03-19
    • 1970-01-01
    • 2015-02-27
    • 1970-01-01
    • 2021-02-11
    • 1970-01-01
    • 1970-01-01
    • 2012-11-26
    • 2022-12-06
    相关资源
    最近更新 更多