Comparing version 2.0.1 to 2.0.2
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var _ = require("./helpers"); | ||
const _ = require("./helpers/index"); | ||
/** | ||
@@ -8,3 +8,3 @@ * Validation Factory, where all the validation magic happens | ||
*/ | ||
var Validator = /** @class */ (function () { | ||
class Validator { | ||
/** | ||
@@ -17,4 +17,3 @@ * @description Use `Validator.factory()` over a direct constructor | ||
*/ | ||
function Validator(rules) { | ||
if (rules === void 0) { rules = []; } | ||
constructor(rules = []) { | ||
this.rules = []; | ||
@@ -32,6 +31,5 @@ this.rules = rules; | ||
*/ | ||
Validator.factory = function (rules) { | ||
if (rules === void 0) { rules = []; } | ||
static factory(rules = []) { | ||
return new Validator(rules); | ||
}; | ||
} | ||
/** | ||
@@ -43,6 +41,6 @@ * Add a rule to the factory | ||
*/ | ||
Validator.prototype.addRule = function (rule) { | ||
this.rules = this.rules.concat([rule]); | ||
addRule(rule) { | ||
this.rules = [...this.rules, rule]; | ||
return this; | ||
}; | ||
} | ||
/** | ||
@@ -54,6 +52,6 @@ * Add a rules to the factory | ||
*/ | ||
Validator.prototype.addRules = function (rules) { | ||
addRules(rules) { | ||
this.rules = this.rules.concat(rules); | ||
return this; | ||
}; | ||
} | ||
/** | ||
@@ -67,5 +65,5 @@ * Run the factory and validate! | ||
*/ | ||
Validator.prototype.runWithValue = function (value) { | ||
runWithValue(value) { | ||
return this.validate(value); | ||
}; | ||
} | ||
/** | ||
@@ -78,12 +76,11 @@ * Validates a string | ||
*/ | ||
Validator.prototype.validate = function (value) { | ||
var failedToRuns = this.rules.filter(function (rule) { return !rule.runWithValue(value); }); | ||
validate(value) { | ||
const failedToRuns = this.rules.filter((rule) => !rule.runWithValue(value)); | ||
if (_.isEmptyArray(failedToRuns)) { | ||
return { success: true }; | ||
} | ||
var failed = _.first(failedToRuns); | ||
return { success: false, failed: failed }; | ||
}; | ||
return Validator; | ||
}()); | ||
const failed = _.first(failedToRuns); | ||
return { success: false, failed, allFailed: failedToRuns }; | ||
} | ||
} | ||
exports.default = Validator; |
import Validator from './factory'; | ||
import * as rules from './rules'; | ||
import * as types from './types'; | ||
import * as rules from './rules/index'; | ||
export { rules }; | ||
export { types }; | ||
export { default as Validator } from './factory'; | ||
export default Validator; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var factory_1 = require("./factory"); | ||
var rules = require("./rules"); | ||
const factory_1 = require("./factory"); | ||
const rules = require("./rules/index"); | ||
exports.rules = rules; | ||
var types = require("./types"); | ||
exports.types = types; | ||
var factory_2 = require("./factory"); | ||
exports.Validator = factory_2.default; | ||
exports.default = factory_1.default; |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = function (d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/* eslint-disable no-unused-vars */ | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
var React = require("react"); | ||
var factory_1 = require("../factory"); | ||
exports.default = (function (WrappedComponent, config) { | ||
var _a = config.initalRules, initalRules = _a === void 0 ? [] : _a, _b = config.renderError, renderError = _b === void 0 ? null : _b, _c = config.showErrorInPlaceholder, showErrorInPlaceholder = _c === void 0 ? false : _c, _d = config.errorStyles, errorStyles = _d === void 0 ? {} : _d; | ||
return /** @class */ (function (_super) { | ||
__extends(class_1, _super); | ||
function class_1(props) { | ||
var _this = _super.call(this, props) || this; | ||
_this.validator = factory_1.default.factory(); | ||
const React = require("react"); | ||
const factory_1 = require("../factory"); | ||
exports.default = (WrappedComponent, config) => { | ||
const { initalRules = [], renderError = null, showErrorInPlaceholder = false, errorStyles = {}, } = config; | ||
return class extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.validator = factory_1.default.factory(); | ||
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type | ||
_this.onChangeText = function (value) { | ||
var propsOnChange = _this.props.onChangeText; | ||
this.onChangeText = (value) => { | ||
const { onChangeText: propsOnChange } = this.props; | ||
if (propsOnChange) | ||
propsOnChange(); | ||
_this.setState({ value: value }); | ||
_this.validate(value); | ||
this.setState({ value }); | ||
this.validate(value); | ||
}; | ||
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type | ||
_this.validate = function (value) { | ||
var _a = _this.validator.runWithValue(value), success = _a.success, failed = _a.failed; | ||
this.validate = (value) => { | ||
const { success, failed } = this.validator.runWithValue(value); | ||
if (success) { | ||
_this.setState({ valid: true }); | ||
this.setState({ valid: true }); | ||
} | ||
else { | ||
_this.setState({ | ||
this.setState({ | ||
valid: false, | ||
@@ -48,3 +34,3 @@ error: failed ? failed.message : '', | ||
}; | ||
_this.state = { | ||
this.state = { | ||
value: '', | ||
@@ -54,15 +40,13 @@ error: '', | ||
}; | ||
return _this; | ||
} | ||
class_1.prototype.render = function () { | ||
var rules = this.props.rules; | ||
var value = this.state.value; | ||
render() { | ||
const { rules } = this.props; | ||
const { value } = this.state; | ||
return React.cloneElement(WrappedComponent, Object.assign(this.props, { | ||
value: value, | ||
rules: rules, | ||
value, | ||
rules, | ||
onChangeText: this.onChangeText, | ||
})); | ||
}; | ||
return class_1; | ||
}(React.Component)); | ||
}); | ||
} | ||
}; | ||
}; |
@@ -6,3 +6,3 @@ import { ValidationRule } from '../types'; | ||
export declare const exactLength: (exact: number) => ValidationRule; | ||
export declare const isPattern: (pattern: string) => ValidationRule; | ||
export declare const isPattern: (pattern: string, message?: string | undefined) => ValidationRule; | ||
export declare const isMatch: (pattern: string) => ValidationRule; | ||
@@ -9,0 +9,0 @@ export declare const isString: ValidationRule; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
// @flow | ||
var _ = require("../helpers"); | ||
const _ = require("../helpers/index"); | ||
exports.isRequired = { | ||
runWithValue: function (value) { | ||
runWithValue: (value) => { | ||
if (value === null || value === undefined) | ||
@@ -15,4 +14,4 @@ return false; | ||
}; | ||
exports.minLength = function (min) { return ({ | ||
runWithValue: function (value) { | ||
exports.minLength = (min) => ({ | ||
runWithValue: (value) => { | ||
if (_.hasText(value)) | ||
@@ -22,6 +21,6 @@ return value.length >= min; | ||
}, | ||
message: "Must be at least " + min + " long", | ||
}); }; | ||
exports.maxLength = function (max) { return ({ | ||
runWithValue: function (value) { | ||
message: `Must be at least ${min} long`, | ||
}); | ||
exports.maxLength = (max) => ({ | ||
runWithValue: (value) => { | ||
if (_.hasText(value)) | ||
@@ -31,6 +30,6 @@ return value.length <= max; | ||
}, | ||
message: "Must be at most " + max + " long", | ||
}); }; | ||
exports.exactLength = function (exact) { return ({ | ||
runWithValue: function (value) { | ||
message: `Must be at most ${max} long`, | ||
}); | ||
exports.exactLength = (exact) => ({ | ||
runWithValue: (value) => { | ||
if (_.hasText(value)) | ||
@@ -40,18 +39,19 @@ return value.length === exact; | ||
}, | ||
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) { | ||
message: `Must be ${exact} long`, | ||
}); | ||
exports.isPattern = (pattern, message) => ({ | ||
runWithValue: (value) => new RegExp(pattern).test(value), | ||
message, | ||
}); | ||
exports.isMatch = (pattern) => ({ | ||
runWithValue(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]+.*'); | ||
exports.isDigit = exports.isPattern('^[0-9]+$', 'Invalid number'); | ||
exports.isNumeric = exports.isPattern('^[\\+\\-]?[0-9\\.,]+$', 'Must be numeric'); | ||
exports.isEmail = exports.isPattern('[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}', "That's not an email"); | ||
exports.isUppercase = exports.isPattern('[A-Z\\s]', 'Must be uppercase'); | ||
exports.containsUppercase = exports.isPattern('.*[A-Z]+.*', 'Must contain one uppercase character'); | ||
exports.containsLowercase = exports.isPattern('.*[a-z]+.*', 'Must contain one lowercase character'); |
@@ -61,2 +61,3 @@ /** | ||
failed?: ValidationRule; | ||
allFailed?: ValidationRule[]; | ||
} |
{ | ||
"name": "validazz", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "Magical, Flexible and Extendible Javascript Validation", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -88,3 +88,3 @@ import * as _ from './helpers/index' | ||
} | ||
const failed = failedToRuns[0] | ||
const failed = _.first(failedToRuns) | ||
return { success: false, failed, allFailed: failedToRuns } | ||
@@ -91,0 +91,0 @@ } |
@@ -37,4 +37,8 @@ import * as _ from '../helpers/index' | ||
export const isPattern = (pattern: string): ValidationRule => ({ | ||
export const isPattern = ( | ||
pattern: string, | ||
message?: string, | ||
): ValidationRule => ({ | ||
runWithValue: (value: string): boolean => new RegExp(pattern).test(value), | ||
message, | ||
}) | ||
@@ -49,9 +53,22 @@ | ||
export const isString: ValidationRule = isPattern('^[a-zA-Z]+$') | ||
export const isDigit: ValidationRule = isPattern('^[0-9]+$') | ||
export const isNumeric: ValidationRule = isPattern('^[\\+\\-]?[0-9\\.,]+$') | ||
export const isDigit: ValidationRule = isPattern('^[0-9]+$', 'Invalid number') | ||
export const isNumeric: ValidationRule = isPattern( | ||
'^[\\+\\-]?[0-9\\.,]+$', | ||
'Must be numeric', | ||
) | ||
export const isEmail: ValidationRule = isPattern( | ||
'[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}', | ||
"That's not an email", | ||
) | ||
export const isUppercase: ValidationRule = isPattern('[A-Z\\s]') | ||
export const containsUppercase: ValidationRule = isPattern('.*[A-Z]+.*') | ||
export const containsLowercase: ValidationRule = isPattern('.*[a-z]+.*') | ||
export const isUppercase: ValidationRule = isPattern( | ||
'[A-Z\\s]', | ||
'Must be uppercase', | ||
) | ||
export const containsUppercase: ValidationRule = isPattern( | ||
'.*[A-Z]+.*', | ||
'Must contain one uppercase character', | ||
) | ||
export const containsLowercase: ValidationRule = isPattern( | ||
'.*[a-z]+.*', | ||
'Must contain one lowercase character', | ||
) |
34376
724