Skip to content

Commit

Permalink
add max_length
Browse files Browse the repository at this point in the history
  • Loading branch information
babacarcissedia committed Apr 5, 2020
1 parent b8b4d6b commit fec46c4
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 3 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
test/
.github/
src/
Expand Down
21 changes: 18 additions & 3 deletions src/Validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class Validator {
const messages = Object.assign({}, {
required: ':field is required.',
min_length: ':field length need to be at least :length: character(s) long.',
max_length: ':field length need to be at most :length: character(s) long.',
email: ':value is not a valid email.',
confirmed: ':field does not match confirmation field.',
min: ':field length must be greater than or equal :min',
Expand Down Expand Up @@ -98,6 +99,9 @@ export class Validator {
case 'min_length':
this.min_length.apply(this, params)
break;
case 'max_length':
this.max_length.apply(this, params)
break;
case 'email':
this.email.apply(this, params)
break;
Expand Down Expand Up @@ -168,7 +172,6 @@ export class Validator {
return true
}


/**
* @param {string} field
* @param {string} l
Expand All @@ -183,6 +186,20 @@ export class Validator {
return true
}

/**
* @param {string} field
* @param {string} l
* @returns boolean
*/
max_length (field: string, l: string): boolean {
const length = Number(l)
if (this.data.has(field) && String(this.data.get(field)).length > length) {
this.addError(field, this.getErrorFor('max_length', field, {length}))
return false
}
return true
}

/**
*
* @param {string} field
Expand Down Expand Up @@ -270,7 +287,6 @@ export class Validator {
return true
}


/**
* @param {string} field
* @param {string} model
Expand All @@ -287,7 +303,6 @@ export class Validator {
return exists
}


/**
* @param {string} field
* @param {string} model
Expand Down
73 changes: 73 additions & 0 deletions test/email.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import Validator from "../src/Validator";

describe('Validator::email', () => {
const rules = {
name: 'email'
}

it('should not fail when field is not present', async (done) => {

const v = await Validator.make({
data: {
},
rules
})
expect(v.fails()).toBe(false)
expect(v.getErrors()).not.toHaveProperty('name')
done()
})

it('should fail for not email values', async (done) => {

for (const input of ['', 'domain.com', 'some[at]domain.com', 'some@domain', 'some@domain.']) {
const v = await Validator.make({
data: {
name: input
},
rules
})
expect(v.fails()).toBe(true)
expect(v.getErrors()).toHaveProperty('name')
}
done()
})

it('should success for email values', async (done) => {

const prefixes = ['john', 'john123', 'john_doe', 'john_doe_123']
const domains = ['.co', '.com', '.fr', '.sn', '.net', '.org', '.edu']
const inputs: string[] = []
for (const prefix of prefixes) {
for (const domain of domains) {
inputs.push(`${prefix}@domain${domain}`)
}
}
for (const input of inputs) {
const v = await Validator.make({
data: {
name: input
},
rules
})
expect(v.fails()).toBe(false)
expect(v.getErrors()).not.toHaveProperty('name')
}
done()
})

it('should success for sub domain emails', async (done) => {

const inputs = ['[email protected]', '[email protected]']
for (const input of inputs) {
const v = await Validator.make({
data: {
name: input
},
rules
})
expect(v.fails()).toBe(false)
expect(v.getErrors()).not.toHaveProperty('name')
}
done()
})
})
7 changes: 7 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Validator from "../src/Validator";

describe('Validator Core', () => {
describe('::after hook', () => {

})
})

0 comments on commit fec46c4

Please sign in to comment.