const expect = require('chai').expect;
const expressValidator = require('..');

describe('Custom validators', () => {
  it('exist in the validator chain', () => {
    const req = {};
    expressValidator({
      customValidators: {
        isFoo: () => true
      }
    })(req, {}, () => {});

    expect(req.check('foo').isFoo).to.be.a('function');
  });

  it('receive the value and additional args', () => {
    const req = {
      body: {
        twoWords: 'two words',
        threeWords: 'three words?'
      }
    };

    expressValidator({
      customValidators: {
        containsWords: (value, number) => value.split(' ').length === number
      }
    })(req, {}, () => {});

    req.check('twoWords').containsWords(2);
    req.check('threeWords').containsWords(3);

    return req.getValidationResult().then(result => {
      expect(result.mapped()).to.not.have.property('twoWords');
      expect(result.mapped()).to.have.property('threeWords');
    });
  });

  it('are appended to existing custom validators built by another middleware instance on the same app', () => {
    const req = {};
    expressValidator({
      customValidators: {
        isFoo: () => true
      }
    })(req, {}, () => {});

    expressValidator({
      customValidators: {
        isBar: () => true
      }
    })(req, {}, () => {});

    expect(req.check('foo')).to.have.property('isFoo').and.to.be.a('function');
    expect(req.check('foo')).to.have.property('isBar').and.to.be.a('function');
  });

  it('are not shared across apps', () => {
    const req1 = {};
    expressValidator({
      customValidators: {
        isFoo: () => true
      }
    })(req1, {}, () => {});

    const req2 = {};
    expressValidator({
      customValidators: {
        isBar: () => true
      }
    })(req2, {}, () => {});

    expect(req2.check('foo').isFoo).to.not.exist;
    expect(req1.check('bar').isBar).to.not.exist;
  });

  it('work asynchronously', () => {
    const req = {
      body: { username: 'george' }
    };

    expressValidator({
      customValidators: {
        usernameAvailable: () => Promise.reject()
      }
    })(req, {}, () => {});

    req.check('username').usernameAvailable().withMessage('username taken');

    return req.getValidationResult().then(result => {
      expect(result.mapped()).to.have.deep.property('username.msg', 'username taken');
    });
  });
});
@@ -2367,12 +2367,800 @@
 );%0A  %7D);
+%0A%0A  it('work with schemas', () =%3E %7B%0A    const req = %7B%0A      body: %7B foo: 'foo', foo2: 'FoO', foo3: 'bar!' %7D%0A    %7D;%0A%0A    expressValidator(%7B%0A      customValidators: %7B%0A        isFoo: (val, options = %7B%7D) =%3E %7B%0A          const checkedVal = options.caseInsensitive ? val.toLowerCase() : val;%0A          return checkedVal === 'foo';%0A        %7D%0A      %7D%0A    %7D)(req, %7B%7D, () =%3E %7B%7D);%0A%0A    req.check(%7B%0A      foo: %7B isFoo: true %7D,%0A      foo2: %7B%0A        isFoo: %7B%0A          options: %7B caseInsensitive: true %7D%0A        %7D%0A      %7D,%0A      foo3: %7B isFoo: true %7D%0A    %7D);%0A%0A    return req.getValidationResult().then(result =%3E %7B%0A      expect(result.mapped()).to.not.have.property('foo');%0A      expect(result.mapped()).to.not.have.property('foo2');%0A      expect(result.mapped()).to.have.property('foo3');%0A    %7D);%0A  %7D);
 %0A%7D);

Add test for customValidators with schemas