Skip to content

1.0.0 #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint/eslint-plugin"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:react/recommended"
],
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module",
"project": "./tsconfig.json"
},
"env": {
"node": true,
"es6": true
},
"ignorePatterns": ["dist", "setupTests.ts", "babel.config.js"],
"rules": {
"comma-dangle": "off",
"class-methods-use-this": "off",
"import/prefer-default-export": "off",
"import/no-dynamic-require": "off",
"global-require": "off",
"quotes": ["error", "single", { "allowTemplateLiterals": true }],
"@typescript-eslint/indent": ["error", 4]
}
}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

# production
/build
/dist

# misc
.DS_Store
Expand All @@ -21,3 +22,6 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# IDE
/.idea
49 changes: 0 additions & 49 deletions .idea/workspace.xml

This file was deleted.

10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
language: node_js

node_js:
- "12"

script:
- npm install codecov -g
- npm run build
after_success:
- bash <(curl -s https://codecov.io/bash)
241 changes: 173 additions & 68 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.<br />
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.

The page will reload if you make edits.<br />
You will also see any lint errors in the console.

### `yarn test`

Launches the test runner in the interactive watch mode.<br />
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.<br />
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.<br />
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';

<ValidatorArea rules="required">
<input name="username" />
</ValidatorArea>
```
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';

<ValidatorArea rules="min" name="dob">
<input name="day" />
<input name="month" />
<input name="year" />
</ValidatorArea>
```

Showing errors:
```jsx
import { ValidatorArea } from '@coderan/validator';

<ValidatorArea rules="min" name="dob">
{({ errors }) => (
<>
<input name="username" />
{ errors.length && <span>{errors[0]}</span> }
</>
)}
</ValidatorArea>
```

#### Provider
Basic usage:
```jsx
import { ValidatorProvider, ValidatorArea } from '@coderan/validator';

<ValidatorProvider>
{({ validate }) => (
<>
<ValidatorArea rules="min" name="dob">
<input name="day" />
<input name="month" />
<input name="year" />
</ValidatorArea>
<ValidatorArea rules="min" name="dob">
<input name="day" />
<input name="month" />
<input name="year" />
</ValidatorArea>
<button
onClick={() => validate(() => alert('valid'))}>Check</button>
</>
)}
</ValidatorProvider>
```

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';

<ValidatorProvider rules="required">
<ValidatorArea rules="min:5">
{/* on blur, both required and min rules are applied */}
<input name="username" />
</ValidatorArea>
</ValidatorProvider>
```

#### 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!
Loading