This commit is contained in:
Semigradsky 2015-04-22 19:06:53 +03:00
parent f64d2443b9
commit b18b4e8c79
15 changed files with 103 additions and 94 deletions

12
.editorconfig Normal file
View File

@ -0,0 +1,12 @@
root = true
[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.md]
trim_trailing_whitespace = false

View File

@ -8,10 +8,8 @@ var uglify = require('gulp-uglify');
var streamify = require('gulp-streamify');
var notify = require('gulp-notify');
var gutil = require('gulp-util');
var shell = require('gulp-shell');
var livereload = require('gulp-livereload');
var glob = require('glob');
var jasminePhantomJs = require('gulp-jasmine2-phantomjs');
var fs = require('fs');
var dependencies = ['react'];
@ -63,7 +61,7 @@ var browserifyTask = function (options) {
// We create a separate bundle for our dependencies as they
// should not rebundle on file changes. This only happens when
// we develop. When deploying the dependencies will be included
// we develop. When deploying the dependencies will be included
// in the application bundle
if (options.development) {

View File

@ -8,7 +8,7 @@
},
"main": "src/main.js",
"scripts": {
"test": "./node_modules/.bin/jasmine-node ./specs",
"test": "echo \"Error: no test specified\" && exit 1",
"examples": "webpack-dev-server --config examples/webpack.config.js --content-base examples"
},
"author": "Christian Alfoni",
@ -25,15 +25,12 @@
"glob": "^4.0.6",
"gulp": "^3.8.9",
"gulp-if": "^1.2.4",
"gulp-jasmine2-phantomjs": "^0.1.1",
"gulp-livereload": "^3.4.0",
"gulp-notify": "^1.4.2",
"gulp-shell": "^0.2.10",
"gulp-streamify": "0.0.5",
"gulp-uglify": "^0.3.1",
"gulp-util": "^3.0.0",
"jsx-loader": "^0.12.2",
"phantomjs": "^1.9.12",
"reactify": "^1.1.0",
"vinyl-source-stream": "^0.1.1",
"watchify": "^2.1.1",

View File

@ -425,6 +425,7 @@ var convertValidationsToObject = function (validations) {
return validations.split(/\,(?![^{\[]*[}\]])/g).reduce(function (validations, validation) {
var args = validation.split(':');
var validateMethod = args.shift();
args = args.map(function (arg) {
try {
return JSON.parse(arg);
@ -432,10 +433,11 @@ var convertValidationsToObject = function (validations) {
return arg; // It is a string if it can not parse it
}
});
if (args.length > 1) {
throw new Error('Formsy does not support multiple args on string validations. Use object format of validations instead.');
}
validations[validateMethod] = args.length ? args[0] : true;
return validations;
}, {});
@ -443,8 +445,8 @@ var convertValidationsToObject = function (validations) {
}
return validations || {};
};
module.exports = {
getInitialState: function () {
return {
@ -463,8 +465,8 @@ module.exports = {
validationErrors: {}
};
},
componentWillMount: function () {
componentWillMount: function () {
var configure = function () {
this.setValidations(this.props.validations, this.props.required);
this.props._attachToForm(this);
@ -484,7 +486,6 @@ module.exports = {
}.bind(this), 0);
}
configure();
},
// We have to make the validate method is kept when new props are added
@ -495,13 +496,13 @@ module.exports = {
componentDidUpdate: function (prevProps, prevState) {
var isValueChanged = function () {
return this.props.value !== prevProps.value && this.state._value === prevProps.value;
}.bind(this);
// If validations has changed or something outside changes
// If validations has changed or something outside changes
// the value, set the value again running a validation
if (isValueChanged()) {
this.setValue(this.props.value);

View File

@ -3,7 +3,7 @@ var Formsy = require('./../src/main.js');
describe('Element', function() {
it('should return passed and setValue() value when using getValue()', function () {
var TestInput = React.createClass({
mixins: [Formsy.Mixin],
updateValue: function (event) {
@ -27,7 +27,7 @@ describe('Element', function() {
});
it('should set back to pristine value when running reset', function () {
var reset = null;
var TestInput = React.createClass({
mixins: [Formsy.Mixin],
@ -55,7 +55,7 @@ describe('Element', function() {
});
it('should return error message passed when calling getErrorMessage()', function () {
var getErrorMessage = null;
var TestInput = React.createClass({
mixins: [Formsy.Mixin],
@ -80,7 +80,7 @@ describe('Element', function() {
});
it('should return true or false when calling isValid() depending on valid state', function () {
var isValid = null;
var TestInput = React.createClass({
mixins: [Formsy.Mixin],
@ -108,7 +108,7 @@ describe('Element', function() {
});
it('should return true or false when calling isRequired() depending on passed required attribute', function () {
var isRequireds = [];
var TestInput = React.createClass({
mixins: [Formsy.Mixin],
@ -137,7 +137,7 @@ describe('Element', function() {
});
it('should return true or false when calling showRequired() depending on input being empty and required is passed, or not', function () {
var showRequireds = [];
var TestInput = React.createClass({
mixins: [Formsy.Mixin],
@ -166,7 +166,7 @@ describe('Element', function() {
});
it('should return true or false when calling isPristine() depending on input has been "touched" or not', function () {
var isPristine = null;
var TestInput = React.createClass({
mixins: [Formsy.Mixin],
@ -190,7 +190,7 @@ describe('Element', function() {
var input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT');
TestUtils.Simulate.change(input, {target: {value: 'foo'}});
expect(isPristine()).toBe(false);
});
it('should allow an undefined value to be updated to a value', function (done) {
@ -227,7 +227,7 @@ it('should allow an undefined value to be updated to a value', function (done) {
expect(input.getDOMNode().value).toBe('foo');
done();
}, 0);
});
});
it('should be able to test a values validity', function () {
@ -255,7 +255,7 @@ it('should allow an undefined value to be updated to a value', function (done) {
expect(input.isValidValue('foo@bar.com')).toBe(true);
expect(input.isValidValue('foo@bar')).toBe(false);
});
});
it('should be able to use an object as validations property', function () {
@ -282,7 +282,7 @@ it('should allow an undefined value to be updated to a value', function (done) {
var input = TestUtils.findRenderedComponentWithType(form, TestInput);
expect(input.isValidValue('foo@bar.com')).toBe(true);
expect(input.isValidValue('foo@bar')).toBe(false);
expect(input.isValidValue('foo@bar')).toBe(false);
});
it('should be able to pass complex values to a validation rule', function () {
@ -314,7 +314,7 @@ it('should allow an undefined value to be updated to a value', function (done) {
var inputComponent = TestUtils.findRenderedComponentWithType(form, TestInput);
expect(inputComponent.isValid()).toBe(true);
var input = TestUtils.findRenderedDOMComponentWithTag(form, 'INPUT');
TestUtils.Simulate.change(input, {target: {value: 'bar'}});
TestUtils.Simulate.change(input, {target: {value: 'bar'}});
expect(inputComponent.isValid()).toBe(false);
});
@ -357,7 +357,7 @@ it('should allow an undefined value to be updated to a value', function (done) {
expect(inputComponent[0].isValid()).toBe(true);
expect(inputComponent[1].isValid()).toBe(true);
var input = TestUtils.scryRenderedDOMComponentsWithTag(form, 'INPUT');
TestUtils.Simulate.change(input[0], {target: {value: 'bar'}});
TestUtils.Simulate.change(input[0], {target: {value: 'bar'}});
expect(inputComponent[0].isValid()).toBe(false);
expect(inputComponent[1].isValid()).toBe(false);
});
@ -399,12 +399,12 @@ it('should allow an undefined value to be updated to a value', function (done) {
render: function () {
return (
<Formsy.Form>
<TestInput name="A"
<TestInput name="A"
validations={{
isEmail: true
}}
validationError="bar"
validationErrors={{isEmail: 'bar2', isLength: 'bar3'}}
}}
validationError="bar"
validationErrors={{isEmail: 'bar2', isLength: 'bar3'}}
value="f"
required={{
isLength: 1
@ -433,12 +433,12 @@ it('should allow an undefined value to be updated to a value', function (done) {
render: function () {
return (
<Formsy.Form>
<TestInput name="A"
<TestInput name="A"
validations={{
isEmail: true
}}
validationError="bar"
validationErrors={{foo: 'bar'}}
}}
validationError="bar"
validationErrors={{foo: 'bar'}}
value="foo"
/>
</Formsy.Form>
@ -464,7 +464,7 @@ it('should allow an undefined value to be updated to a value', function (done) {
render: function () {
return (
<Formsy.Form>
<TestInput name="A"
<TestInput name="A"
required
/>
</Formsy.Form>

View File

@ -1,12 +1,12 @@
var Formsy = require('./../src/main.js');
describe('Rules: equals', function() {
var TestInput, isValid, form, input;
var TestInput, isValid, form, input;
beforeEach(function() {
isValid = jasmine.createSpy('valid');
TestInput = React.createClass({
isValid = jasmine.createSpy('valid');
TestInput = React.createClass({
mixins: [Formsy.Mixin],
updateValue: function (event) {
this.setValue(event.target.value);
@ -30,7 +30,7 @@ describe('Rules: equals', function() {
});
afterEach(function() {
TestInput = isValid = isInvalid = form = null;
TestInput = isValid = isInvalid = form = null;
});
it('should fail with undefined', function () {
@ -57,4 +57,4 @@ describe('Rules: equals', function() {
expect(isValid).toHaveBeenCalled();
});
});
});

View File

@ -1,12 +1,12 @@
var Formsy = require('./../src/main.js');
describe('Rules: hasValue', function() {
var TestInput, isValid, form, input;
var TestInput, isValid, form, input;
beforeEach(function() {
isValid = jasmine.createSpy('valid');
TestInput = React.createClass({
isValid = jasmine.createSpy('valid');
TestInput = React.createClass({
mixins: [Formsy.Mixin],
updateValue: function (event) {
this.setValue(event.target.value);
@ -30,7 +30,7 @@ describe('Rules: hasValue', function() {
});
afterEach(function() {
TestInput = isValid = isInvalid = form = null;
TestInput = isValid = isInvalid = form = null;
});
it('should fail with undefined', function () {
@ -51,4 +51,4 @@ describe('Rules: hasValue', function() {
expect(isValid).toHaveBeenCalled();
});
});
});

View File

@ -1,12 +1,12 @@
var Formsy = require('./../src/main.js');
describe('Rules: isAlpha', function() {
var TestInput, isValid, form, input;
var TestInput, isValid, form, input;
beforeEach(function() {
isValid = jasmine.createSpy('valid');
TestInput = React.createClass({
isValid = jasmine.createSpy('valid');
TestInput = React.createClass({
mixins: [Formsy.Mixin],
updateValue: function (event) {
this.setValue(event.target.value);
@ -30,7 +30,7 @@ describe('Rules: isAlpha', function() {
});
afterEach(function() {
TestInput = isValid = isInvalid = form = null;
TestInput = isValid = isInvalid = form = null;
});
it('should fail with undefined', function () {
@ -57,4 +57,4 @@ describe('Rules: isAlpha', function() {
expect(isValid).toHaveBeenCalled();
});
});
});

View File

@ -4,9 +4,9 @@ describe('Rules: isEmail', function() {
var TestInput, isValid, form, input;
beforeEach(function() {
isValid = jasmine.createSpy('valid');
TestInput = React.createClass({
isValid = jasmine.createSpy('valid');
TestInput = React.createClass({
mixins: [Formsy.Mixin],
updateValue: function (event) {
this.setValue(event.target.value);
@ -30,7 +30,7 @@ describe('Rules: isEmail', function() {
});
afterEach(function() {
TestInput = isValid = isInvalid = form = null;
TestInput = isValid = isInvalid = form = null;
});
it('should fail with "foo"', function () {
@ -45,4 +45,4 @@ describe('Rules: isEmail', function() {
expect(isValid).toHaveBeenCalled();
});
});
});

View File

@ -1,12 +1,12 @@
var Formsy = require('./../src/main.js');
describe('Rules: isLength', function() {
var TestInput, isValid, form, input;
var TestInput, isValid, form, input;
beforeEach(function() {
isValid = jasmine.createSpy('valid');
TestInput = React.createClass({
isValid = jasmine.createSpy('valid');
TestInput = React.createClass({
mixins: [Formsy.Mixin],
updateValue: function (event) {
this.setValue(event.target.value);
@ -30,7 +30,7 @@ describe('Rules: isLength', function() {
});
afterEach(function() {
TestInput = isValid = isInvalid = form = null;
TestInput = isValid = isInvalid = form = null;
});
it('should fail with undefined', function () {
@ -69,4 +69,4 @@ describe('Rules: isLength', function() {
expect(isValid).toHaveBeenCalled();
});
});
});

View File

@ -1,12 +1,12 @@
var Formsy = require('./../src/main.js');
describe('Rules: isNumeric', function() {
var TestInput, isValid, form, input;
var TestInput, isValid, form, input;
beforeEach(function() {
isValid = jasmine.createSpy('valid');
TestInput = React.createClass({
isValid = jasmine.createSpy('valid');
TestInput = React.createClass({
mixins: [Formsy.Mixin],
updateValue: function (event) {
this.setValue(event.target.value);
@ -30,7 +30,7 @@ describe('Rules: isNumeric', function() {
});
afterEach(function() {
TestInput = isValid = isInvalid = form = null;
TestInput = isValid = isInvalid = form = null;
});
it('should fail with undefined', function () {
@ -69,4 +69,4 @@ describe('Rules: isNumeric', function() {
expect(isValid).toHaveBeenCalled();
});
});
});

View File

@ -1,12 +1,12 @@
var Formsy = require('./../src/main.js');
describe('Rules: isWords', function() {
var TestInput, isValid, form, input;
var TestInput, isValid, form, input;
beforeEach(function() {
isValid = jasmine.createSpy('valid');
TestInput = React.createClass({
isValid = jasmine.createSpy('valid');
TestInput = React.createClass({
mixins: [Formsy.Mixin],
updateValue: function (event) {
this.setValue(event.target.value);
@ -30,7 +30,7 @@ describe('Rules: isWords', function() {
});
afterEach(function() {
TestInput = isValid = isInvalid = form = null;
TestInput = isValid = isInvalid = form = null;
});
it('should fail with undefined', function () {
@ -63,4 +63,4 @@ describe('Rules: isWords', function() {
expect(isValid).toHaveBeenCalled();
});
});
});

View File

@ -1,12 +1,12 @@
var Formsy = require('./../src/main.js');
describe('Rules: maxLength', function() {
var TestInput, isValid, form, input;
var TestInput, isValid, form, input;
beforeEach(function() {
isValid = jasmine.createSpy('valid');
TestInput = React.createClass({
isValid = jasmine.createSpy('valid');
TestInput = React.createClass({
mixins: [Formsy.Mixin],
updateValue: function (event) {
this.setValue(event.target.value);
@ -30,7 +30,7 @@ describe('Rules: maxLength', function() {
});
afterEach(function() {
TestInput = isValid = isInvalid = form = null;
TestInput = isValid = isInvalid = form = null;
});
it('should fail with undefined', function () {
@ -69,4 +69,4 @@ describe('Rules: maxLength', function() {
expect(isValid).not.toHaveBeenCalled();
});
});
});

View File

@ -1,12 +1,12 @@
var Formsy = require('./../src/main.js');
describe('Rules: minLength', function() {
var TestInput, isValid, form, input;
var TestInput, isValid, form, input;
beforeEach(function() {
isValid = jasmine.createSpy('valid');
TestInput = React.createClass({
isValid = jasmine.createSpy('valid');
TestInput = React.createClass({
mixins: [Formsy.Mixin],
updateValue: function (event) {
this.setValue(event.target.value);
@ -30,7 +30,7 @@ describe('Rules: minLength', function() {
});
afterEach(function() {
TestInput = isValid = isInvalid = form = null;
TestInput = isValid = isInvalid = form = null;
});
it('should fail with undefined', function () {
@ -69,4 +69,4 @@ describe('Rules: minLength', function() {
expect(isValid).toHaveBeenCalled();
});
});
});

View File

@ -5,6 +5,7 @@ var convertValidationsToObject = function (validations) {
return validations.split(/\,(?![^{\[]*[}\]])/g).reduce(function (validations, validation) {
var args = validation.split(':');
var validateMethod = args.shift();
args = args.map(function (arg) {
try {
return JSON.parse(arg);
@ -12,10 +13,11 @@ var convertValidationsToObject = function (validations) {
return arg; // It is a string if it can not parse it
}
});
if (args.length > 1) {
throw new Error('Formsy does not support multiple args on string validations. Use object format of validations instead.');
}
validations[validateMethod] = args.length ? args[0] : true;
return validations;
}, {});
@ -23,8 +25,8 @@ var convertValidationsToObject = function (validations) {
}
return validations || {};
};
module.exports = {
getInitialState: function () {
return {
@ -43,8 +45,8 @@ module.exports = {
validationErrors: {}
};
},
componentWillMount: function () {
componentWillMount: function () {
var configure = function () {
this.setValidations(this.props.validations, this.props.required);
this.props._attachToForm(this);
@ -64,7 +66,6 @@ module.exports = {
}.bind(this), 0);
}
configure();
},
// We have to make the validate method is kept when new props are added
@ -75,13 +76,13 @@ module.exports = {
componentDidUpdate: function (prevProps, prevState) {
var isValueChanged = function () {
return this.props.value !== prevProps.value && this.state._value === prevProps.value;
}.bind(this);
// If validations has changed or something outside changes
// If validations has changed or something outside changes
// the value, set the value again running a validation
if (isValueChanged()) {
this.setValue(this.props.value);