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

policeman

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

policeman - npm Package Compare versions

Comparing version 0.2.2 to 0.2.3

4

CHANGELOG.md
# CHANGELOG
## 0.2.3
* Target ES5
## 0.2.2

@@ -4,0 +8,0 @@

2

lib/policeman.d.ts

@@ -16,3 +16,3 @@ export interface Filter {

errors: any;
valid: boolean;
valid: any;
};

@@ -19,0 +19,0 @@ /**

@@ -5,8 +5,12 @@ "use strict";

}
const get = require("lodash/get");
const set = require("lodash/set");
var get = require("lodash/get");
var set = require("lodash/set");
/**
* Default filter function which accepts each entry
*/
function always(...args) {
function always() {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i - 0] = arguments[_i];
}
return true;

@@ -18,6 +22,7 @@ }

function applyValidators(value, source, validators) {
return validators.map(validator => validator(value, source)).filter(Boolean);
return validators.map(function (validator) { return validator(value, source); }).filter(Boolean);
}
function appendValue(source, errorPath, sourcePath, validate, filter = always) {
const value = get(source, sourcePath);
function appendValue(source, errorPath, sourcePath, validate, filter) {
if (filter === void 0) { filter = always; }
var value = get(source, sourcePath);
return [errorPath, sourcePath, validate, filter, value];

@@ -30,9 +35,9 @@ }

if (validate instanceof Array) {
const errors = applyValidators(value, source, validate);
const valid = !errors.length;
var errors = applyValidators(value, source, validate);
var valid = !errors.length;
return [errorPath, errors, valid];
}
else {
const error = validate(value, source);
const valid = error === null;
var error = validate(value, source);
var valid = error === null;
return [errorPath, error, valid];

@@ -47,9 +52,15 @@ }

function policeman(schema) {
return (source) => {
const validated = schema
.map(entry => appendValue.apply(null, [source, ...entry]))
.filter(entry => omitByFilter.apply(null, [source, ...entry]))
.map(entry => validateToErrors.apply(null, [source, ...entry]));
const errors = validated.reduce((errs, [errorPath, error]) => set(errs, errorPath, error), {});
const valid = !validated.find(([_errorPath, _error, correct]) => !correct);
return function (source) {
var validated = schema
.map(function (entry) { return appendValue.apply(null, [source].concat(entry)); })
.filter(function (entry) { return omitByFilter.apply(null, [source].concat(entry)); })
.map(function (entry) { return validateToErrors.apply(null, [source].concat(entry)); });
var errors = validated.reduce(function (errs, _a) {
var errorPath = _a[0], error = _a[1];
return set(errs, errorPath, error);
}, {});
var valid = validated.reduce(function (val, _a) {
var _errorPath = _a[0], _error = _a[1], correct = _a[2];
return val && correct;
}, true);
return { errors: errors, valid: valid };

@@ -65,4 +76,8 @@ };

*/
function combineValidators(...validators) {
return (value, source) => applyValidators(value, source, validators)[0] || null;
function combineValidators() {
var validators = [];
for (var _i = 0; _i < arguments.length; _i++) {
validators[_i - 0] = arguments[_i];
}
return function (value, source) { return applyValidators(value, source, validators)[0] || null; };
}

@@ -69,0 +84,0 @@ exports.combineValidators = combineValidators;

"use strict";
const curry = require("lodash/curry");
exports.isRequired = curry((message, value) => {
const valid = value !== null && value !== undefined && value !== "";
var curry = require("lodash/curry");
exports.isRequired = curry(function (message, value) {
var valid = value !== null && value !== undefined && value !== "";
return valid ? null : message(value);
});
exports.isMinLength = curry((min, message, value) => {
const valid = value.length >= min;
exports.isMinLength = curry(function (min, message, value) {
var valid = value.length >= min;
return valid ? null : message(value);
});
exports.isMaxLength = curry((max, message, value) => {
const valid = value.length <= max;
exports.isMaxLength = curry(function (max, message, value) {
var valid = value.length <= max;
return valid ? null : message(value);
});
exports.isEqualLength = curry((equal, message, value) => {
const valid = value.length === equal;
exports.isEqualLength = curry(function (equal, message, value) {
var valid = value.length === equal;
return valid ? null : message(value);
});
exports.isEmail = curry((message, value) => {
const valid = /^[a-z0-9_\-\.]{2,}@[a-z0-9_\-\.]{2,}\.[a-z]{2,}$/i.test(value);
exports.isEmail = curry(function (message, value) {
var valid = /^[a-z0-9_\-\.]{2,}@[a-z0-9_\-\.]{2,}\.[a-z]{2,}$/i.test(value);
return valid ? null : message(value);
});
exports.isMatching = curry((regexp, message, value) => {
const valid = regexp.test(value);
exports.isMatching = curry(function (regexp, message, value) {
var valid = regexp.test(value);
return valid ? null : message(value);
});
exports.isPassing = curry((predicate, message, value) => {
const valid = predicate(value);
exports.isPassing = curry(function (predicate, message, value) {
var valid = predicate(value);
return valid ? null : message(value);
});
//# sourceMappingURL=validators.js.map
{
"name": "policeman",
"version": "0.2.2",
"version": "0.2.3",
"description": "Lightweight yet powerful schema validator",

@@ -5,0 +5,0 @@ "main": "lib/policeman.js",

@@ -85,3 +85,3 @@ import * as get from "lodash/get";

const errors = validated.reduce((errs: Object, [errorPath, error]) => set(errs, errorPath, error), {});
const valid = !validated.find(([_errorPath, _error, correct]) => !correct);
const valid = validated.reduce((val, [_errorPath, _error, correct]) => val && correct, true);

@@ -88,0 +88,0 @@ return { errors, valid };

"use strict";
const tape = require("tape");
const policeman_1 = require("../lib/policeman");
const validators_1 = require("../lib/validators");
var tape = require("tape");
var policeman_1 = require("../lib/policeman");
var validators_1 = require("../lib/validators");
function appliesSignleValidator(t) {
const requiredCheck = validators_1.isRequired(() => "is required");
const validator = policeman_1.default([
var requiredCheck = validators_1.isRequired(function () { return "is required"; });
var validator = policeman_1.default([
["fullName", "full_name", requiredCheck],
]);
const actual = validator({});
const expected = {
var actual = validator({});
var expected = {
valid: false,

@@ -20,9 +20,9 @@ errors: {

function appliesMultipleValidators(t) {
const requiredCheck = validators_1.isRequired(() => "is required");
const emailCheck = validators_1.isEmail(() => "is invalid email");
const validator = policeman_1.default([
var requiredCheck = validators_1.isRequired(function () { return "is required"; });
var emailCheck = validators_1.isEmail(function () { return "is invalid email"; });
var validator = policeman_1.default([
["email", "email", [requiredCheck, emailCheck]],
]);
const actual = validator({});
const expected = {
var actual = validator({});
var expected = {
valid: false,

@@ -36,9 +36,9 @@ errors: {

function combinesMultipleValidators(t) {
const requiredCheck = validators_1.isRequired(() => "is required");
const emailCheck = validators_1.isEmail(() => "is invalid email");
const validator = policeman_1.default([
var requiredCheck = validators_1.isRequired(function () { return "is required"; });
var emailCheck = validators_1.isEmail(function () { return "is invalid email"; });
var validator = policeman_1.default([
["email", "email", policeman_1.combineValidators(requiredCheck, emailCheck)],
]);
const actual = validator({ email: "invalid" });
const expected = {
var actual = validator({ email: "invalid" });
var expected = {
valid: false,

@@ -52,9 +52,9 @@ errors: {

function validCombinesMultipleValidators(t) {
const requiredCheck = validators_1.isRequired(() => "is required");
const emailCheck = validators_1.isEmail(() => "is invalid email");
const validator = policeman_1.default([
var requiredCheck = validators_1.isRequired(function () { return "is required"; });
var emailCheck = validators_1.isEmail(function () { return "is invalid email"; });
var validator = policeman_1.default([
["email", "email", policeman_1.combineValidators(requiredCheck, emailCheck)],
]);
const actual = validator({ email: "test@example.com" });
const expected = {
var actual = validator({ email: "test@example.com" });
var expected = {
valid: true,

@@ -68,7 +68,7 @@ errors: {

function skipValidationByFilter(t) {
const requiredCheck = validators_1.isRequired(() => "is required");
const emailCheck = validators_1.isEmail(() => "is invalid email");
const phoneNumberCheck = validators_1.isMatching(/\d{3}-?\d{3}-?\d{3}/, () => "is invalid phone");
const skipGift = (value, source) => source.gift === true;
const validator = policeman_1.default([
var requiredCheck = validators_1.isRequired(function () { return "is required"; });
var emailCheck = validators_1.isEmail(function () { return "is invalid email"; });
var phoneNumberCheck = validators_1.isMatching(/\d{3}-?\d{3}-?\d{3}/, function () { return "is invalid phone"; });
var skipGift = function (value, source) { return source.gift === true; };
var validator = policeman_1.default([
["email", "email", [requiredCheck, emailCheck]],

@@ -78,4 +78,4 @@ ["phone", "phone", policeman_1.combineValidators(requiredCheck, phoneNumberCheck)],

]);
const actual = validator({ gift: false, email: "invalid@example", phone: "777-666-55" });
const expected = {
var actual = validator({ gift: false, email: "invalid@example", phone: "777-666-55" });
var expected = {
valid: false,

@@ -90,11 +90,11 @@ errors: {

function validSkipValidationByFilter(t) {
const requiredCheck = validators_1.isRequired(() => "is required");
const emailCheck = validators_1.isEmail(() => "is invalid email");
const isntGuest = (value, source) => source.guest !== true;
const validator = policeman_1.default([
var requiredCheck = validators_1.isRequired(function () { return "is required"; });
var emailCheck = validators_1.isEmail(function () { return "is invalid email"; });
var isntGuest = function (value, source) { return source.guest !== true; };
var validator = policeman_1.default([
["email", "email", [requiredCheck, emailCheck], isntGuest],
["name", "name", requiredCheck],
]);
const actual = validator({ guest: true, name: "Foo" });
const expected = {
var actual = validator({ guest: true, name: "Foo" });
var expected = {
valid: true,

@@ -107,3 +107,3 @@ errors: {

}
tape("policeman", (t) => {
tape("policeman", function (t) {
t.plan(6);

@@ -110,0 +110,0 @@ appliesSignleValidator(t);

"use strict";
const tape = require("tape");
const policeman_1 = require("../lib/policeman");
var tape = require("tape");
var policeman_1 = require("../lib/policeman");
function testIsRequired(t) {
const required = policeman_1.isRequired(() => "is required");
var required = policeman_1.isRequired(function () { return "is required"; });
t.is(required(""), "is required", "isRequired fails on empty string");

@@ -13,3 +13,3 @@ t.is(required(null), "is required", "isRequired fails on null");

function testIsMinLengthValidator(t) {
const isMinLength4 = policeman_1.isMinLength(4, () => "should be at least 4");
var isMinLength4 = policeman_1.isMinLength(4, function () { return "should be at least 4"; });
t.is(isMinLength4("foo bar"), null, "isMinLength(4) accepts strings with 4+ characters");

@@ -20,3 +20,3 @@ t.is(isMinLength4("fooz"), null, "isMinLength(4) accepts strings with 4 characters");

function testIsMaxLengthValidator(t) {
const isMaxLength4 = policeman_1.isMaxLength(4, () => "should be at most 4");
var isMaxLength4 = policeman_1.isMaxLength(4, function () { return "should be at most 4"; });
t.is(isMaxLength4("foo bar"), "should be at most 4", "isMaxLength(4) fails on strings with 4+ characters");

@@ -27,3 +27,3 @@ t.is(isMaxLength4("fooz"), null, "isMaxLength(4) accepts strings with 4 characters");

function testIsEqualLengthValidator(t) {
const isEqualLength4 = policeman_1.isEqualLength(4, () => "should be 4 characters long");
var isEqualLength4 = policeman_1.isEqualLength(4, function () { return "should be 4 characters long"; });
t.is(isEqualLength4("abc"), "should be 4 characters long", "isEqualLength(4) fails on 3 characters long string");

@@ -34,3 +34,3 @@ t.is(isEqualLength4("abcde"), "should be 4 characters long", "isEqualLength(4) fails on 5 characters long string");

function testIsEmail(t) {
const email = policeman_1.isEmail(() => "is invalid email");
var email = policeman_1.isEmail(function () { return "is invalid email"; });
t.is(email("foo@bar.com"), null, "isEmail accepts valid email");

@@ -40,3 +40,3 @@ t.is(email("foo@.com"), "is invalid email", "isEmail fails on invalid email");

function testIsMatching(t) {
const matching = policeman_1.isMatching(/^\d{3}-?\d{3}-?\d{3}$/, () => "is invalid number");
var matching = policeman_1.isMatching(/^\d{3}-?\d{3}-?\d{3}$/, function () { return "is invalid number"; });
t.is(matching("777-888-999"), null, "isMatching(/^\d{3}-?\d{3}-?\d{3}$/) accepts valid number");

@@ -47,8 +47,8 @@ t.is(matching("777888999"), null, "isMatching(/^\d{3}-?\d{3}-?\d{3}$/) accepts valid number");

function testIsPassing(t) {
const isFoo = (str) => str === "foo";
const matching = policeman_1.isPassing(isFoo, () => "must be \"foo\"");
var isFoo = function (str) { return str === "foo"; };
var matching = policeman_1.isPassing(isFoo, function () { return "must be \"foo\""; });
t.is(matching("foo"), null, "predicate returns true on \"foo\"");
t.is(matching("bar"), "must be \"foo\"", "predicate returns false on not \"foo\"");
}
tape("validators", (t) => {
tape("validators", function (t) {
t.plan(21);

@@ -55,0 +55,0 @@ testIsRequired(t);

{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"target": "es5",
"sourceMap": true,

@@ -6,0 +6,0 @@ "rootDir": "./src",

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

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