Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added "or" condition processing to validation parameters #53

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ node_modules
.coverage/
dist/
lib/
coverage/
39 changes: 37 additions & 2 deletions __tests__/RenderTests.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict';
import Form from '../src/Form';
import {ValidatedInput,Form, Radio, RadioGroup} from '../src';

// Note: THere is an issue with react-bootstrap and mocking. For now the whole node_modules directory has been unmocked.
describe('React bootstrap validation compilation test', () => {
var React = require('react');
var TestUtils = require('react-addons-test-utils');

beforeEach(function() {

});

it('Renders Form component correctly.', () => {
Expand All @@ -19,5 +19,40 @@ describe('React bootstrap validation compilation test', () => {
// Do some work with the validation outcomes
}
} />);
});
it('Renders ValidatedInput component correctly.', () => {
// Render into document
let validSubmit = function(event){};
TestUtils.renderIntoDocument(
<Form onValidSubmit={validSubmit}>
<ValidatedInput
type='text'
label='Email'
name='email'
validate='required,isEmail'
errorHelp={{
required: 'Please enter your email',
isEmail: 'Email is invalid'
}}
/>
</Form>);
});
it('Renders RadioGroup component correctly.', () => {
// Render into document
let validSubmit = function(event){};
TestUtils.renderIntoDocument(
<Form onValidSubmit={validSubmit}>
<RadioGroup name='radio'
value='3'
label='Which one is better?'
validate={v => v === 'cola'}
errorHelp='Pepsi? Seriously?'
labelClassName='col-xs-2'
wrapperClassName='col-xs-10'>
<Radio value='cola' label='Cola' />
<Radio value='pepsi' label='Pepsi' />
</RadioGroup>
</Form>);
});

});
150 changes: 150 additions & 0 deletions __tests__/ValidatedInputTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
'use strict';
import {ValidatedInput,Form} from '../src';

