Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Battle-tested runtime type checker for Typescript using JSON Schema type guards
Battle-tested runtime type checker for Typescript using JSON Schema type guards.
All you have to do is installing the package:
$ npm i runtypor
Here is a simple example of request input validation with Express:
import express from 'express';
import { createRuntype } from '../src/index';
type Car = {
brand: string;
model: string;
manufacturedAt: string;
color: string;
price: number;
}
const validationSchema = {
type: 'object',
properties: {
brand: { type: 'string' },
model: { type: 'string' },
manufacturedAt: { type: 'string', format: 'date-time' },
color: { type: 'string', default: 'green' },
price: { type: 'number' },
},
required: ['brand', 'model'],
};
const carRuntype = createRuntype<Car>(validationSchema);
app.post('/car-purchase', (req: express.Request, res: express.Response): void => {
const car = req.body;
if (!carRuntype.match(car)) {
res.status(400).send(carRuntype.badType.message);
// throw carRuntype.badType; // In some cases, you may throw an error.
} else {
const newPrice = computeReduction(Number.parseInt(car.price, 10)); // OK
// ...
}
});
By default, runtype create a naive clone (JSON.parse(JSON.stringify(value))
) in order to prevent mutation of matched value.
In that example, it would be useful to retrieve the validated and mutated clone with coerced types and default values:
// ...
app.post('/car-purchase', (req: express.Request, res: express.Response): void => {
const car = req.body;
if (!carRuntype.match(car)) {
res.status(400).send(carRuntype.badType.message);
} else {
const validatedCar = carRuntype.validatedValue;
const newPrice = computeReduction(validatedCar.price);
carBuilder.setColor(validatedCar.color); // Default to green.
}
});
Note that you should only use the validated value when your data has a simple JSON structure, as it won't replicate prototypal chain or functions for instance.
Do you prefer object notation over functional one ? Use the class instantiation:
import { createRuntype, Runtype } from '../src/index';
// ...
createRuntype<Car>(validationSchema);
// is equivalent to
new Runtype<Car>(validationSchema);
Many npm
scripts are available to help testing:
$ npm run {script}
check
: lint and check unit and integration testslint
: lintlint-fix
: try to fix lint automaticallytest
: execute teststest-coverage
: execure tests with coverage informationstest-watch
: work in TDD !Use npm run check
to check that everything is ok.
If you want to contribute, just fork this repository and make a pull request !
Your development must respect these rules:
You must keep test coverage at 100%.
FAQs
Battle-tested runtime type checker for Typescript using JSON Schema type guards
The npm package runtypor receives a total of 0 weekly downloads. As such, runtypor popularity was classified as not popular.
We found that runtypor 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.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.