以下是使用joi库(脚本和REPL)验证基本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] }
我期望REPL代码和脚本代码都出错。 相同的确切代码如何根据运行方式(在脚本中还是在REPL中)执行不同的操作?
使用最新命令更新... 更改的文件名:
/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' }));
现在结果与以前不同,为什么?
答案 0 :(得分:0)
您不是要告诉您的Shell脚本作为节点应用程序运行。您的bash脚本不知道如何处理javascript命令,除非您告诉它作为节点执行。尝试将以下内容添加到文件顶部。
#!/usr/bin/env node
var Joi = require('joi');
Joi.validate({ status: 'success' }, Joi.object().keys({ status: 'qwerty' }));