Comparing version 0.0.1 to 1.1.0
@@ -0,1 +1,13 @@ | ||
# 1.1.0 (2019-03-15) | ||
## Version 1.1 | ||
- Rename `.babelrc` to `babel.config.js` | ||
- Change exports in index.js | ||
- Update types.js | ||
- Create RULES.md | ||
- Updated README.md | ||
- Updated Package.json | ||
- Setup documentation |
@@ -16,9 +16,16 @@ "use strict"; | ||
* Validation Factory, where all the validation magic happens | ||
* | ||
* @class {Validator} | ||
*/ | ||
class Validator { | ||
/** | ||
* @description Use `Validator.factory()` over a direct constructor | ||
* @param {ValidationRule[]} rules - List of rules to initalize with | ||
* @returns {Validator} - Validator Instance | ||
* @example | ||
* const validator = new Validator() | ||
*/ | ||
constructor(rules = []) { | ||
_defineProperty(this, "rules", []); | ||
if (rules) this.rules = [...rules]; | ||
this.rules = rules; | ||
} | ||
@@ -29,3 +36,7 @@ | ||
* | ||
* @param {ValidationRule} rules List of rules to initalize the factory with | ||
* @param {ValidationRule[]} rules - List of rules to initalize the factory with | ||
* @returns {Validator} - Validator instance | ||
* | ||
* @example | ||
* const validator = Validator.factory([]) | ||
*/ | ||
@@ -39,2 +50,3 @@ static factory(rules = []) { | ||
* @param {ValidationRule} rule A validation rule | ||
* @returns {Validator} - Validator instance | ||
*/ | ||
@@ -50,3 +62,4 @@ | ||
* | ||
* @param {Array<ValidationRule>} rules An array of rules to add | ||
* @param {ValidationRule[]} rules An array of rules to add | ||
* @returns {Validator} - Validator instance | ||
*/ | ||
@@ -63,2 +76,5 @@ | ||
* @param {string} value The string to be validated | ||
* @deprecated Use `validate(value: string)` instead. Depricated since v1.1 | ||
* @see validate | ||
* @returns {ValidatorResult} - The validation outcome | ||
*/ | ||
@@ -68,2 +84,14 @@ | ||
runWithValue(value) { | ||
return this.validate(value); | ||
} | ||
/** | ||
* Validates a string | ||
* @param {String} value - The string to be validated | ||
* @returns {ValidatorResult} - The validation outcome | ||
* @example | ||
* const { success, failed } = Validator.factory(rules.isRequired).validate('hello') | ||
*/ | ||
validate(value) { | ||
const failedToRuns = this.rules.filter(rule => !rule.runWithValue(value)); | ||
@@ -70,0 +98,0 @@ |
@@ -12,10 +12,14 @@ "use strict"; | ||
}); | ||
exports.rules = exports.default = void 0; | ||
exports.types = exports.rules = exports.default = void 0; | ||
var _factory = _interopRequireDefault(require("./factory")); | ||
var _rules = _interopRequireWildcard(require("./rules")); | ||
var rules = _interopRequireWildcard(require("./rules")); | ||
exports.rules = _rules; | ||
exports.rules = rules; | ||
var types = _interopRequireWildcard(require("./types")); | ||
exports.types = types; | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } | ||
@@ -22,0 +26,0 @@ |
@@ -13,3 +13,4 @@ "use strict"; | ||
const isRequired = { | ||
runWithValue: (value = null) => { | ||
runWithValue: value => { | ||
if (value === null || value === undefined) return false; | ||
if (typeof value === 'string') return _.hasText(value); | ||
@@ -16,0 +17,0 @@ return value !== null || value !== undefined; |
{ | ||
"name": "validazz", | ||
"version": "0.0.1", | ||
"version": "1.1.0", | ||
"description": "Magical, Flexible and Extendible Javascript Validation", | ||
@@ -19,3 +19,3 @@ "license": "MIT", | ||
"test": "jest", | ||
"coverage": "npm test -- --coverage", | ||
"coverage": "yarn test -- --coverage", | ||
"postcoverage": "opn coverage/lcov-report/index.html", | ||
@@ -28,5 +28,5 @@ "lint": "eslint .", | ||
"flowbuild": "flow-copy-source src dist", | ||
"prebuild": "npm run docs && npm run clean && npm run flowbuild", | ||
"prebuild": "yarn run docs && yarn run clean && yarn run flowbuild", | ||
"build": "babel src -d dist", | ||
"preversion": "npm run lint && npm test && npm run build", | ||
"preversion": "yarn run lint && yarn test && yarn run build", | ||
"version": "standard-changelog && git add CHANGELOG.md", | ||
@@ -53,2 +53,3 @@ "postpublish": "git push origin master --follow-tags" | ||
"@babel/plugin-proposal-class-properties": "7.3.4", | ||
"@babel/plugin-proposal-export-namespace-from": "7.2.0", | ||
"@babel/preset-env": "7.3.4", | ||
@@ -58,2 +59,4 @@ "@babel/preset-flow": "7.0.0", | ||
"babel-jest": "24.1.0", | ||
"browserslist": "4.4.2", | ||
"caniuse-lite": "1.0.30000947", | ||
"documentation": "9.3.0", | ||
@@ -76,3 +79,7 @@ "eslint": "5.15.1", | ||
"standard-changelog": "2.0.7" | ||
}, | ||
"resolutions": { | ||
"browserslist": "4.4.1", | ||
"caniuse-lite": "1.0.30000932" | ||
} | ||
} |
184
README.md
@@ -7,3 +7,3 @@ # validazz | ||
Magical, Flexible and Extendible Javascript Validation | ||
Magical, Flexible and Extendible Javascript Validation. | ||
@@ -52,2 +52,4 @@ ## Install | ||
For a list of all the included rules, be sure to check the [RULES.md](<>) file | ||
## API | ||
@@ -59,16 +61,184 @@ | ||
- [sayHello](#sayhello) | ||
- [Validator](#validator) | ||
- [Parameters](#parameters) | ||
- [addRule](#addrule) | ||
- [Parameters](#parameters-1) | ||
- [addRules](#addrules) | ||
- [Parameters](#parameters-2) | ||
- [runWithValue](#runwithvalue) | ||
- [Parameters](#parameters-3) | ||
- [validate](#validate) | ||
- [Parameters](#parameters-4) | ||
- [Examples](#examples) | ||
- [factory](#factory) | ||
- [Parameters](#parameters-5) | ||
- [Examples](#examples-1) | ||
- [runWithValue](#runwithvalue-1) | ||
- [Parameters](#parameters-6) | ||
- [Examples](#examples-2) | ||
- [ValidationRule](#validationrule) | ||
- [Properties](#properties) | ||
- [Examples](#examples-3) | ||
- [ValidatorResult](#validatorresult) | ||
- [Properties](#properties-1) | ||
- [Examples](#examples-4) | ||
### sayHello | ||
### Validator | ||
This function says hello. | ||
Validation Factory, where all the validation magic happens | ||
**Parameters** | ||
Type: [Validator](#validator) | ||
- `name` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Some name to say hello for. | ||
#### Parameters | ||
Returns **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** The hello. | ||
- `rules` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[ValidationRule](#validationrule)>** List of rules to initalize with (optional, default `[]`) | ||
#### addRule | ||
Add a rule to the factory | ||
##### Parameters | ||
- `rule` **[ValidationRule](#validationrule)** A validation rule | ||
Returns **[Validator](#validator)** Validator instance | ||
#### addRules | ||
Add a rules to the factory | ||
##### Parameters | ||
- `rules` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[ValidationRule](#validationrule)>** An array of rules to add | ||
Returns **[Validator](#validator)** Validator instance | ||
#### runWithValue | ||
- **See: validate** | ||
Run the factory and validate! | ||
##### Parameters | ||
- `value` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** The string to be validated | ||
Returns **[ValidatorResult](#validatorresult)** The validation outcome | ||
**Meta** | ||
- **deprecated**: Use `validate(value: string)` instead. Depricated since v1.1 | ||
#### validate | ||
Validates a string | ||
##### Parameters | ||
- `value` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** The string to be validated | ||
##### Examples | ||
```javascript | ||
const { success, failed } = Validator.factory(rules.isRequired).validate('hello') | ||
``` | ||
Returns **[ValidatorResult](#validatorresult)** The validation outcome | ||
#### factory | ||
Create a new validation factory | ||
##### Parameters | ||
- `rules` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[ValidationRule](#validationrule)>** List of rules to initalize the factory with (optional, default `[]`) | ||
##### Examples | ||
```javascript | ||
const validator = Validator.factory([]) | ||
``` | ||
Returns **[Validator](#validator)** Validator instance | ||
### runWithValue | ||
The validation function for this rule. It takes the a string/integer value and returns a boolean. | ||
Type: [Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function) | ||
#### Parameters | ||
- `value` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Value of array element | ||
#### Examples | ||
```javascript | ||
const rule = { | ||
runWithValue(value) { | ||
return value != null | ||
} | ||
} | ||
``` | ||
Returns **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** If it returns true, the field is valid. | ||
### ValidationRule | ||
A validation rule | ||
Type: [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | ||
#### Properties | ||
- `message` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** A custom error message for this validation rule | ||
- `runWithValue` **[runWithValue](#runwithvalue)** Validation callback | ||
#### Examples | ||
```javascript | ||
// Basic Example | ||
const validationRule = { | ||
message: 'This field is required', | ||
runWithValue(value) { | ||
return value != null | ||
} | ||
} | ||
// Example with parameters | ||
const minimum = (min) => ({ | ||
message: `Amount must be greater than ${min}`, | ||
runWithValue(value) { | ||
const value = Number(value) | ||
return value > min | ||
} | ||
}) | ||
``` | ||
### ValidatorResult | ||
The validation result | ||
Type: [Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | ||
#### Properties | ||
- `success` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** The outcome of the validation | ||
- `failed` **[ValidationRule](#validationrule)** An optional value. Returns the rule that failed to validate | ||
#### Examples | ||
```javascript | ||
const { success, failed } = Validator.factory(rules.isRequired).validate('hello') | ||
if (success) { | ||
alert('validated') | ||
} else { | ||
const { message } = failed | ||
alert(`Failed: ${message}`) | ||
} | ||
``` | ||
## License | ||
MIT © [Jesse Onolememen](https://github.com/jesster2k10) |
// @flow | ||
import type { ValidationRule } from './types' | ||
import type { ValidationRule, ValidatorResult } from './types' | ||
import * as _ from './helpers' | ||
@@ -7,7 +7,15 @@ | ||
* Validation Factory, where all the validation magic happens | ||
* | ||
* @class {Validator} | ||
*/ | ||
class Validator { | ||
constructor(rules: ?(ValidationRule[]) = []) { | ||
/** | ||
* @description Use `Validator.factory()` over a direct constructor | ||
* @param {ValidationRule[]} rules - List of rules to initalize with | ||
* @returns {Validator} - Validator Instance | ||
* @example | ||
* const validator = new Validator() | ||
*/ | ||
constructor(rules: ValidationRule[] = []) { | ||
this.rules = rules | ||
@@ -21,6 +29,10 @@ } | ||
* | ||
* @param {ValidationRule} rules List of rules to initalize the factory with | ||
* @param {ValidationRule[]} rules - List of rules to initalize the factory with | ||
* @returns {Validator} - Validator instance | ||
* | ||
* @example | ||
* const validator = Validator.factory([]) | ||
*/ | ||
static factory(rules: ?(ValidationRule[]) = []) { | ||
static factory(rules: ValidationRule[] = []) { | ||
return new Validator(rules) | ||
@@ -33,2 +45,3 @@ } | ||
* @param {ValidationRule} rule A validation rule | ||
* @returns {Validator} - Validator instance | ||
*/ | ||
@@ -43,6 +56,7 @@ addRule(rule: ValidationRule) { | ||
* | ||
* @param {Array<ValidationRule>} rules An array of rules to add | ||
* @param {ValidationRule[]} rules An array of rules to add | ||
* @returns {Validator} - Validator instance | ||
*/ | ||
addRules(rules: ?(ValidationRule[])) { | ||
this.rules = [...this.rules, ...rules] | ||
addRules(rules: ValidationRule[]) { | ||
this.rules = this.rules.concat(rules) | ||
return this | ||
@@ -55,7 +69,22 @@ } | ||
* @param {string} value The string to be validated | ||
* @deprecated Use `validate(value: string)` instead. Depricated since v1.1 | ||
* @see validate | ||
* @returns {ValidatorResult} - The validation outcome | ||
*/ | ||
runWithValue(value: ?string): ValidatorResult { | ||
runWithValue(value: string): ValidatorResult { | ||
return this.validate(value) | ||
} | ||
/** | ||
* Validates a string | ||
* @param {String} value - The string to be validated | ||
* @returns {ValidatorResult} - The validation outcome | ||
* @example | ||
* const { success, failed } = Validator.factory(rules.isRequired).validate('hello') | ||
*/ | ||
validate(value: string): ValidatorResult { | ||
const failedToRuns = this.rules.filter( | ||
(rule: ValidationRules) => !rule.runWithValue(value) | ||
(rule: ValidationRule) => !rule.runWithValue(value) | ||
) | ||
@@ -62,0 +91,0 @@ if (_.isEmptyArray(failedToRuns)) { |
import Validator from './factory' | ||
import * as rules from './rules' | ||
import * as types from './types' | ||
export * as rules from './rules' | ||
export { rules } | ||
export { types } | ||
export { default as Validator } from './factory' | ||
export default Validator |
@@ -6,5 +6,6 @@ // @flow | ||
export const isRequired: ValidationRule = { | ||
runWithValue: (value: any = null) => { | ||
runWithValue: (value: string) => { | ||
if (value === null || value === undefined) return false | ||
if (typeof value === 'string') return _.hasText(value) | ||
return value !== null || value != undefined | ||
return value !== null || value !== undefined | ||
}, | ||
@@ -11,0 +12,0 @@ message: 'This field is required', |
// @flow | ||
/** | ||
* @callback runWithValue | ||
* @description The validation function for this rule. It takes the a string/integer value and returns a boolean. | ||
* @param {string} value - Value of array element | ||
* @returns {Boolean} - If it returns true, the field is valid. | ||
* @example | ||
* const rule = { | ||
* runWithValue(value) { | ||
* return value != null | ||
* } | ||
* } | ||
*/ | ||
/** | ||
* Validation Rule | ||
* @typedef {Object} ValidationRule | ||
* @description A validation rule | ||
* @property {string} message - A custom error message for this validation rule | ||
* @property {runWithValue} runWithValue - Validation callback | ||
* @example | ||
* // Basic Example | ||
* const validationRule = { | ||
* message: 'This field is required', | ||
* runWithValue(value) { | ||
* return value != null | ||
* } | ||
* } | ||
* | ||
* // Example with parameters | ||
* const minimum = (min) => ({ | ||
* message: `Amount must be greater than ${min}`, | ||
* runWithValue(value) { | ||
* const value = Number(value) | ||
* return value > min | ||
* } | ||
* }) | ||
*/ | ||
type ValidationRule = { | ||
runWithValue: (value: string | number) => boolean, | ||
runWithValue: (value: string) => boolean, | ||
message?: string, | ||
} | ||
/** | ||
* ValidatorResult | ||
* @typedef {Object} ValidatorResult | ||
* @description The validation result | ||
* @property {Boolean} success - The outcome of the validation | ||
* @property {ValidationRule} failed - An optional value. Returns the rule that failed to validate | ||
* @example | ||
* const { success, failed } = Validator.factory(rules.isRequired).validate('hello') | ||
* if (success) { | ||
* alert('validated') | ||
* } else { | ||
* const { message } = failed | ||
* alert(`Failed: ${message}`) | ||
* } | ||
* | ||
* | ||
*/ | ||
type ValidatorResult = { | ||
@@ -9,0 +63,0 @@ success: boolean, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
31714
452
1
242
27