Skip to content

Commit 69f3d93

Browse files
Eran MachielsEranNL
Eran Machiels
andauthored
1.0.0 (#12)
* Feature/setup (#1) * Added typescript & removed start files * Added, fixed and tests most of the code * Fixes * removed redundand extra if check * Added tests to have a higher coverage * Added eslint * Re-added babel config * Removed unused validate method * Fixed default destructing value never used * fixed testarea not validatable Co-authored-by: Eran Machiels <dev@eranmachiels.nl> * Feature/min max rule (#2) * Changed messages to use area name * Fixed area test messages * Added min rule * Changed min value argument name * Added max rule Co-authored-by: Eran Machiels <dev@eranmachiels.nl> * Feature/rewrite rules (#3) * Added basic localization and fixed tests * Fixed eslint for tests * Added docs, cleanup * Fixed typo * Fixed typo * Added root imports, reorganized file structure Co-authored-by: Eran Machiels <dev@eranmachiels.nl> * Fixed including and ignoring files incorrectly in eslint and tsconfig (#4) Co-authored-by: Eran Machiels <dev@eranmachiels.nl> * Added travis config * Added codecov (#7) Co-authored-by: Eran Machiels <dev@eranmachiels.nl> * Feature/access other areas (#6) * Added access to validator, area and provider (when used) within validation rules * Added missing doc * Minor fixes * Unneeded initial value fixes * Re-added missing null type for name Co-authored-by: Eran Machiels <dev@eranmachiels.nl> * WIP validation all elements (#8) * WIP validation all elements * Fixes * Added missing docs Co-authored-by: Eran Machiels <dev@eranmachiels.nl> * Feature/validate all elements (#9) * WIP validation all elements * Fixes * Added missing docs * Added some dom tests * Fixes Co-authored-by: Eran Machiels <dev@eranmachiels.nl> * Feature/publishing (#10) * Added readme * Added everything to be exported Co-authored-by: Eran Machiels <dev@eranmachiels.nl> * Feature/publishing (#11) * Added readme * Added everything to be exported * Added idea workspace map * Removed idea workspace file * 1.0.0 Co-authored-by: Eran Machiels <dev@eranmachiels.nl> Co-authored-by: Eran Machiels <dev@eranmachiels.nl>
1 parent dc4ab03 commit 69f3d93

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+18041
-11210
lines changed

.eslintrc

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"parser": "@typescript-eslint/parser",
3+
"plugins": ["@typescript-eslint/eslint-plugin"],
4+
"extends": [
5+
"eslint:recommended",
6+
"plugin:@typescript-eslint/recommended",
7+
"plugin:react/recommended"
8+
],
9+
"parserOptions": {
10+
"ecmaVersion": 6,
11+
"sourceType": "module",
12+
"project": "./tsconfig.json"
13+
},
14+
"env": {
15+
"node": true,
16+
"es6": true
17+
},
18+
"ignorePatterns": ["dist", "setupTests.ts", "babel.config.js"],
19+
"rules": {
20+
"comma-dangle": "off",
21+
"class-methods-use-this": "off",
22+
"import/prefer-default-export": "off",
23+
"import/no-dynamic-require": "off",
24+
"global-require": "off",
25+
"quotes": ["error", "single", { "allowTemplateLiterals": true }],
26+
"@typescript-eslint/indent": ["error", 4]
27+
}
28+
}

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
# production
1212
/build
13+
/dist
1314

1415
# misc
1516
.DS_Store
@@ -21,3 +22,6 @@
2122
npm-debug.log*
2223
yarn-debug.log*
2324
yarn-error.log*
25+
26+
# IDE
27+
/.idea

.idea/workspace.xml

-49
This file was deleted.

.travis.yml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
language: node_js
2+
3+
node_js:
4+
- "12"
5+
6+
script:
7+
- npm install codecov -g
8+
- npm run build
9+
after_success:
10+
- bash <(curl -s https://codecov.io/bash)

README.md

+173-68
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!

0 commit comments

Comments
 (0)