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.
happy-chappy
Advanced tools
Happy-chappy is a compact, dependency-free JSON object validator package for JavaScript and TypeScript.
import { Schema, createValidator, validate } from "happy-chappy";
const MY_OBJECT_SCHEMA: Schema = {
object: {
members: {
firstField: "number",
secondField: {
number: {},
optional: true
},
thirdField: "string",
fourthField: {
enum: [1, 2, 3, "four", 5]
}
}
}
};
const validateMyObject = createValidator(MY_OBJECT_SCHEMA);
const myObject = { firstField: 5, thirdField: "hey", fourthField: "four" };
validateMyObject(myObject) === true;
// Or alternatively
validate(myObject, MY_OBJECT_SCHEMA) === true;
Many more examples available in the test folder.
Supported data types are:
Each of these can be further restricted using matcher functions or specific configuration.
Text string validation is supported using multiple types of matcher. A string can be matched by:
Number validation options allow to restrict the available range as follows:
Arrays can be further scoped by setting the following:
Object specification can be customised by allowing extra memebrs not to be taken into account during validation or by specifying a matcher function.
Enumerated values can be any string or number, matched with the strict equality operator.
Matchers allow more complex logic to be included in the validation model. For example:
const isPersonWithAgeTuple = (v: any[]) => (
v.length === 2
&& typeof v[0] === "string"
&& typeof v[1] === "number"
&& Number.isInteger(v[1])
); // Enforces [Name: string, Age: integer]
const schema: Schema = {
array: { matcher: isPersonWithAgeTuple }
};
validate(["Dummy", 5], schema) === true;
validate([5, "Dummy"], schema) === false;
Version 2 brings in typing aids for objects and enumerations to streamline the schema ceration process. When you define a schema for an object or enumeration you can provide a type that will be used to assist in constraining object members and enumerated values.
interface MyRequest {
a: number
b: string
}
const MY_REQUEST_SCHEMA: Schema<MyRequest> = {
object: {
members: {
a: { ... },
b: { ... },
c: { ... }, // TypeScript error!
}
}
};
enum MyEnum {
first = 1,
second = 2
}
const MY_REQUEST_SCHEMA: Schema<MyEnum> = {
enum: [
MyEnum.first,
2,
3 // TypeScript error!
]
};
type MyEnum = "first" | "second";
const MY_REQUEST_SCHEMA: Schema<MyEnum> = {
enum: [
"first",
"second",
"third" // TypeScript error!
]
};
Read the changelog here.
This package is licensed under the ISC license.
You can find the repostiory for this package on GitHub.
FAQs
JSON object validation module
We found that happy-chappy 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.