From 43008e89256f9deda0eedf44ac38be4312f9842d Mon Sep 17 00:00:00 2001 From: Eran Machiels Date: Fri, 2 Oct 2020 15:45:26 +0200 Subject: [PATCH 1/5] Added readme --- README.md | 241 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 173 insertions(+), 68 deletions(-) diff --git a/README.md b/README.md index 9c40dcd..e5156d2 100644 --- a/README.md +++ b/README.md @@ -1,68 +1,173 @@ -This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). - -## Available Scripts - -In the project directory, you can run: - -### `yarn start` - -Runs the app in the development mode.
-Open [http://localhost:3000](http://localhost:3000) to view it in the browser. - -The page will reload if you make edits.
-You will also see any lint errors in the console. - -### `yarn test` - -Launches the test runner in the interactive watch mode.
-See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. - -### `yarn build` - -Builds the app for production to the `build` folder.
-It correctly bundles React in production mode and optimizes the build for the best performance. - -The build is minified and the filenames include the hashes.
-Your app is ready to be deployed! - -See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. - -### `yarn eject` - -**Note: this is a one-way operation. Once you `eject`, you can’t go back!** - -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. - -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. - -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. - -## Learn More - -You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). - -To learn React, check out the [React documentation](https://reactjs.org/). - -### Code Splitting - -This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting - -### Analyzing the Bundle Size - -This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size - -### Making a Progressive Web App - -This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app - -### Advanced Configuration - -This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration - -### Deployment - -This section has moved here: https://facebook.github.io/create-react-app/docs/deployment - -### `yarn build` fails to minify - -This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify +## Coderan: Validator +The smart React element validator + +### Introduction +The goal of this package, is to simplify the struggle of validating elements in React, with a simple system which allows +users to add their own rules. +The system communicates directly with the elements in the DOM, and is therefore widely compatible with other libraries, +like [Bootstrap](https://react-bootstrap.github.io/). + +### The concept +Validator consists of two main elements, an `Area` and a `Provider`. Areas are a sort of wrappers having elements that +need validation as their children. An area scans the underlying components and elements and indexes validatable elements. + +Providers on the other hand are wrappers around areas, and allow them to communicate between each other. This communication +is needed in order to match with values in other areas. It can also be used to validate all areas at once, and preventing +actions to happen while not all areas are valid. + +### How to use +First, start with adding rules to the validator in order to use them. There are some rules pre-made, but more specific +rules you have to create yourself. + +```javascript +import { Validator } from '@coderan/validator'; +import { min } from '@coderan/rules/min'; + +Validator.extend('min', min); +``` + +#### Area +Basic usage: +```jsx +import { ValidatorArea } from '@coderan/validator'; + + + + +``` +When the input is focused and blurred, the `required` rule is called. + +Every area needs a name. This name is used to index areas in the provider, and make meaningful error messages. When using +multiple inputs within an area, i.e. when validating a multi-input date of birth, `name` prop is required when defining +the `ValidatorArea` component. Like so: + +```jsx +import { ValidatorArea } from '@coderan/validator'; + + + + + + +``` + +Showing errors: +```jsx +import { ValidatorArea } from '@coderan/validator'; + + + {({ errors }) => ( + <> + + { errors.length && {errors[0]} } + + )} + +``` + +#### Provider +Basic usage: +```jsx +import { ValidatorProvider, ValidatorArea } from '@coderan/validator'; + + + {({ validate }) => ( + <> + + + + + + + + + + + + + )} + +``` + +It is possible to give the validator a `rules` prop as well, whose rules apply to all underlying areas: + +```jsx +import { ValidatorProvider, ValidatorArea } from '@coderan/validator'; + + + + {/* on blur, both required and min rules are applied */} + + + +``` + +#### Adding rules + +With access to validator +```javascript +import { Validator } from '@coderan/validator' +Validator.extend('test_types', (validator: Validator) => ({ + passed(): boolean { + return validator.refs(undefined, HTMLInputElement).length === 1 + && validator.refs('test1', HTMLTextAreaElement).length === 1 + && validator.refs('test1').length === 1 + && validator.refs('test1', HTMLProgressElement).length === 0; + }, + message(): string { + return 'test'; + } +})); +``` + +or without +```javascript +import { getValue, isInputElement, isSelectElement } from '@/utils/dom'; + +export default { + passed(elements: HTMLElement[]): boolean { + return elements.every((element: HTMLElement) => { + if (isInputElement(element) || isSelectElement(element)) { + const value = getValue(element); + + return value && value.length; + } + + return true; + }) + }, + + message(): string { + return `{name} is required`; + } +}; +``` + +You can create your own rules, as long as it follows this interface: +```typescript +import { Validator } from '@coderan/validator'; + +/** + * Function to access validator using the rule + */ +declare type RuleFunction = (validator: Validator) => RuleObject; + +/** + * Object structure rules must implement + */ +declare type RuleObject = { + /** + * Returns whether the rule passed with the given element(s) + */ + passed(elements: HTMLElement[], ...args: string[]): boolean; + /** + * Message shown when the rule doesn't pass + */ + message(): string; +} + +export type Rule = RuleObject | RuleFunction; +``` + +### Happy Coding! From 3b896cc4d94547dd91ca09977e72e7e2d783e22a Mon Sep 17 00:00:00 2001 From: Eran Machiels Date: Fri, 2 Oct 2020 15:45:52 +0200 Subject: [PATCH 2/5] Added everything to be exported --- package.json | 5 +++++ src/rules/index.ts | 4 ++++ 2 files changed, 9 insertions(+) create mode 100644 src/rules/index.ts diff --git a/package.json b/package.json index 74a4436..0600b0a 100644 --- a/package.json +++ b/package.json @@ -11,11 +11,16 @@ "react-dom": "^16.13.1", "react-is": "^16.13.1" }, + "types": "dist/index.d.ts", "scripts": { "build": "tsc -p tsconfig-build.json", "test": "jest --config=jest.config.json", "test-coverage": "jest --config=jest.config.json --collectCoverage" }, + "repository": { + "type": "git", + "url": "https://github.com/machielsdev/validator" + }, "eslintConfig": { "extends": "react-app" }, diff --git a/src/rules/index.ts b/src/rules/index.ts new file mode 100644 index 0000000..b1363dd --- /dev/null +++ b/src/rules/index.ts @@ -0,0 +1,4 @@ +export { default as min } from '@/rules/min'; +export { default as max } from '@/rules/max'; +export { default as required } from '@/rules/required'; +export { IncorrectArgumentTypeError } from '@/rules/IncorrectArgumentTypeError'; From 2736c6f28a7c9ce593b5b005173f400367f0135b Mon Sep 17 00:00:00 2001 From: Eran Machiels Date: Fri, 2 Oct 2020 15:56:27 +0200 Subject: [PATCH 3/5] Added idea workspace map --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 800f3a8..6b79f25 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,6 @@ npm-debug.log* yarn-debug.log* yarn-error.log* + +# IDE +/.idea From 4cdf79d71ba661ed96a721639dbe3ed5d40dac65 Mon Sep 17 00:00:00 2001 From: Eran Machiels Date: Fri, 2 Oct 2020 15:58:44 +0200 Subject: [PATCH 4/5] Removed idea workspace file --- .idea/workspace.xml | 376 -------------------------------------------- 1 file changed, 376 deletions(-) delete mode 100644 .idea/workspace.xml diff --git a/.idea/workspace.xml b/.idea/workspace.xml deleted file mode 100644 index 9e8f4a7..0000000 --- a/.idea/workspace.xml +++ /dev/null @@ -1,376 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1597856329680 - - - 1600603797033 - - - 1600603954618 - - - 1600614414828 - - - 1600672907924 - - - 1600673892156 - - - 1600793437637 - - - 1601129017593 - - - 1601130549887 - - - 1601130933686 - - - 1601131153317 - - - 1601143654435 - - - 1601144562939 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file From d426f9f4004238b00c8ec6df98120f79f15c4699 Mon Sep 17 00:00:00 2001 From: Eran Machiels Date: Fri, 2 Oct 2020 15:59:31 +0200 Subject: [PATCH 5/5] 1.0.0 --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index e93233d..eb03e7a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@coderan/validator", - "version": "0.1.0", + "version": "1.0.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 0600b0a..adbd9eb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@coderan/validator", - "version": "0.1.0", + "version": "1.0.0", "private": true, "dependencies": { "@formatjs/intl": "^1.3.0",