Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
twilio-sdk-type-validator
Advanced tools
Provides runtime type validation functionality for class methods and functions.
Most of the time, using a decorator is the most convenient way to apply type checking to a class method:
import { validateTypesAsync } from "twilio-sdk-type-validator";
class TestClass {
@validateTypes("number")
someMethod(value) {
console.log(`value is a number: ${value}`);
}
}
Or if it is an async method, you could use an async version of the decorator, in which case the type error will be thrown as a rejected promise:
import { validateTypesAsync } from "twilio-sdk-type-validator";
class TestClass {
@validateTypesAsync("number")
async someMethod(value) {
console.log(`value is a number: ${value}`);
}
}
Each argument passed into the decorator should be a valid rule that corresponds to the respective argument of the method being decorated. If an argument requires more than one check, you can pass an array of rules which will get applied following the OR logic:
import { validateTypesAsync } from "twilio-sdk-type-validator";
class TestClass {
@validateTypes(["number", "string"])
someMethod(value) {
console.log(`value is either a number or a string: ${value}`);
}
@validateTypes("string", ["number", "boolean"])
someOtherMethod(value, secondValue) {
console.log(`value is a string: ${value}`);
console.log(`secondValue is either a number or a boolean: ${secondValue}`);
}
}
For functions outside of classes, you can utilize runtimeTypeValidation
function instead. It accepts two arguments, first being the array of rules (each
element of the array corresponding to a rule (or an array of rules) for a
respective argument), with the second being the arguments to validate.
Here's an example of the two methods from previous example rewritten using the
runtimeTypeValidation
function:
import { runtimeTypeValidation } from "twilio-sdk-type-validator";
function someFunction(value) {
runtimeTypeValidation(["number"], [value]);
console.log(`value is a number: ${value}`);
}
function someOtherFunction(value, secondValue) {
runtimeTypeValidation(
["string", ["number", "boolean"]],
[value, secondValue]
);
console.log(`value is a string: ${value}`);
console.log(`secondValue is either a number or a boolean: ${secondValue}`);
}
Type validator comes with a few prebuilt rules. They come in two shapes: rule factories and rule constants.
Rule factories are functions which generate an object describing the validation
rule. Every single rule factory accepts one or multiple arguments. If multiple
arguments are passed, then they will be applied following the OR logic.
The rule factory objectSchema
is an exception. The rule factories are:
type
literal
custom
objectSchema
Rule constants are pre-made rule objects which don't require any parameters. The rule constants are:
nonEmptyString
nonNegativeInteger
pureObject
type
Validates against either a primitive type (typeof x
) or a constructor
function (instanceof x
).
Using the type
rule factory itself could be omitted if a string representing a
primitive type or a constructor function is passed as a rule itself.
@validateTypes(type("number"))
// OR
@validateTypes("number")
@validateTypes(["number", "string"])
// OR
@validateTypes(type("number", "string"))
// OR
@validateTypes([type("number"), type("string")])
@validateTypes(FormData)
@validateTypes([FormData, Blob])
// OR
@validateTypes(type(FormData, Blob))
// OR
@validateTypes([type(FormData), type(Blob)])
literal
Validates against a literal value (compared using the strict equality operator
===
).
@validateTypes(literal("foobar"))
@validateTypes(literal("foobar", 15))
// OR
@validateTypes([literal("foobar"), literal(15)])
custom
Validates against a custom rule. The custom rule is represented as a function
that returns a tuple of type [boolean, string]
where the boolean is whether
the check has passed and the string is the description of the rule. The value
to validate gets passed as the first argument.
@validateTypes(
custom((value) => [
typeof value === "number" && value > 15,
"a number greater than 15",
])
)
nonEmptyString
Validates a non-empty string.
@validateTypes(nonEmptyString)
nonNegativeInteger
Validates a non-negative integer. I.E., it should contain no decimal point and be greater than or equal to 0.
@validateTypes(nonNegativeInteger)
pureObject
Validates an object that is not null
and is not an array.
@validateTypes(pureObject)
objectSchema
Validates an object against a schema. The first argument is a short description of the object (which will appear in runtime type errors) with the second argument being the schema itself. The rule will validates every key of the schema object against a rule (or a set of rules) defined as values.
@validateTypes(
objectSchema("network configuration", {
port: "number",
protocol: literal("http", "https"),
retries: custom((value) => [
typeof value === "number" && value > 0 && value < 16,
"a value between 0 (exclusive) and 15 (inclusive)"
])
})
)
@validateTypes(
objectSchema("root object", {
foo: ["boolean", "number"],
bar: objectSchema("child object", {
baz: "number",
}),
})
)
FAQs
Runtime type validator for JS Messaging SDKs
The npm package twilio-sdk-type-validator receives a total of 12,653 weekly downloads. As such, twilio-sdk-type-validator popularity was classified as popular.
We found that twilio-sdk-type-validator 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
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.