gpt4 book ai didi

javascript - Hapi Joi - 验证串联键

转载 作者:行者123 更新时间:2023-12-01 00:08:08 24 4
gpt4 key购买 nike

我有一个像这样的请求正文:

bod = {
file: '123654',
extension: 'bbb',
...
}

我正在使用 hapi joi 来验证它的字段:

let valSchema = {};
valSchema.play = Joi.object().keys({
file: Joi.string().required().message("Invalid name. Please check documentation."),
extension: Joi.string().empty().required().message("No extension sent. check the docs..."),
});

module.exports.valSchema = valSchema;

问题是其中一个验证需要检查“$file:$extension”是否在数组中,例如:

validConcatenations = [
"1111:aaa","111123:bbb", "12312:aab",
"21323:zzzs" ... ];

我需要这样做:

if (data.file && settings.fileWithExtensions.indexOf(`${data.file}:${data.extension}`) < 0) {
return errors.invalidSampleRate;
}

使用 joi...可以使用它吗?使用与?

最佳答案

这是可能的,您需要做的就是 custom validation如下例所示。

版本 16.1.8 或更高版本

'use strict';

const Joi = require('@hapi/joi');

const customValidation = function (value, helpers) {
const validConcatenations = [
"1111:aaa", "111123:bbb", "12312:aab", "21323:zzzs"];
if (!validConcatenations.includes(`${value.file}:${value.extension}`)) {
throw Error(`Wrong concatenation ${value.file}:${value.extension}`)
}
return value;
};

const schema = Joi.object().keys({
file: Joi.string().required(),
extension: Joi.string().empty().required(),
}).custom(customValidation);

const wrongData = { file: '123654', extension: 'bbb' };
const niceData = { file: '1111', extension: 'aaa' };

console.log(schema.validate(wrongData));
console.log(schema.validate(niceData));

输出:

{ value: { file: '123654', extension: 'bbb' },
error:
{ ValidationError: "value" failed custom validation because Wrong concatenation 123654:bbb
_original: { file: '123654', extension: 'bbb' },
details: [ [Object] ] } }
{ value: { file: '1111', extension: 'aaa' } }

查看the docs从版本12.1.0

开始可以执行以下解决方案

版本 12.1.0 或更高版本

'use strict';

const _Joi = require('@hapi/joi');

const custom = {
base: _Joi.object(),
name: 'custom',
language: {
concatenation: 'is a invalid concatenation',
},
rules: [
{
name: 'concatenation',
validate(params, value, state, options) {
const validConcatenations = [
"1111:aaa", "111123:bbb", "12312:aab", "21323:zzzs"];
if (!validConcatenations.includes(`${value.file}:${value.extension}`)) {
return this.createError('custom.concatenation', {v: value}, state, options);
}
return value;
}
}
]
};

const Joi = _Joi.extend(custom);

const schema = Joi.custom().keys({
file: Joi.string().required(),
extension: Joi.string().empty().required(),
}).concatenation();

const wrongData = { file: '123654', extension: 'bbb' };
const niceData = { file: '1111', extension: 'aaa' };

console.log(schema.validate(wrongData));
console.log(schema.validate(niceData));

输出:

{ error: 
{ ValidationError: "value" is a invalid concatenation
at Object.exports.process (/tmp/test/node_modules/@hapi/joi/lib/errors.js:202:19)
at type._validateWithOptions (/tmp/test/node_modules/@hapi/joi/lib/types/any/index.js:762:31)
at type.validate (/tmp/test/node_modules/@hapi/joi/lib/types/any/index.js:796:21)
at Object.<anonymous> (/tmp/test/index.js:36:20)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Function.Module.runMain (module.js:694:10)
isJoi: true,
name: 'ValidationError',
details: [ [Object] ],
_object: { file: '123654', extension: 'bbb' },
annotate: [Function] },
value: { file: '123654', extension: 'bbb' },
then: [Function: then],
catch: [Function: catch] }
{ error: null,
value: { file: '1111', extension: 'aaa' },
then: [Function: then],
catch: [Function: catch] }

关于javascript - Hapi Joi - 验证串联键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60231147/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com