// Note: THere is an issue with react-bootstrap and mocking. For now the whole node_modules directory has been unmocked.
describe('React bootstrap ValidatedInput test', () => {
var React = require('react');
var TestUtils = require('react-addons-test-utils');
beforeEach(function() {
});
it('Validates or conditons.', () => {
// Render into document
var submittedObject = null;
var validSubmit = function(event){
submittedObject = event;
};

let item = TestUtils.renderIntoDocument(
<Form onValidSubmit={validSubmit}>
<ValidatedInput
type='text'
label='Email'
name='email'
className='testInput'
validate='isEmpty|isEmail'
errorHelp={{
isEmail: 'Must be an email'
}}
/>
</Form>);

let input = TestUtils.scryRenderedDOMComponentsWithClass(item, 'testInput')[1];
input.value='';
TestUtils.Simulate.change(input);
TestUtils.Simulate.submit(item.refs.form);
// test for error messages next to the item
expect(submittedObject.email).toBe('');

input.value='[email protected]';
TestUtils.Simulate.change(input);
TestUtils.Simulate.submit(item.refs.form);
// test for error messages next to the item
expect(submittedObject.email).toBe('[email protected]');

});
it('Validates an empty field.', () => {
// Render into document
var submittedObject = null;
var validSubmit = function(event){
submittedObject = event;
};

let item = TestUtils.renderIntoDocument(
<Form onValidSubmit={validSubmit}>
<ValidatedInput
type='text'
label='Email'
name='email'
className='testInput'
validate='isEmpty'
errorHelp={{
isEmail: 'Must be an email'
}}
/>
</Form>);

let input = TestUtils.scryRenderedDOMComponentsWithClass(item, 'testInput')[1];
input.value='aaaa';
TestUtils.Simulate.change(input);
TestUtils.Simulate.submit(item.refs.form);
// test for error messages next to the item
expect(submittedObject).toBe(null);
input.value='';
TestUtils.Simulate.change(input);
TestUtils.Simulate.submit(item.refs.form);
// test for error messages next to the item
expect(submittedObject.email).toBe('');
});

it('Validates a number.', () => {
// Render into document
var submittedObject = null;
var validSubmit = function(event){
submittedObject = event;
};

let item = TestUtils.renderIntoDocument(
<Form onValidSubmit={validSubmit}>
<ValidatedInput
type='text'
label='Number'
name='number'
className='testInput'
validate='required,isInt'
errorHelp={{
required: 'Please enter a number',
isInt: 'Must be a whole number'
}}
/>
</Form>);

let input = TestUtils.scryRenderedDOMComponentsWithClass(item, 'testInput')[1];
input.value='not a number';
TestUtils.Simulate.change(input);
TestUtils.Simulate.submit(item.refs.form);
// test for error messages next to the item
expect(submittedObject).toBe(null);

input.value='23';
TestUtils.Simulate.change(input);
TestUtils.Simulate.submit(item.refs.form);
// test for error messages next to the item
expect(submittedObject.number).toBe('23');
});

it('Validates an email.', () => {
// Render into document
var submittedObject = null;
var validSubmit = function(event){
submittedObject = event;
};

let item = TestUtils.renderIntoDocument(
<Form onValidSubmit={validSubmit}>
<ValidatedInput
type='text'
label='Email'
name='email'
className='testInput'
validate='required,isEmail'
errorHelp={{
required: 'Please enter your email',
isEmail: 'Email is invalid'
}}
/>
</Form>);

let input = TestUtils.scryRenderedDOMComponentsWithClass(item, 'testInput')[1];
input.value='notavlidaemailaddress';
TestUtils.Simulate.change(input);
TestUtils.Simulate.submit(item.refs.form);
// test for error messages next to the item
expect(submittedObject).toBe(null);

input.value='[email protected]';
TestUtils.Simulate.change(input);
TestUtils.Simulate.submit(item.refs.form);
console.log(submittedObject);
expect(submittedObject.email).toBe('[email protected]');
});
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
},
"jest": {
"scriptPreprocessor": "node_modules/babel-jest",
"collectCoverage": true,
"unmockedModulePathPatterns": [
"node_modules",
"src"
Expand Down
21 changes: 17 additions & 4 deletions src/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export default class Form extends InputContainer {

if (typeof this.props.validateOne === 'function') {
result = this.props.validateOne(iptName, value, context, result);
}
}
// if result is !== true, it is considered an error
// it can be either bool or string error
if (result !== true) {
Expand Down Expand Up @@ -248,7 +248,16 @@ export default class Form extends InputContainer {
}

_compileValidationRules(input, ruleProp) {
let rules = ruleProp.split(',').map(rule => {
let deliminator =',';
let andCondition =true;
// set the deliminator
if(ruleProp.indexOf('|')>0){

deliminator='|';
andCondition=false;
}
// Split and groups
let rules = ruleProp.split(deliminator).map(rule => {
let params = rule.split(':');
let name = params.shift();
let inverse = name[0] === '!';
Expand All @@ -257,14 +266,14 @@ export default class Form extends InputContainer {
name = name.substr(1);
}

return { name, inverse, params };
return { name, inverse, params,andCondition:andCondition };
});

let validator = (input.props && input.props.type) === 'file' ? FileValidator : Validator;

return val => {
let result = true;

let previousResult = true;
rules.forEach(rule => {
if (typeof validator[rule.name] !== 'function') {
throw new Error('Invalid input validation rule "' + rule.name + '"');
Expand All @@ -275,6 +284,10 @@ export default class Form extends InputContainer {
if (rule.inverse) {
ruleResult = !ruleResult;
}
if(!rule.andCondition){
ruleResult = ruleResult || previousResult;
}
previousResult = ruleResult;

if (result === true && ruleResult !== true) {
result = getInputErrorMessage(input, rule.name) ||
Expand Down
1 change: 1 addition & 0 deletions src/Validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import validator from 'validator';
*/
validator.extend('required', val => !validator.isNull(val));

validator.extend('isEmpty', val => validator.isNull(val));
/**
* Returns true if the value is boolean true
*
Expand Down