<commit_before>import { expect } from 'chai';
import satpam from '../lib';

describe('Validator', () => {
  describe('.createRuleObject', () => {
    const validator = satpam.create();

    context('when the given rule is an object', () => {
      context('and it has no rule parameters', () => {
        it('should create valid ruleObj', () => {
          const ruleObj = validator._createRuleObject({
            name: 'wut'
          });

          expect(ruleObj).to.deep.equal({
            name: 'wut',
            fullName: 'wut',
            params: []
          });
        });
      });

      context('and it has one rule parameter', () => {
        it('should create valid ruleObj', () => {
          const ruleObj = validator._createRuleObject({
            name: 'this',
            params: ['is smelll']
          });

          expect(ruleObj).to.deep.equal({
            name: 'this',
            fullName: 'this:$1',
            params: ['is smelll']
          });
        });
      });

      context('and it has two rule parameters', () => {
        it('should create valid ruleObj', () => {
          const ruleObj = validator._createRuleObject({
            name: 'woosah',
            params: ['rocky', 'balboa']
          });

          expect(ruleObj).to.deep.equal({
            name: 'woosah',
            fullName: 'woosah:$1:$2',
            params: ['rocky', 'balboa']
          });
        });
      });
    });

    context('when the given rule is a string', () => {
      context('and it has no rule parameters', () => {
        it('should create valid ruleObj', () => {
          const ruleObj = validator._createRuleObject('minions');

          expect(ruleObj).to.deep.equal({
            name: 'minions',
            fullName: 'minions',
            params: []
          });
        });
      });

      context('and it has one rule parameter', () => {
        it('should create valid ruleObj', () => {
          const ruleObj = validator._createRuleObject('minions:banana');

          expect(ruleObj).to.deep.equal({
            name: 'minions',
            fullName: 'minions:$1',
            params: ['banana']
          });
        });
      });

      context('and it has two rule parameters', () => {
        it('should create valid ruleObj', () => {
          const ruleObj = validator._createRuleObject('minions:banana:hulala');

          expect(ruleObj).to.deep.equal({
            name: 'minions',
            fullName: 'minions:$1:$2',
            params: ['banana', 'hulala']
          });
        });
      });
    });
  });

  describe('.validate()', () => {
    const rules = {
      name: ['required'],
      officeEmail: ['required', 'email'],
      officeEmailOptional: ['email']
    };

    context('Test with simple rules', () => {
      it('should success', () => {
        const result = satpam.validate(rules, {
          name: 'sendy',
          officeEmail: '<EMAIL>'
        });
        const err = result.messages;

        expect(result.success).to.equal(true);
        expect(err).to.not.have.property('name');
        expect(err).to.not.have.property('officeEmail');
        expect(err).to.not.have.property('officeEmailOptional');
      });

      it('should fail with empty name', () => {
        const result = satpam.validate(rules, {
          name: '',
          officeEmail: '<EMAIL>'
        });
        const err = result.messages;

        expect(result.success).to.equal(false);
        expect(err).to.have.property('name');
        expect(err.name.required).to.equal('Name field is required.');
        expect(err.name).to.have.property('required');
        expect(err).to.not.have.property('officeEmail');
        expect(err).to.not.have.property('officeEmailOptional');
      });

      it('should fail with optional email', () => {
        const result = satpam.validate(rules, {
          name: 'sendy',
          officeEmail: '<EMAIL>',
          officeEmailOptional: 'hehe'
        });
        const err = result.messages;

        expect(result.success).to.equal(false);
        expect(err).to.have.property('officeEmailOptional');
        expect(err.officeEmailOptional).to.have.property('email');
        expect(err).to.not.have.property('name');
        expect(err).to.not.have.property('officeEmail');
      });
    });

    context('Test with custom rules', () => {
      satpam.addCustomValidation('must-be-ironman', function (val) {
        return val === 'ironman';
      });
      satpam.setValidationMessage('must-be-ironman', 'Not ironman D:');

      const rules = {
        name: ['required', 'must-be-ironman'],
        phone: ['numeric']
      };

      it('should success', () => {
        const result = satpam.validate(rules, {
          name: 'ironman',
          phone: 123131
        });
        const err = result.messages;

        expect(result.success).to.equal(true);
        expect(err).to.not.have.property('name');
        expect(err).to.not.have.property('phone');
      });

      it('should fail', () => {
        const result = satpam.validate(rules, {
          name: 'sendy',
          phone: 123131
        });
        const err = result.messages;

        expect(result.success).to.equal(false);
        expect(err).to.have.property('name');
        expect(err.name['must-be-ironman']).to.equal('Not ironman D:');
        expect(err).to.not.have.property('phone');
        expect(err.messageArray.length).to.equal(1);
        expect(err.messageArray[0]).to.equal('Not ironman D:');
      });
    });

    context('Test with rule params', () => {
      it('should fail with custom validation range:0:30', () => {
        satpam.addCustomValidation('range:$1:$2', function (val, ruleObj) {
          return val >= ruleObj.params[0] && val <= ruleObj.params[1];
        });
        satpam.setValidationMessage('range:$1:$2', '<%= propertyName %> must between <%= ruleParams[0] %> and <%= ruleParams[1] %>');

        const obj = { salary: 31 };
        const rules = { salary: ['range:0:30'] };
        const result = satpam.validate(rules, obj);
        const err = result.messages;

        expect(result.success).to.equal(false);
        expect(err.salary['range:$1:$2']).to.equal('Salary must between 0 and 30');
        expect(err.messageArray.length).to.equal(1);
      });
    });

    context('Test with multiple errors', () => {
      it('should have multiple errors', () => {
        satpam.addCustomValidation('must-equal:$1', function (val, ruleObj) {
          return val === ruleObj.params[0];
        });
        satpam.setValidationMessage('must-equal:$1', '<%= propertyName %> must equal <%= ruleParams[0] %> !!');

        const obj = {
          officeEmail: '<EMAIL>'
        };

        const rules = {
          name: ['must-equal:sendyhalim'],
          officeEmail: ['email'],
          address: ['must-equal:dimana aja bole', 'required'],
          title: ['required']
        };
        const result = satpam.validate(rules, obj);
        const err = result.messages;

        expect(result.success).to.equal(false);
        expect(err.messageArray.length).to.equal(4);
        expect(err.name['must-equal:$1']).to.equal('Name must equal sendyhalim !!');
        expect(err.address['must-equal:$1']).to.equal('Address must equal dimana aja bole !!');
        expect(err.address['required']).to.equal('Address field is required.');
        expect(err.title['required']).to.equal('Title field is required.');
      });
    });

    context('.getValidationMessage()', () => {
      context('when instance validation message is customized', () => {
        const validator = satpam.create();

        before('set validation message', () => {
          validator.setValidationMessage('required', 'bulbazaurz');
        });

        it('should return correct validation message', () => {
          const ruleObj = {
            fullName: 'required'
          };

          const message = validator.getValidationMessage(ruleObj, 'name', 'wut');
          expect(message).to.equal('bulbazaurz');
        });

        it('should not affect global validation message', () => {
          const rules = {fullName: ['required']};
          const result = satpam.validate(rules, {});

          expect(result.messages.fullName.required).to.equal('Full Name field is required.');
        });
      });

      context('when global validation message is customized', () => {
        const validator = satpam.create();

        before('set global validation message', () => {
          satpam.setValidationMessage('required', 'huuu!');
        });

        it('should return correct validation message', () => {
          const ruleObj = {fullName: ['required']};
          const result = satpam.validate(ruleObj, {});

          expect(result.messages.fullName.required).to.equal('huuu!');
        });

        it('should affect the newly created validator instance', () => {
          const newValidator = satpam.create();
          const ruleObj = {fullName: 'required'};

          const message = newValidator.getValidationMessage(ruleObj, 'fullName', 'wut');
          expect(message).to.equal('huuu!');
        });

        it('should not affect the old validator insance', () => {
          const rules = {fullName: ['required']};<commit_msg>Add test when setting validation message with a function<commit_after>import R from 'ramda';
import { expect } from 'chai';
import satpam from '../lib';

describe('Validator', () => {
  describe('.createRuleObject', () => {
    const validator = satpam.create();

    context('when the given rule is an object', () => {
      context('and it has no rule parameters', () => {
        it('should create valid ruleObj', () => {
          const ruleObj = validator._createRuleObject({
            name: 'wut'
          });

          expect(ruleObj).to.deep.equal({
            name: 'wut',
            fullName: 'wut',
            params: []
          });
        });
      });

      context('and it has one rule parameter', () => {
        it('should create valid ruleObj', () => {
          const ruleObj = validator._createRuleObject({
            name: 'this',
            params: ['is smelll']
          });

          expect(ruleObj).to.deep.equal({
            name: 'this',
            fullName: 'this:$1',
            params: ['is smelll']
          });
        });
      });

      context('and it has two rule parameters', () => {
        it('should create valid ruleObj', () => {
          const ruleObj = validator._createRuleObject({
            name: 'woosah',
            params: ['rocky', 'balboa']
          });

          expect(ruleObj).to.deep.equal({
            name: 'woosah',
            fullName: 'woosah:$1:$2',
            params: ['rocky', 'balboa']
          });
        });
      });
    });

    context('when the given rule is a string', () => {
      context('and it has no rule parameters', () => {
        it('should create valid ruleObj', () => {
          const ruleObj = validator._createRuleObject('minions');

          expect(ruleObj).to.deep.equal({
            name: 'minions',
            fullName: 'minions',
            params: []
          });
        });
      });

      context('and it has one rule parameter', () => {
        it('should create valid ruleObj', () => {
          const ruleObj = validator._createRuleObject('minions:banana');

          expect(ruleObj).to.deep.equal({
            name: 'minions',
            fullName: 'minions:$1',
            params: ['banana']
          });
        });
      });

      context('and it has two rule parameters', () => {
        it('should create valid ruleObj', () => {
          const ruleObj = validator._createRuleObject('minions:banana:hulala');

          expect(ruleObj).to.deep.equal({
            name: 'minions',
            fullName: 'minions:$1:$2',
            params: ['banana', 'hulala']
          });
        });
      });
    });
  });

  describe('.validate()', () => {
    const rules = {
      name: ['required'],
      officeEmail: ['required', 'email'],
      officeEmailOptional: ['email']
    };

    context('Test with simple rules', () => {
      it('should success', () => {
        const result = satpam.validate(rules, {
          name: 'sendy',
          officeEmail: '<EMAIL>'
        });
        const err = result.messages;

        expect(result.success).to.equal(true);
        expect(err).to.not.have.property('name');
        expect(err).to.not.have.property('officeEmail');
        expect(err).to.not.have.property('officeEmailOptional');
      });

      it('should fail with empty name', () => {
        const result = satpam.validate(rules, {
          name: '',
          officeEmail: '<EMAIL>'
        });
        const err = result.messages;

        expect(result.success).to.equal(false);
        expect(err).to.have.property('name');
        expect(err.name.required).to.equal('Name field is required.');
        expect(err.name).to.have.property('required');
        expect(err).to.not.have.property('officeEmail');
        expect(err).to.not.have.property('officeEmailOptional');
      });

      it('should fail with optional email', () => {
        const result = satpam.validate(rules, {
          name: 'sendy',
          officeEmail: '<EMAIL>',
          officeEmailOptional: 'hehe'
        });
        const err = result.messages;

        expect(result.success).to.equal(false);
        expect(err).to.have.property('officeEmailOptional');
        expect(err.officeEmailOptional).to.have.property('email');
        expect(err).to.not.have.property('name');
        expect(err).to.not.have.property('officeEmail');
      });
    });

    context('Test with custom rules', () => {
      satpam.addCustomValidation('must-be-ironman', function (val) {
        return val === 'ironman';
      });
      satpam.setValidationMessage('must-be-ironman', 'Not ironman D:');

      const rules = {
        name: ['required', 'must-be-ironman'],
        phone: ['numeric']
      };

      it('should success', () => {
        const result = satpam.validate(rules, {
          name: 'ironman',
          phone: 123131
        });
        const err = result.messages;

        expect(result.success).to.equal(true);
        expect(err).to.not.have.property('name');
        expect(err).to.not.have.property('phone');
      });

      it('should fail', () => {
        const result = satpam.validate(rules, {
          name: 'sendy',
          phone: 123131
        });
        const err = result.messages;

        expect(result.success).to.equal(false);
        expect(err).to.have.property('name');
        expect(err.name['must-be-ironman']).to.equal('Not ironman D:');
        expect(err).to.not.have.property('phone');
        expect(err.messageArray.length).to.equal(1);
        expect(err.messageArray[0]).to.equal('Not ironman D:');
      });
    });

    context('Test with rule params', () => {
      it('should fail with custom validation range:0:30', () => {
        satpam.addCustomValidation('range:$1:$2', function (val, ruleObj) {
          return val >= ruleObj.params[0] && val <= ruleObj.params[1];
        });
        satpam.setValidationMessage('range:$1:$2', '<%= propertyName %> must between <%= ruleParams[0] %> and <%= ruleParams[1] %>');

        const obj = { salary: 31 };
        const rules = { salary: ['range:0:30'] };
        const result = satpam.validate(rules, obj);
        const err = result.messages;

        expect(result.success).to.equal(false);
        expect(err.salary['range:$1:$2']).to.equal('Salary must between 0 and 30');
        expect(err.messageArray.length).to.equal(1);
      });
    });

    context('Test with multiple errors', () => {
      it('should have multiple errors', () => {
        satpam.addCustomValidation('must-equal:$1', function (val, ruleObj) {
          return val === ruleObj.params[0];
        });
        satpam.setValidationMessage('must-equal:$1', '<%= propertyName %> must equal <%= ruleParams[0] %> !!');

        const obj = {
          officeEmail: '<EMAIL>'
        };

        const rules = {
          name: ['must-equal:sendyhalim'],
          officeEmail: ['email'],
          address: ['must-equal:dimana aja bole', 'required'],
          title: ['required']
        };
        const result = satpam.validate(rules, obj);
        const err = result.messages;

        expect(result.success).to.equal(false);
        expect(err.messageArray.length).to.equal(4);
        expect(err.name['must-equal:$1']).to.equal('Name must equal sendyhalim !!');
        expect(err.address['must-equal:$1']).to.equal('Address must equal dimana aja bole !!');
        expect(err.address['required']).to.equal('Address field is required.');
        expect(err.title['required']).to.equal('Title field is required.');
      });
    });

    context('.getValidationMessage()', () => {
      context('when instance validation message is customized', () => {
        context('and the validation messsage is a string literal', () => {
          const validator = satpam.create();

          before('set validation message', () => {
            validator.setValidationMessage('required', 'bulbazaurz');
          });

          it('should return correct validation message', () => {
            const ruleObj = {
              fullName: 'required'
            };

            const message = validator.getValidationMessage(ruleObj, 'name', 'wut');
            expect(message).to.equal('bulbazaurz');
          });

          it('should not affect global validation message', () => {
            const rules = {fullName: ['required']};
            const result = satpam.validate(rules, {});

            expect(result.messages.fullName.required).to.equal('Full Name field is required.');
          });
        });

        context('and the validation messsage is a function literal', () => {
          const validator = satpam.create();

          before('set validation message', () => {
            validator.setValidationMessage('required', R.always('it is required!'));
          });

          it('should return correct validation message', () => {
            const ruleObj = {
              fullName: 'required'
            };

            const message = validator.getValidationMessage(ruleObj, 'name', 'wut');

            expect(message).to.equal('it is required!');
          });

          it('should not affect global validation message', () => {
            const rules = {fullName: ['required']};
            const result = satpam.validate(rules, {});

            expect(result.messages.fullName.required).to.equal('Full Name field is required.');
          });
        });
      });

      context('when global validation message is customized', () => {
        const validator = satpam.create();

        before('set global validation message', () => {
          satpam.setValidationMessage('required', 'huuu!');
        });

        it('should return correct validation message', () => {
          const ruleObj = {fullName: ['required']};
          const result = satpam.validate(ruleObj, {});

          expect(result.messages.fullName.required).to.equal('huuu!');
        });

        it('should affect the newly created validator instance', () => {
          const newValidator = satpam.create();
          const ruleObj = {fullName: 'required'};

          const message = newValidator.getValidationMessage(ruleObj, 'fullName', 'wut');
          expect(message).to.equal('huuu!');
        });

        it('should not affect the old validator insance', () => {
          const rules = {fullName: ['required']};
{'fraction_non_alphanumeric': 0.16791520154920248, 'fraction_numerical': 0.004994139530143199, 'mean_word_length': 2.1723246039443906, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 104, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5864186', 'n_tokens_mistral': 5597, 'n_tokens_neox': 5394, 'n_words': 1098}