Skip to content

Commit

Permalink
add LICENSE
Browse files Browse the repository at this point in the history
  • Loading branch information
0529bill committed Jul 6, 2022
1 parent f8ac4e7 commit fe1cec8
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 12 deletions.
9 changes: 9 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT License

Copyright (c) 2022, Lin Tse.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
43 changes: 36 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# React-Client-Validation

[![npm version](https://badge.fury.io/js/react-client-validation.svg)](https://badge.fury.io/js/react-client-validation)

## Installation

```js
Expand All @@ -23,6 +25,7 @@ const loginValidation = [
},
{
index: "password",
//change errorObject's format
errorFormat: ["invalidPassword"],
customCondition: (data, errorReturnArray) => {
if (data.password) {
Expand All @@ -32,19 +35,20 @@ const loginValidation = [
},
];

const [isPass, loginValidationError] = handleValidation({
const [isPass, loginErrorObject] = handleValidation({
errorArray: loginValidation,
defaultErrorMessage: "input can't be blank",
});
```

```js
console.log(isPass); // boolean, true or false
console.log(loginValidationError);
console.log(loginErrorObject);

{
username: {msg: 'User name is not valid!'},
password: [ invalidPassword: {msg: "input can't be blank"}]
//password's value is formatted based on errorFormat from the errorArray
}
```

Expand All @@ -55,14 +59,35 @@ console.log(loginValidationError);
2. dataSource: any (is isRequired if using customCondition)

3. errorArray: Array (isRequired)
- condition: Array (pick one between condition or customCondition)
- customCondition: Function (pick one between condition or customCondition): (dataSource, returnArray) => [true, false....]
- index: string (isRequired): will be the key for the return error Object.
- errorMessage: string: will be the value for the return error message.
- errorFormat: Array (only allowed when using customCondition) set custom format.

## Example

- condition: Array (pick one between condition or customCondition)
- index: string (isRequired): will be the key for the return error Object.
- errorMessage: string: will be the value for the return error message.
- errorFormat: Array (only allowed when using customCondition) set custom format.
### errorFormat

```js
//errorFormat example
//only allowed when using customCondition



1. without errorFormat

{ ...,
customCondition: ...,
}
//returned error object

{
username: {msg:'User name is not valid!'}
}


2. with errorFormat
//scenario 1

{
Expand Down Expand Up @@ -98,7 +123,7 @@ console.log(loginValidationError);

```

- customCondition: Function (pick one between condition or customCondition): (dataSource, returnArray) => [true, false....]
### customCondition

```js

Expand All @@ -118,3 +143,7 @@ customCondition: (data, errorReturnArray) => {
}
}
```

## License

Released under [MIT License](LICENSE.md).
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"dist"
],
"author": "water-tw",
"license": "ISC",
"license": "MIC",
"dependencies": {
"prop-types": "^15.8.1"
},
Expand Down
8 changes: 4 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const handleValidation = ({
errorArray = [],
dataSource = [],
defaultErrorMessage = '',
defaultErrorMessage = "",
}) => {
let isPass = true;
const errorValidation = {};
const defaultMessage = defaultErrorMessage
? { msg: defaultErrorMessage }
: '';
: "";

// 接受 condition 為 [ condition1, condition2]
const handleValidateItem = (errorItem) => {
Expand All @@ -24,7 +24,7 @@ const handleValidation = ({
errorResult
) => {
const format = errorFormat.shift();
const targetIndex = format === 'index' ? currentIndex : format;
const targetIndex = format === "index" ? currentIndex : format;
if (errorFormat.length === 0) {
tempErrorObject[targetIndex] = errorResult;
return;
Expand All @@ -47,7 +47,7 @@ const handleValidation = ({
);
}

if (typeof item?.index !== 'string') {
if (typeof item?.index !== "string") {
// eslint-disable-next-line no-console
return console.error(
`Invalid type \`${typeof item?.index}"\` supplied to parameter "index", expected \`string\`!`
Expand Down

0 comments on commit fe1cec8

Please sign in to comment.