Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
* develop:
  :: Added a pattern regex validation
  • Loading branch information
Christian Fortes committed May 13, 2019
2 parents e5fc8e3 + 424006c commit 075db92
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ valueNotEquals | Define one value that shouldn't be equal | `valueNo
url | Require a valid url | `url: true`
range | Require a given value range | `range: [5, 8]`
equalTo | Requires the field to be the same as another one | `equalTo: #fieldId`
pattern | Requires a value to match a regex pattern | `pattern: ^[0-9]+$`

## Demo
See the validations working on our demo page: https://octaform.github.io/demo
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "octaform-additional",
"alias": "Octaform Additional",
"version": "1.1.0",
"version": "1.1.1",
"description": "Octaform Additional Validation - Presets validation to use with Octaform validate, this package contains validations such as email, extension, minlength, and others",
"main": "index.js",
"homepage": "https://github.com/octaform/octaform#readme",
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import minchecked from './minchecked';
import url from './url';
import range from './range';
import equalTo from './equalTo';
import pattern from './pattern';

module.exports = {
email,
Expand All @@ -28,4 +29,5 @@ module.exports = {
url,
range,
equalTo,
pattern,
};
8 changes: 8 additions & 0 deletions src/pattern.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default {
name: 'pattern',
message: 'Please enter a value following the pattern /{0}/',
paramType: String,
fn: (value, element, param) => {
return new RegExp(param).test(value);
},
};
5 changes: 3 additions & 2 deletions test/unit/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import modules from '../../src';
describe('Index Validation', () => {
test('Test: Should be defined all validations', () => {
const validations = Object.keys(modules);
expect(validations).toHaveLength(14);
expect(validations).toHaveLength(15);
expect(validations).toEqual(
expect.arrayContaining([
'email',
Expand All @@ -19,7 +19,8 @@ describe('Index Validation', () => {
'url',
'valueNotEquals',
'equalTo',
'valueEquals'
'valueEquals',
'pattern'
])
);
});
Expand Down
13 changes: 13 additions & 0 deletions test/unit/pattern.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Pattern from '../../src/pattern';

describe('Pattern Validation', () => {
test('Test: Should pass when typed only numbers', () => {
const isValid = Pattern.fn('250', null, '^[0-9]+$');
expect(isValid).toBe(true);
});

test('Test: Should fail when typed letters and number', () => {
const isValid = Pattern.fn('ws4', null, '^[0-9]+$');
expect(isValid).toBe(false);
});
});

0 comments on commit 075db92

Please sign in to comment.