Skip to content

Commit

Permalink
Fix #15 and upgrade dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
João Faustino committed Jul 28, 2020
1 parent 9585ad5 commit 3d45e0c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
2 changes: 1 addition & 1 deletion dist/Validator.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Validator",
"version": "1.1.0",
"version": "1.1.1",
"description": "A JavaScript validation package, based on Laravel validation.",
"main": "dist/Validator.js",
"scripts": {
Expand Down
4 changes: 4 additions & 0 deletions src/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ class Validator {
this.errors = {};
this.failedRules = {};

if (this.isEmptyObject(this.data)) {
return true;
}

this.rules.forEach(function (item) {
let name = item.name;

Expand Down
30 changes: 18 additions & 12 deletions test/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@ describe('Validator', () => {
expect(v.fails()).to.be.false;
expect(v.invalid()).to.be.empty;
});

it('should not fail if data is defined as an empty object', () => {
let v = Validator.make({}, { date: 'date' });

expect(v.fails()).to.be.false;
});
});
describe('#getErrors()', () => {
const rules = {
Expand Down Expand Up @@ -334,7 +340,7 @@ describe('Validator', () => {

describe('#validateRequired()', () => {
it('return false when the required field is not present', () => {
const v = Validator.make({}, { name: 'required' });
const v = Validator.make({ field: 'data' }, { name: 'required' });
expect(v.passes()).to.be.false;
});

Expand Down Expand Up @@ -827,7 +833,7 @@ describe('Validator', () => {
});

it('returns false when the field is not present', () => {
const v = Validator.make({}, rules);
const v = Validator.make({ field: {} }, rules);
expect(v.passes()).to.be.false;
});

Expand Down Expand Up @@ -1435,7 +1441,7 @@ describe('Validator', () => {
});

it('returns false when the data is empty (the field under validation is not present)', () => {
const v = Validator.make({}, rules);
const v = Validator.make({ field: 'data' }, rules);
expect(v.passes()).to.be.false;
});

Expand All @@ -1453,7 +1459,7 @@ describe('Validator', () => {
f2: 'required_without:f1,f3',
f3: 'required_without:f1,f2',
};
const v = Validator.make({}, rules);
const v = Validator.make({ field: 'data' }, rules);
expect(v.fails()).to.be.true;

v.setData({ f1: 'foo' }, rules);
Expand Down Expand Up @@ -1487,7 +1493,7 @@ describe('Validator', () => {
};

it('returns false when given data is empty', () => {
const v = Validator.make({}, rules);
const v = Validator.make({ field: 'data' }, rules);
expect(v.fails()).to.be.true;
});

Expand Down Expand Up @@ -1608,7 +1614,7 @@ describe('Validator', () => {
});
});

describe('# Error Messages', () => {
describe('#Error Messages', () => {
const rules = {
name: 'required|min:3',
age: 'numeric|min:20',
Expand Down Expand Up @@ -1649,14 +1655,14 @@ describe('Validator', () => {
});
});

describe('# Others', () => {
describe('#Others', () => {
it('tests that empty rules are skipped', () => {
const v = Validator.make({ x: 'asksksks' }, { x: '|||required|' });
expect(v.passes()).to.be.true;
});
});

describe('# Custom Names', () => {
describe('#Custom Names', () => {
const customNames = {
name: 'Name',
age: 'Age',
Expand All @@ -1673,7 +1679,7 @@ describe('Validator', () => {
};

it('tests custom name being applied using constructor', () => {
const v = Validator.make({}, rules, {}, customNames);
const v = Validator.make({ field: 'name' }, rules, {}, customNames);
expect(v.passes()).to.be.false;
expect(v.getErrors()).to.deep.equal(expectedResult);
});
Expand All @@ -1693,7 +1699,7 @@ describe('Validator', () => {
});
});

describe('# Custom Messages', () => {
describe('#Custom Messages', () => {
const rules = {
name: 'required',
age: 'required',
Expand Down Expand Up @@ -1737,7 +1743,7 @@ describe('Validator', () => {
});
});

describe('# Displayable values are replaced', () => {
describe('#Displayable values are replaced', () => {
it('tests required_if:foo,bar', () => {
const v = Validator.make(
{ color: '1', bar: '' },
Expand Down Expand Up @@ -1785,7 +1791,7 @@ describe('Validator', () => {
});
});

describe('# nullable rule tests', () => {
describe('#validateNullable', () => {
it('should passed a empty date with "nullable" rule', () => {
const v = Validator.make(
{
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1382,9 +1382,9 @@ ecc-jsbn@~0.1.1:
safer-buffer "^2.1.0"

electron-to-chromium@^1.3.488:
version "1.3.509"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.509.tgz#830fcb89cd66dc2984d18d794973b99e3f00584c"
integrity sha512-cN4lkjNRuTG8rtAqTOVgwpecEC2kbKA04PG6YijcKGHK/kD0xLjiqExcAOmLUwtXZRF8cBeam2I0VZcih919Ug==
version "1.3.510"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.510.tgz#dee781ff8b595c0deb60172b75d50b6889757eda"
integrity sha512-sLtGB0znXdmo6lM8hy5wTVo+fLqvIuO8hEpgc0DvPmFZqvBu/WB7AarEwhxVKjf3rVbws/rC8Xf+AlsOb36lJQ==

emoji-regex@^7.0.1:
version "7.0.3"
Expand Down

0 comments on commit 3d45e0c

Please sign in to comment.