Skip to content

Commit a2f2c67

Browse files
Eran MachielsEranNL
Eran Machiels
andauthored
Feature/publishing (#10)
* Added readme * Added everything to be exported Co-authored-by: Eran Machiels <[email protected]>
1 parent 7a6bf5e commit a2f2c67

File tree

3 files changed

+182
-68
lines changed

3 files changed

+182
-68
lines changed

README.md

Lines changed: 173 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,173 @@
1-
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
2-
3-
## Available Scripts
4-
5-
In the project directory, you can run:
6-
7-
### `yarn start`
8-
9-
Runs the app in the development mode.<br />
10-
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
11-
12-
The page will reload if you make edits.<br />
13-
You will also see any lint errors in the console.
14-
15-
### `yarn test`
16-
17-
Launches the test runner in the interactive watch mode.<br />
18-
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
19-
20-
### `yarn build`
21-
22-
Builds the app for production to the `build` folder.<br />
23-
It correctly bundles React in production mode and optimizes the build for the best performance.
24-
25-
The build is minified and the filenames include the hashes.<br />
26-
Your app is ready to be deployed!
27-
28-
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
29-
30-
### `yarn eject`
31-
32-
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
33-
34-
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
35-
36-
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
37-
38-
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
39-
40-
## Learn More
41-
42-
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
43-
44-
To learn React, check out the [React documentation](https://reactjs.org/).
45-
46-
### Code Splitting
47-
48-
This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting
49-
50-
### Analyzing the Bundle Size
51-
52-
This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size
53-
54-
### Making a Progressive Web App
55-
56-
This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app
57-
58-
### Advanced Configuration
59-
60-
This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration
61-
62-
### Deployment
63-
64-
This section has moved here: https://facebook.github.io/create-react-app/docs/deployment
65-
66-
### `yarn build` fails to minify
67-
68-
This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify
1+
## Coderan: Validator
2+
The smart React element validator
3+
4+
### Introduction
5+
The goal of this package, is to simplify the struggle of validating elements in React, with a simple system which allows
6+
users to add their own rules.
7+
The system communicates directly with the elements in the DOM, and is therefore widely compatible with other libraries,
8+
like [Bootstrap](https://react-bootstrap.github.io/).
9+
10+
### The concept
11+
Validator consists of two main elements, an `Area` and a `Provider`. Areas are a sort of wrappers having elements that
12+
need validation as their children. An area scans the underlying components and elements and indexes validatable elements.
13+
14+
Providers on the other hand are wrappers around areas, and allow them to communicate between each other. This communication
15+
is needed in order to match with values in other areas. It can also be used to validate all areas at once, and preventing
16+
actions to happen while not all areas are valid.
17+
18+
### How to use
19+
First, start with adding rules to the validator in order to use them. There are some rules pre-made, but more specific
20+
rules you have to create yourself.
21+
22+
```javascript
23+
import { Validator } from '@coderan/validator';
24+
import { min } from '@coderan/rules/min';
25+
26+
Validator.extend('min', min);
27+
```
28+
29+
#### Area
30+
Basic usage:
31+
```jsx
32+
import { ValidatorArea } from '@coderan/validator';
33+
34+
<ValidatorArea rules="required">
35+
<input name="username" />
36+
</ValidatorArea>
37+
```
38+
When the input is focused and blurred, the `required` rule is called.
39+
40+
Every area needs a name. This name is used to index areas in the provider, and make meaningful error messages. When using
41+
multiple inputs within an area, i.e. when validating a multi-input date of birth, `name` prop is required when defining
42+
the `ValidatorArea` component. Like so:
43+
44+
```jsx
45+
import { ValidatorArea } from '@coderan/validator';
46+
47+
<ValidatorArea rules="min" name="dob">
48+
<input name="day" />
49+
<input name="month" />
50+
<input name="year" />
51+
</ValidatorArea>
52+
```
53+
54+
Showing errors:
55+
```jsx
56+
import { ValidatorArea } from '@coderan/validator';
57+
58+
<ValidatorArea rules="min" name="dob">
59+
{({ errors }) => (
60+
<>
61+
<input name="username" />
62+
{ errors.length && <span>{errors[0]}</span> }
63+
</>
64+
)}
65+
</ValidatorArea>
66+
```
67+
68+
#### Provider
69+
Basic usage:
70+
```jsx
71+
import { ValidatorProvider, ValidatorArea } from '@coderan/validator';
72+
73+
<ValidatorProvider>
74+
{({ validate }) => (
75+
<>
76+
<ValidatorArea rules="min" name="dob">
77+
<input name="day" />
78+
<input name="month" />
79+
<input name="year" />
80+
</ValidatorArea>
81+
<ValidatorArea rules="min" name="dob">
82+
<input name="day" />
83+
<input name="month" />
84+
<input name="year" />
85+
</ValidatorArea>
86+
<button
87+
onClick={() => validate(() => alert('valid'))}>Check</button>
88+
</>
89+
)}
90+
</ValidatorProvider>
91+
```
92+
93+
It is possible to give the validator a `rules` prop as well, whose rules apply to all underlying areas:
94+
95+
```jsx
96+
import { ValidatorProvider, ValidatorArea } from '@coderan/validator';
97+
98+
<ValidatorProvider rules="required">
99+
<ValidatorArea rules="min:5">
100+
{/* on blur, both required and min rules are applied */}
101+
<input name="username" />
102+
</ValidatorArea>
103+
</ValidatorProvider>
104+
```
105+
106+
#### Adding rules
107+
108+
With access to validator
109+
```javascript
110+
import { Validator } from '@coderan/validator'
111+
Validator.extend('test_types', (validator: Validator) => ({
112+
passed(): boolean {
113+
return validator.refs(undefined, HTMLInputElement).length === 1
114+
&& validator.refs('test1', HTMLTextAreaElement).length === 1
115+
&& validator.refs('test1').length === 1
116+
&& validator.refs('test1', HTMLProgressElement).length === 0;
117+
},
118+
message(): string {
119+
return 'test';
120+
}
121+
}));
122+
```
123+
124+
or without
125+
```javascript
126+
import { getValue, isInputElement, isSelectElement } from '@/utils/dom';
127+
128+
export default {
129+
passed(elements: HTMLElement[]): boolean {
130+
return elements.every((element: HTMLElement) => {
131+
if (isInputElement(element) || isSelectElement(element)) {
132+
const value = getValue(element);
133+
134+
return value && value.length;
135+
}
136+
137+
return true;
138+
})
139+
},
140+
141+
message(): string {
142+
return `{name} is required`;
143+
}
144+
};
145+
```
146+
147+
You can create your own rules, as long as it follows this interface:
148+
```typescript
149+
import { Validator } from '@coderan/validator';
150+
151+
/**
152+
* Function to access validator using the rule
153+
*/
154+
declare type RuleFunction = (validator: Validator) => RuleObject;
155+
156+
/**
157+
* Object structure rules must implement
158+
*/
159+
declare type RuleObject = {
160+
/**
161+
* Returns whether the rule passed with the given element(s)
162+
*/
163+
passed(elements: HTMLElement[], ...args: string[]): boolean;
164+
/**
165+
* Message shown when the rule doesn't pass
166+
*/
167+
message(): string;
168+
}
169+
170+
export type Rule = RuleObject | RuleFunction;
171+
```
172+
173+
### Happy Coding!

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,16 @@
1111
"react-dom": "^16.13.1",
1212
"react-is": "^16.13.1"
1313
},
14+
"types": "dist/index.d.ts",
1415
"scripts": {
1516
"build": "tsc -p tsconfig-build.json",
1617
"test": "jest --config=jest.config.json",
1718
"test-coverage": "jest --config=jest.config.json --collectCoverage"
1819
},
20+
"repository": {
21+
"type": "git",
22+
"url": "https://github.com/machielsdev/validator"
23+
},
1924
"eslintConfig": {
2025
"extends": "react-app"
2126
},

src/rules/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export { default as min } from '@/rules/min';
2+
export { default as max } from '@/rules/max';
3+
export { default as required } from '@/rules/required';
4+
export { IncorrectArgumentTypeError } from '@/rules/IncorrectArgumentTypeError';

0 commit comments

Comments
 (0)