当在脚本中运行时,JOI验证库将在REPL中运行时返回不同的结果



这是示例使用JOI库验证基本的JavaScript对象(脚本和替补(:

/tmp/validate-test $ cat test.sh
var Joi = require('joi');
Joi.validate({ status: 'success' }, Joi.object().keys({ status: 'qwerty' }));
/tmp/validate-test $ node test.sh
/tmp/validate-test $ echo $?
0
/tmp/validate-test $ node
> var Joi = require('joi');
undefined
> Joi.validate({ status: 'success' }, Joi.object().keys({ status: 'qwerty' }));
{ error:
   { ValidationError: child "status" fails because ["status" must be one of [qwerty]]
       at Object.exports.process (/private/tmp/validate-test/node_modules/joi/lib/errors.js:203:19)
       at internals.Object._validateWithOptions (/private/tmp/validate-test/node_modules/joi/lib/types/any/index.js:764:31)
       at module.exports.internals.Any.root.validate (/private/tmp/validate-test/node_modules/joi/lib/index.js:147:23)
       at repl:1:5
       at Script.runInThisContext (vm.js:96:20)
       at REPLServer.defaultEval (repl.js:329:29)
       at bound (domain.js:396:14)
       at REPLServer.runBound [as eval] (domain.js:409:12)
       at REPLServer.onLine (repl.js:627:10)
       at REPLServer.emit (events.js:187:15)
     isJoi: true,
     name: 'ValidationError',
     details: [ [Object] ],
     _object: { status: 'success' },
     annotate: [Function] },
  value: { status: 'success' },
  then: [Function: then],
  catch: [Function: catch] }

我期望引用代码和脚本代码出错。相同的确切代码如何根据其运行方式来执行不同的操作(在REPP中的脚本中(?

更新带有最新命令...更改文件名:

/tmp/validate-test $ mv test.sh test.js
/tmp/validate-test $ node test.js
/private/tmp/validate-test/node_modules/joi/lib/index.js:185
                throw error;
                ^
ValidationError: {
  "status" [1]: "success"
}
[1] "status" must be one of [qwerty]
    at Object.exports.process (/private/tmp/validate-test/node_modules/joi/lib/errors.js:203:19)
    at internals.Object._validateWithOptions (/private/tmp/validate-test/node_modules/joi/lib/types/any/index.js:764:31)
    at module.exports.internals.Any.root.validate (/private/tmp/validate-test/node_modules/joi/lib/index.js:147:23)
    at module.exports.internals.Any.root.attempt (/private/tmp/validate-test/node_modules/joi/lib/index.js:177:29)
    at module.exports.internals.Any.root.assert (/private/tmp/validate-test/node_modules/joi/lib/index.js:172:14)
    at Object.<anonymous> (/private/tmp/validate-test/test.js:2:5)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)

然后更改回:

/tmp/validate-test $ mv test.js test.sh
/tmp/validate-test $ node test.sh
/private/tmp/validate-test/node_modules/joi/lib/index.js:185
                throw error;
                ^
ValidationError: {
  "status" [1]: "success"
}
[1] "status" must be one of [qwerty]
    at Object.exports.process (/private/tmp/validate-test/node_modules/joi/lib/errors.js:203:19)
    at internals.Object._validateWithOptions (/private/tmp/validate-test/node_modules/joi/lib/types/any/index.js:764:31)
    at module.exports.internals.Any.root.validate (/private/tmp/validate-test/node_modules/joi/lib/index.js:147:23)
    at module.exports.internals.Any.root.attempt (/private/tmp/validate-test/node_modules/joi/lib/index.js:177:29)
    at module.exports.internals.Any.root.assert (/private/tmp/validate-test/node_modules/joi/lib/index.js:172:14)
    at Object.<anonymous> (/private/tmp/validate-test/test.sh:2:5)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
/tmp/validate-test $ cat test.sh
var Joi = require('joi');
Joi.assert({ status: 'success' }, Joi.object().keys({ status: 'qwerty' }));

现在结果与以前不同,为什么?

您没有告诉shell脚本作为节点应用程序运行。您的bash脚本不知道该如何处理JavaScript命令,除非您告诉它作为节点执行。尝试将以下内容添加到文件的顶部。

#!/usr/bin/env node
var Joi = require('joi');
Joi.validate({ status: 'success' }, Joi.object().keys({ status: 'qwerty' }));

最新更新