Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Lightweight yet powerful schema validator
npm i -S policeman
import policeman, { isRequired, isEmail, isMatching, combineValidators } from "policeman";
// setup entry validators
const requiredValidator = isRequired(() => "is required");
const emailValidator = isEmail(() => "is invalid email");
const phoneNumberValidator = isMatching(/\d{3}-?\d{3}-?\d{3}/, () => "is invalid phone");
// setup entry filter predicates
const isGift: Filter = (value, source) => source.gift === true;
// define schema
const schema = [
// 1. array of validators - multiple errors
// 2. combine validators - first of many errors
// 3. single validator - single error
// 4. skip validation based on filter predicate
["email", "email", [requiredValidator, emailValidator]], // #1
["phone", "phone", combineValidators(requiredValidator, phoneNumberValidator)], // #2
["name", "name", requiredValidator], // #3
["giftCode", "giftCode", requiredValidator, isGift], // #4
];
// create validator
const validator = policeman(schema);
// validate
validator({ gift: false, email: "invalid@example", phone: "777-666-55" });
// {
// valid: false,
// errors: {
// email: ["is invalid email"],
// phone: "is invalid phone",
// name: "is required"
// }
// }
See tests for more examples.
All built-in validators are curried.
isRequired(() => message, value)
Validates presence. Fails on null
, empty string or undefined
.
isMinLength(min, () => message, value)
Passed value
must be a string longer or with length equal to min
.
isMaxLength(max, () => message, value)
Passed value
must be a string shorther or with length equal to max
.
isEqualLength(equal, () => message, value)
Passed value
must be a string shorther or with length equal to max
.
isEmail(() => message, value)
Passed value
must be a valid email. It's a simple check, if you need more complex solution use
isMatching
or isPassing
.
isMatching(regexp, () => message, value)
Passed value
must pass regexp
.
isPassing(predicate, () => message, value)
Passed predicate
answers on "Is value
valid?". When predicate
returns true
validator passes,
when predicate
returns false
error message is returned.
It makes policeman
compatible with all available validators i.e. validator.
import validator from "validator";
import { isPassing } from "policeman";
const isCreditCard = isPassing(validator.isCreditCard, () => "is invalid credit card");
const isUUID4 = isPassing(value => validator.isUUID(value, 4), () => "is invalid UUID v4");
const isFTP = isPassing(value => validator.isURL(value, { protocols: ["ftp"] }, () => "is invalid FTP address");
See tests for more examples.
0.2.4
FAQs
Lightweight yet powerful schema validator
The npm package policeman receives a total of 1 weekly downloads. As such, policeman popularity was classified as not popular.
We found that policeman demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.