Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. import floJsModels from '../../src/flo-js-models';
  2. const Joi = require('joi');
  3. const t = require('tcomb-validation');
  4. const validate = t.validate;
  5.  
  6. describe('floJsModels', () => {
  7. describe('Greet function', () => {
  8. it('JOI validation', () => {
  9.  
  10. const schema = Joi.object().options({ abortEarly: false }).keys({
  11. username: Joi.string().alphanum().min(3).max(30).required(),
  12. password: Joi.string().regex(/^[a-zA-Z0-9]{3,30}$/),
  13. access_token: [Joi.string(), Joi.number()],
  14. birthyear: Joi.number().integer().min(1900).max(2013).options({
  15. language: {
  16. number: {
  17. max: 'Max message (Facu)!!'
  18. }
  19. }
  20. }),
  21. email: Joi.string().email(),
  22. other: Joi.object().required().keys({
  23. b: Joi.number().integer().options({
  24. language: {
  25. number: {
  26. base: 'b number (Facu)!!'
  27. }
  28. }
  29. }),
  30. c: Joi.number().integer().min(Joi.ref('b'))
  31.  
  32. /*
  33. This does not work, so is dificult to override specific types messages like integer
  34. .options({
  35. language: {
  36. number: {
  37. integer: {
  38. base: 'must be hexadecimal numbers separated with dashes'
  39. }
  40. }
  41. }
  42. })
  43. */
  44.  
  45. })
  46. }).with('username', 'birthyear').without('password', 'access_token');
  47.  
  48. // Return result.
  49. const {error, value} = Joi.validate({ username: 'abc', birthyear: '2994', other: { b: '2', c: 1 } }, schema);
  50.  
  51.  
  52. console.log(error.details);
  53. });
  54.  
  55. it('tcomb validation', () => {
  56.  
  57. //Override default error messages strategy
  58. /*
  59. t.ValidationError.of = function (actual, expected, path, context) {
  60. return new t.ValidationError({
  61. message: 'Hola pepe!!!' + expected.message(actual, expected, path, context),
  62. actual: actual,
  63. expected: expected,
  64. path: path
  65. });
  66. };
  67. */
  68.  
  69.  
  70. //Override default tcomb types error messages
  71. t.Number.getValidationErrorMessage = function (actual, path, context) {
  72. return path.length ? path.join('.') + ' should be a number.' : 'Should be a number.';
  73. };
  74.  
  75. // an object with two numerical properties
  76. let Point = t.struct({
  77. x: t.Number,
  78. y: t.struct({
  79. b: t.Number
  80. })
  81. }, 'Point');
  82.  
  83.  
  84. //Add a more sophisticated velidation like inter fields validations
  85. let RarePoint = t.refinement(Point, o => o.x > o.y.b, 'RatePoint');
  86.  
  87. //Add the error message for the refinement type
  88. RarePoint.getValidationErrorMessage = function (actual, expected, path, context) {
  89. return 'X should be grater than y.b';
  90. };
  91.  
  92. let MoreRarePoint = t.refinement(RarePoint, o => o.y.b < 3, 'MoreRarePoint');
  93. MoreRarePoint.getValidationErrorMessage = function (actual, expected, path, context) {
  94. return 'y.b should be less than 3';
  95. };
  96.  
  97.  
  98. let obj = {x: '10', y: { b: '2' }};
  99.  
  100. console.log(validate(obj, MoreRarePoint).isValid()); // => false, y is missing
  101.  
  102. let errors = validate(obj, MoreRarePoint).errors
  103. let errorMessages = validate(obj, MoreRarePoint).errors.map(x => x.message);
  104.  
  105. console.log(errorMessages);
  106.  
  107. //expect(floJsModels.greet).to.have.always.returned('hello');
  108. });
  109. });
  110. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement