Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import floJsModels from '../../src/flo-js-models';
- const Joi = require('joi');
- const t = require('tcomb-validation');
- const validate = t.validate;
- describe('floJsModels', () => {
- describe('Greet function', () => {
- it('JOI validation', () => {
- const schema = Joi.object().options({ abortEarly: false }).keys({
- username: Joi.string().alphanum().min(3).max(30).required(),
- password: Joi.string().regex(/^[a-zA-Z0-9]{3,30}$/),
- access_token: [Joi.string(), Joi.number()],
- birthyear: Joi.number().integer().min(1900).max(2013).options({
- language: {
- number: {
- max: 'Max message (Facu)!!'
- }
- }
- }),
- email: Joi.string().email(),
- other: Joi.object().required().keys({
- b: Joi.number().integer().options({
- language: {
- number: {
- base: 'b number (Facu)!!'
- }
- }
- }),
- c: Joi.number().integer().min(Joi.ref('b'))
- /*
- This does not work, so is dificult to override specific types messages like integer
- .options({
- language: {
- number: {
- integer: {
- base: 'must be hexadecimal numbers separated with dashes'
- }
- }
- }
- })
- */
- })
- }).with('username', 'birthyear').without('password', 'access_token');
- // Return result.
- const {error, value} = Joi.validate({ username: 'abc', birthyear: '2994', other: { b: '2', c: 1 } }, schema);
- console.log(error.details);
- });
- it('tcomb validation', () => {
- //Override default error messages strategy
- /*
- t.ValidationError.of = function (actual, expected, path, context) {
- return new t.ValidationError({
- message: 'Hola pepe!!!' + expected.message(actual, expected, path, context),
- actual: actual,
- expected: expected,
- path: path
- });
- };
- */
- //Override default tcomb types error messages
- t.Number.getValidationErrorMessage = function (actual, path, context) {
- return path.length ? path.join('.') + ' should be a number.' : 'Should be a number.';
- };
- // an object with two numerical properties
- let Point = t.struct({
- x: t.Number,
- y: t.struct({
- b: t.Number
- })
- }, 'Point');
- //Add a more sophisticated velidation like inter fields validations
- let RarePoint = t.refinement(Point, o => o.x > o.y.b, 'RatePoint');
- //Add the error message for the refinement type
- RarePoint.getValidationErrorMessage = function (actual, expected, path, context) {
- return 'X should be grater than y.b';
- };
- let MoreRarePoint = t.refinement(RarePoint, o => o.y.b < 3, 'MoreRarePoint');
- MoreRarePoint.getValidationErrorMessage = function (actual, expected, path, context) {
- return 'y.b should be less than 3';
- };
- let obj = {x: '10', y: { b: '2' }};
- console.log(validate(obj, MoreRarePoint).isValid()); // => false, y is missing
- let errors = validate(obj, MoreRarePoint).errors
- let errorMessages = validate(obj, MoreRarePoint).errors.map(x => x.message);
- console.log(errorMessages);
- //expect(floJsModels.greet).to.have.always.returned('hello');
- });
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement