在 Fastify 中使用 `fluent-json-schema` 处理验证错误
Handling validation errors using `fluent-json-schema` in Fastify
我正在使用 fluent-json-schema
来验证请求。具有以下架构:
const gwoogl = S.object()
.prop('title_desc', S.string()/*.allow('')*/.minLength(3).maxLength(100))
.prop('exact', S.boolean().default(false))
.prop('div_q', S.string()/*.allow('')*/.minLength(3).maxLength(40))
.prop('since', S.string().format(S.FORMATS.DATE_TIME))
.prop('section', S.string().enum(['donations', 'skills']))
const gwooglSchema = {
body: gwoogl,
}
在消费的地方显示如下(所以我认为格式是正确的):
{"level":50,"time":1643319721118,"pid":9712,"hostname":"WIN-E91D14FS6F4","body":{"isFluentSchema":true,"isFluentJSONSchema":true}}
问题是我看不到我是如何处理错误的,而且似乎根本没有考虑架构,我可以 POST { title_desc: 'a' }
不能验证。
fastify.post('/gwoogl', { gwooglSchema }, async (req, reply) => {});
我的差我应该通过了:{ schema: gwooglSchema }
我正在使用 fluent-json-schema
来验证请求。具有以下架构:
const gwoogl = S.object()
.prop('title_desc', S.string()/*.allow('')*/.minLength(3).maxLength(100))
.prop('exact', S.boolean().default(false))
.prop('div_q', S.string()/*.allow('')*/.minLength(3).maxLength(40))
.prop('since', S.string().format(S.FORMATS.DATE_TIME))
.prop('section', S.string().enum(['donations', 'skills']))
const gwooglSchema = {
body: gwoogl,
}
在消费的地方显示如下(所以我认为格式是正确的):
{"level":50,"time":1643319721118,"pid":9712,"hostname":"WIN-E91D14FS6F4","body":{"isFluentSchema":true,"isFluentJSONSchema":true}}
问题是我看不到我是如何处理错误的,而且似乎根本没有考虑架构,我可以 POST { title_desc: 'a' }
不能验证。
fastify.post('/gwoogl', { gwooglSchema }, async (req, reply) => {});
我的差我应该通过了:{ schema: gwooglSchema }