Hapi / Joi替代品何时是-测试正则表达式

时间:2018-08-02 09:33:58

标签: javascript hapijs joi

我有以下joi模式:

const schema = Joi.object({
    name: Joi.string().required(),
    resource: Joi.object({
        uri: Joi.string(),
    }),
    fov: Joi.alternatives().when('resource.uri', {
        is: Joi.string().regex(/\.jpg$/),
        then: Joi.any().forbidden(),
        otherwise: Joi.number().required()
    })
});

我原本希望在发送以下请求正文时

{
    name: 'name', 
    resource: {
         uri: 'file.jpg'
    }, 
    fov: 10
}

我会得到一个错误,因为when.is条件的正则表达式将匹配'file.jpg',因此fov将被验证为Joi.any().forbidden()。但是验证似乎被忽略了。您有任何想法我在做什么错吗?

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

通过发布问题,我找到了答案:

我的初始验证对象如下:

resource: Joi.object({
    uri: Joi.alternatives().try(
        Joi.string(),
        Joi.array().max(config.objectTargets.maxImages).items(Joi.string())
    ),
    fov: Joi.alternatives().when('resource.uri', {
        is: Joi.string().regex(/\.wto$/),
        then: Joi.any().forbidden(),
        otherwise: Joi.number().required()
    }),
})

因此它正在resource.uri属性内寻找resource。解决方案是使when条件成为对uri的引用,例如:

resource: Joi.object({
    uri: Joi.alternatives().try(
        Joi.string(),
        Joi.array().max(config.objectTargets.maxImages).items(Joi.string())
    ),
    fov: Joi.alternatives().when('uri', {
        is: Joi.string().regex(/\.wto$/),
        then: Joi.any().forbidden(),
        otherwise: Joi.number().required()
    }),
})

很抱歉以前没专心

答案 1 :(得分:0)

好吧,它似乎可以正常工作,但是不会引发错误,它包含在返回值中:

const Joi = require('joi');

const schema = Joi.object({
    name: Joi.string().required(),
    resource: Joi.object({
        uri: Joi.string(),
    }),
    fov: Joi.alternatives().when('resource.uri', {
        is: Joi.string().regex(/\.jpg$/),
        then: Joi.any().forbidden(),
        otherwise: Joi.number().required()
    })
});

const testVal = format => ({
    name: 'name',
    resource: {
         uri: format
    },
    fov: 10
});

const png = 'file.png';
const jpg = 'file.jpg';

const testPng = testVal(png);
const testJpg = testVal(jpg);

console.log(Joi.validate(testJpg, schema));
console.log("------------------------------");
console.log(Joi.validate(testPng, schema));

给出此结果:

{ error: 
   { ValidationError: child "fov" fails because ["fov" is not allowed]
    at Object.exports.process (/home/boehm-s/tmp/node_modules/joi/lib/errors.js:196:19)
    at internals.Object._validateWithOptions (/home/boehm-s/tmp/node_modules/joi/lib/types/any/index.js:675:31)
    at module.exports.internals.Any.root.validate (/home/boehm-s/tmp/node_modules/joi/lib/index.js:138:23)
    at Object.<anonymous> (/home/boehm-s/tmp/plop.js:29:17)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Function.Module.runMain (module.js:684:10)
     isJoi: true,
     name: 'ValidationError',
     details: [ [Object] ],
     _object: { name: 'name', resource: [Object], fov: 10 },
     annotate: [Function] },
  value: { name: 'name', resource: { uri: 'file.jpg' }, fov: 10 },
  then: [Function: then],
  catch: [Function: catch] }
------------------------------
{ error: null,
  value: { name: 'name', resource: { uri: 'file.png' }, fov: 10 },
  then: [Function: then],
  catch: [Function: catch] }

fov不允许,因为ressource.uri与您提供的正则表达式匹配。