简体   繁体   中英

Joi validation library returns different result when run in REPL that when run in script

Here is example validating basic javascript object using joi library (script and REPL):

/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] }

I was expecting both REPL code and script code to error. How can the same exact code do different things depending on how it is run (in script vs in REPL)?

Update with latest commands... Changed file name:

/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)

Then changed it back:

/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' }));

Now the result is different to before, why?

You aren't telling your shell script to run as a node app. Your bash script has no idea what to do with javascript commands unless you tell it to execute as node. Try adding the following to the top of your file.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM