Skip to content
This repository was archived by the owner on Feb 29, 2024. It is now read-only.

Commit c844e61

Browse files
author
Dominik Sumer
committed
Merge remote-tracking branch 'origin/master'
2 parents b34fb3f + 1545ca3 commit c844e61

File tree

4 files changed

+74
-5
lines changed

4 files changed

+74
-5
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ module.exports = {
2727
'dot-location': [ERROR, 'property'],
2828
'dot-notation': ERROR,
2929
'eqeqeq': [ERROR, 'allow-null'],
30-
'indent': [ERROR, 4],
30+
'indent': [ERROR, 4, {"SwitchCase": 1}],
3131
'jsx-quotes': [ERROR, 'prefer-double'],
3232
'keyword-spacing': [ERROR, {after: true, before: true}],
3333
'no-bitwise': OFF,

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1-
# [@cat-react](https://github.com/cat-react) / form ![Build Status](https://travis-ci.org/cat-react/form.svg?branch=master) [![codecov](https://codecov.io/gh/cat-react/form/branch/master/graph/badge.svg)](https://codecov.io/gh/cat-react/form)
1+
<p align="center">
2+
<img src="https://user-images.githubusercontent.com/4418879/30520764-b86d5cc6-9bb4-11e7-9313-ced40eb4e066.png" alt="cat-react" />
3+
</p>
4+
5+
# [@cat-react](https://github.com/cat-react) / form ![Build Status](https://travis-ci.org/cat-react/form.svg?branch=master) [![codecov](https://codecov.io/gh/cat-react/form/branch/master/graph/badge.svg)](https://codecov.io/gh/cat-react/form) [![npm version](https://badge.fury.io/js/%40cat-react%2Fform.svg)](https://badge.fury.io/js/%40cat-react%2Fform)
26
A simple yet powerful library which helps creating validated forms in react. This project is inspired by [formsy-react](https://github.com/christianalfoni/formsy-react).
37

8+
## Installation
9+
[![npm package](https://nodei.co/npm/@cat-react/form.png?compact=true)](https://www.npmjs.com/package/@cat-react/form)
10+
- Install the dependency `@cat-react/form` <br/>
11+
(e.g. with `yarn add @cat-react/form` or `npm install @cat-react/form --save`)
12+
- Import the Components with `import {Form, Input} from '@cat-react/form';`
13+
414
## Getting Started
515
Are you looking for a simple way to create validated forms with React?
616

@@ -91,8 +101,6 @@ export default class BasicInput extends React.Component {
91101
}
92102
```
93103

94-
## Installation
95-
96104
## Contribution
97105
The project requires at least the latest stable version of node and npm. You also need to have yarn installed globally.
98106

src/validationRules.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ const rules = {
2828
}
2929
}
3030
return allValid;
31+
},
32+
isNumber: (values, value) => {
33+
switch (typeof value) {
34+
case 'string':
35+
return rules.matchRegexp(values, value, /^-?\d+\.?\d*$/);
36+
case 'number':
37+
return true;
38+
default:
39+
return false;
40+
}
3141
}
3242
};
3343
// TODO: add more basic rules

tests/validationRules.spec.js

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,56 @@ describe('Validation Rules', () => {
104104
let valid = rules.minLength(null, 'abcde', 6);
105105
expect(valid).toBe(false);
106106
});
107-
})
107+
});
108+
109+
describe('number', () => {
110+
it('should validate a whole number strings successfully', () => {
111+
let valid = rules.isNumber(null, '10');
112+
expect(valid).toBe(true);
113+
});
114+
it('should validate a negative number strings successfully', () => {
115+
let valid = rules.isNumber(null, '-50');
116+
expect(valid).toBe(true);
117+
});
118+
it('should validate a decimal number strings successfully', () => {
119+
let valid = rules.isNumber(null, '10.50');
120+
expect(valid).toBe(true);
121+
});
122+
it('should invalidate the invalid number strings', () => {
123+
let valid = rules.isNumber(null, '1A0');
124+
expect(valid).toBe(false);
125+
valid = rules.isNumber(null, '');
126+
expect(valid).toBe(false);
127+
valid = rules.isNumber(null, 'abcde');
128+
expect(valid).toBe(false);
129+
valid = rules.isNumber(null, '10.a1');
130+
expect(valid).toBe(false);
131+
});
132+
it('should validate a whole number successfully', () => {
133+
let valid = rules.isNumber(null, 10);
134+
expect(valid).toBe(true);
135+
});
136+
it('should validate a negative number successfully', () => {
137+
let valid = rules.isNumber(null, -50);
138+
expect(valid).toBe(true);
139+
});
140+
it('should validate a decimal number successfully', () => {
141+
let valid = rules.isNumber(null, 10.50);
142+
expect(valid).toBe(true);
143+
});
144+
it('should invalidate the invalid numbers', () => {
145+
let valid = rules.isNumber(null, {});
146+
expect(valid).toBe(false);
147+
valid = rules.isNumber(null, '');
148+
expect(valid).toBe(false);
149+
valid = rules.isNumber(null, null);
150+
expect(valid).toBe(false);
151+
valid = rules.isNumber(null, undefined);
152+
expect(valid).toBe(false);
153+
valid = rules.isNumber(null, true);
154+
expect(valid).toBe(false);
155+
valid = rules.isNumber(null, () => {});
156+
expect(valid).toBe(false);
157+
});
158+
});
108159
});

0 commit comments

Comments
 (0)