Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

validazz

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

validazz - npm Package Compare versions

Comparing version 1.1.0 to 2.0.0

dist/factory.d.ts

178

dist/factory.js
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _ = _interopRequireWildcard(require("./helpers"));
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; } }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
Object.defineProperty(exports, "__esModule", { value: true });
var _ = require("./helpers");
/**

@@ -18,94 +8,76 @@ * Validation Factory, where all the validation magic happens

*/
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", []);
this.rules = rules;
}
/**
* Create a new validation factory
*
* @param {ValidationRule[]} rules - List of rules to initalize the factory with
* @returns {Validator} - Validator instance
*
* @example
* const validator = Validator.factory([])
*/
static factory(rules = []) {
return new Validator(rules);
}
/**
* Add a rule to the factory
*
* @param {ValidationRule} rule A validation rule
* @returns {Validator} - Validator instance
*/
addRule(rule) {
this.rules = [...this.rules, rule];
return this;
}
/**
* Add a rules to the factory
*
* @param {ValidationRule[]} rules An array of rules to add
* @returns {Validator} - Validator instance
*/
addRules(rules) {
this.rules = this.rules.concat(rules);
return this;
}
/**
* Run the factory and validate!
*
* @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) {
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));
if (_.isEmptyArray(failedToRuns)) {
return {
success: true
};
var Validator = /** @class */ (function () {
/**
* @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()
*/
function Validator(rules) {
if (rules === void 0) { rules = []; }
this.rules = [];
this.rules = rules;
}
const failed = _.first(failedToRuns);
return {
success: false,
failed
/**
* Create a new validation factory
*
* @param {ValidationRule[]} rules - List of rules to initalize the factory with
* @returns {Validator} - Validator instance
*
* @example
* const validator = Validator.factory([])
*/
Validator.factory = function (rules) {
if (rules === void 0) { rules = []; }
return new Validator(rules);
};
}
}
var _default = Validator;
exports.default = _default;
/**
* Add a rule to the factory
*
* @param {ValidationRule} rule A validation rule
* @returns {Validator} - Validator instance
*/
Validator.prototype.addRule = function (rule) {
this.rules = this.rules.concat([rule]);
return this;
};
/**
* Add a rules to the factory
*
* @param {ValidationRule[]} rules An array of rules to add
* @returns {Validator} - Validator instance
*/
Validator.prototype.addRules = function (rules) {
this.rules = this.rules.concat(rules);
return this;
};
/**
* Run the factory and validate!
*
* @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
*/
Validator.prototype.runWithValue = function (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')
*/
Validator.prototype.validate = function (value) {
var failedToRuns = this.rules.filter(function (rule) { return !rule.runWithValue(value); });
if (_.isEmptyArray(failedToRuns)) {
return { success: true };
}
var failed = _.first(failedToRuns);
return { success: false, failed: failed };
};
return Validator;
}());
exports.default = Validator;
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isEmptyArray = isEmptyArray;
exports.first = first;
Object.defineProperty(exports, "__esModule", { value: true });
function isEmptyArray(array) {
if (!array || array.length === 0) return true;
return false;
if (!array || array.length === 0)
return true;
return false;
}
exports.isEmptyArray = isEmptyArray;
function first(array) {
if (!isEmptyArray(array)) return array[0];
return null;
}
if (!isEmptyArray(array))
return array[0];
return null;
}
exports.first = first;
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _array = require("./array");
Object.keys(_array).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _array[key];
}
});
});
var _strings = require("./strings");
Object.keys(_strings).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _strings[key];
}
});
});
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
Object.defineProperty(exports, "__esModule", { value: true });
__export(require("./array"));
__export(require("./strings"));
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.hasText = hasText;
Object.defineProperty(exports, "__esModule", { value: true });
// eslint-disable-next-line import/prefer-default-export
function hasText(text) {
if (typeof text !== 'string') {
return false;
}
if (!text || text === '' || text === null || text === undefined) {
return false;
}
if (!/\S/.test(text)) {
return false;
}
return true;
}
if (typeof text !== 'string') {
return false;
}
if (!text || text === '' || text === null || text === undefined) {
return false;
}
if (!/\S/.test(text)) {
return false;
}
return true;
}
exports.hasText = hasText;
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "Validator", {
enumerable: true,
get: function get() {
return _factory.default;
}
});
exports.types = exports.rules = exports.default = void 0;
var _factory = _interopRequireDefault(require("./factory"));
var rules = _interopRequireWildcard(require("./rules"));
Object.defineProperty(exports, "__esModule", { value: true });
var factory_1 = require("./factory");
var rules = require("./rules");
exports.rules = rules;
var types = _interopRequireWildcard(require("./types"));
var types = 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; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _default = _factory.default;
exports.default = _default;
var factory_2 = require("./factory");
exports.Validator = factory_2.default;
exports.default = factory_1.default;
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.containsLowercase = exports.containsUppercase = exports.isUppercase = exports.isEmail = exports.isNumeric = exports.isDigit = exports.isString = exports.isMatch = exports.isPattern = exports.exactLength = exports.maxLength = exports.minLength = exports.isRequired = void 0;
var _ = _interopRequireWildcard(require("../helpers"));
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; } }
const isRequired = {
runWithValue: value => {
if (value === null || value === undefined) return false;
if (typeof value === 'string') return _.hasText(value);
return value !== null || value !== undefined;
},
message: 'This field is required'
Object.defineProperty(exports, "__esModule", { value: true });
// @flow
var _ = require("../helpers");
exports.isRequired = {
runWithValue: function (value) {
if (value === null || value === undefined)
return false;
if (typeof value === 'string')
return _.hasText(value);
return value !== null || value !== undefined;
},
message: 'This field is required',
};
exports.isRequired = isRequired;
const minLength = min => ({
runWithValue: value => {
if (_.hasText(value)) return value.length >= min;
return false;
},
message: `Must be at least ${min} long`
});
exports.minLength = minLength;
const maxLength = max => ({
runWithValue: value => {
if (_.hasText(value)) return value.length <= max;
return false;
},
message: `Must be at most ${max} long`
});
exports.maxLength = maxLength;
const exactLength = exact => ({
runWithValue: value => {
if (_.hasText(value)) return value.length === exact;
return false;
},
message: `Must be ${exact} long`
});
exports.exactLength = exactLength;
const isPattern = pattern => ({
runWithValue: value => new RegExp(pattern).test(value)
});
exports.isPattern = isPattern;
const isMatch = pattern => ({
runWithValue(value) {
return new RegExp(pattern).test(value);
}
});
exports.isMatch = isMatch;
const isString = isPattern('^[a-zA-Z]+$');
exports.isString = isString;
const isDigit = isPattern('^[0-9]+$');
exports.isDigit = isDigit;
const isNumeric = isPattern('^[\\+\\-]?[0-9\\.,]+$');
exports.isNumeric = isNumeric;
const isEmail = isPattern('[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}');
exports.isEmail = isEmail;
const isUppercase = isPattern('[A-Z\\s]');
exports.isUppercase = isUppercase;
const containsUppercase = isPattern('.*[A-Z]+.*');
exports.containsUppercase = containsUppercase;
const containsLowercase = isPattern('.*[a-z]+.*');
exports.containsLowercase = containsLowercase;
exports.minLength = function (min) { return ({
runWithValue: function (value) {
if (_.hasText(value))
return value.length >= min;
return false;
},
message: "Must be at least " + min + " long",
}); };
exports.maxLength = function (max) { return ({
runWithValue: function (value) {
if (_.hasText(value))
return value.length <= max;
return false;
},
message: "Must be at most " + max + " long",
}); };
exports.exactLength = function (exact) { return ({
runWithValue: function (value) {
if (_.hasText(value))
return value.length === exact;
return false;
},
message: "Must be " + exact + " long",
}); };
exports.isPattern = function (pattern) { return ({
runWithValue: function (value) { return new RegExp(pattern).test(value); },
}); };
exports.isMatch = function (pattern) { return ({
runWithValue: function (value) {
return new RegExp(pattern).test(value);
},
}); };
exports.isString = exports.isPattern('^[a-zA-Z]+$');
exports.isDigit = exports.isPattern('^[0-9]+$');
exports.isNumeric = exports.isPattern('^[\\+\\-]?[0-9\\.,]+$');
exports.isEmail = exports.isPattern('[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}');
exports.isUppercase = exports.isPattern('[A-Z\\s]');
exports.containsUppercase = exports.isPattern('.*[A-Z]+.*');
exports.containsLowercase = exports.isPattern('.*[a-z]+.*');

@@ -1,1 +0,14 @@

"use strict";
"use strict";
/**
* @callback runWithValue
* @description The validation function for this rule. It takes the a string/integer value
* @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
* }
* }
*/
Object.defineProperty(exports, "__esModule", { value: true });
{
"name": "validazz",
"version": "1.1.0",
"version": "2.0.0",
"description": "Magical, Flexible and Extendible Javascript Validation",

@@ -8,2 +8,3 @@ "license": "MIT",

"main": "dist/index.js",
"types": "dist/index.d.ts",
"author": {

@@ -24,8 +25,9 @@ "name": "Jesse Onolememen",

"flow": "flow check",
"docs": "documentation readme src --section=API",
"docs": "echo 'docs'",
"postdocs": "git add README.md",
"clean": "rimraf dist",
"flowbuild": "flow-copy-source src dist",
"prebuild": "yarn run docs && yarn run clean && yarn run flowbuild",
"build": "babel src -d dist",
"tsc": "tsc",
"prebuild": "yarn run docs && yarn run clean",
"build": "tsc",
"preversion": "yarn run lint && yarn test && yarn run build",

@@ -56,2 +58,5 @@ "version": "standard-changelog && git add CHANGELOG.md",

"@babel/preset-flow": "7.0.0",
"@types/react": "16.8.12",
"@typescript-eslint/eslint-plugin": "1.6.0",
"@typescript-eslint/parser": "1.6.0",
"babel-eslint": "10.0.1",

@@ -76,4 +81,6 @@ "babel-jest": "24.1.0",

"prettier": "1.16.4",
"react": "16.8.4",
"rimraf": "2.6.3",
"standard-changelog": "2.0.7"
"standard-changelog": "2.0.7",
"typescript": "3.4.2"
},

@@ -80,0 +87,0 @@ "resolutions": {

@@ -51,6 +51,60 @@ # validazz

For a list of all the included rules, be sure to check the [RULES.md](<>) file
For a list of all the included rules, be sure to check the [RULES.md]() file
## API
# validazz
[![Generated with nod](https://img.shields.io/badge/generator-nod-2196F3.svg?style=flat-square)](https://github.com/diegohaz/nod)
[![NPM version](https://img.shields.io/npm/v/validazz.svg?style=flat-square)](https://npmjs.org/package/validazz)
[![Build Status](https://img.shields.io/travis//master.svg?style=flat-square)](https://travis-ci.org/) [![Coverage Status](https://img.shields.io/codecov/c/github//master.svg?style=flat-square)](https://codecov.io/gh//branch/master)
Magical, Flexible and Extendible Javascript Validation.
## Install
npm:
npm i validazz
Yarn:
yarn add validazz
## Usage
```js
import Validator, { rules } from 'validazz'
const mySuperCoolValidator = Validator.factory()
// Let's add some rules
mySuperCoolValidator.addRule(rules.isRequired)
mySuperCoolValidator.addRules([rules.minLength(2), rules.maxLength(8)])
mySuperCoolValidator.addRule(rules.isString)
// How about a custom rule?
const customRule = {
runWithValue: value => {
if (value !== '🤪') return false
return true
},
message: 'Houston, we got a problem',
}
mySuperCoolValidator.addRule(customRule)
// Okay let's start validating
const { success, failed } = mySuperCoolValidator.runWithValue('hello')
if (success) {
console.log('Wow, this was validated just like that')
} else {
const { message } = failed
console.log(`Okay so here's the error message: ${failed}`)
}
```
For a list of all the included rules, be sure to check the [RULES.md]() file
## API
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

@@ -60,25 +114,25 @@

- [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)
- [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)

@@ -93,3 +147,3 @@ ### Validator

- `rules` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[ValidationRule](#validationrule)>** List of rules to initalize with (optional, default `[]`)
- `rules` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[ValidationRule](#validationrule)>** List of rules to initalize with (optional, default `[]`)

@@ -102,3 +156,3 @@ #### addRule

- `rule` **[ValidationRule](#validationrule)** A validation rule
- `rule` **[ValidationRule](#validationrule)** A validation rule

@@ -113,3 +167,3 @@ Returns **[Validator](#validator)** Validator instance

- `rules` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[ValidationRule](#validationrule)>** An array of rules to add
- `rules` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[ValidationRule](#validationrule)>** An array of rules to add

@@ -120,3 +174,3 @@ Returns **[Validator](#validator)** Validator instance

- **See: validate**
- **See: validate**

@@ -127,3 +181,3 @@ Run the factory and validate!

- `value` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** The string to be validated
- `value` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** The string to be validated

@@ -134,5 +188,4 @@ Returns **[ValidatorResult](#validatorresult)** The validation outcome

- **deprecated**: Use `validate(value: string)` instead. Depricated since v1.1
- **deprecated**: Use `validate(value: string)` instead. Depricated since v1.1
#### validate

@@ -144,3 +197,3 @@

- `value` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** The string to be validated
- `value` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** The string to be validated

@@ -150,3 +203,5 @@ ##### Examples

```javascript
const { success, failed } = Validator.factory(rules.isRequired).validate('hello')
const { success, failed } = Validator.factory(rules.isRequired).validate(
'hello'
)
```

@@ -162,3 +217,3 @@

- `rules` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[ValidationRule](#validationrule)>** List of rules to initalize the factory with (optional, default `[]`)
- `rules` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[ValidationRule](#validationrule)>** List of rules to initalize the factory with (optional, default `[]`)

@@ -181,3 +236,3 @@ ##### Examples

- `value` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Value of array element
- `value` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Value of array element

@@ -188,6 +243,6 @@ #### Examples

const rule = {
runWithValue(value) {
return value != null
}
}
runWithValue(value) {
return value != null
},
}
```

@@ -205,4 +260,4 @@

- `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
- `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

@@ -213,17 +268,17 @@ #### Examples

// Basic Example
const validationRule = {
message: 'This field is required',
runWithValue(value) {
return value != null
}
}
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
}
})
// Example with parameters
const minimum = min => ({
message: `Amount must be greater than ${min}`,
runWithValue(value) {
const value = Number(value)
return value > min
},
})
```

@@ -239,4 +294,4 @@

- `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
- `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

@@ -246,9 +301,11 @@ #### Examples

```javascript
const { success, failed } = Validator.factory(rules.isRequired).validate('hello')
if (success) {
alert('validated')
} else {
const { message } = failed
alert(`Failed: ${message}`)
}
const { success, failed } = Validator.factory(rules.isRequired).validate(
'hello'
)
if (success) {
alert('validated')
} else {
const { message } = failed
alert(`Failed: ${message}`)
}
```

@@ -259,1 +316,7 @@

MIT © [Jesse Onolememen](https://github.com/jesster2k10)
### Table of Contents
## License
MIT © [Jesse Onolememen](https://github.com/jesster2k10)
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc