Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
TypeScript Declarative Validation. Decorate your class properties to validate them using Joi.
This lib allows you to apply Joi validation constraints on class properties, by using decorators.
This means you can combine your type schema and your validation schema in one go!
Calling Validator.validateAsClass()
allows you to validate any object as if it were an instance of a given class.
npm add joiful reflect-metadata
Or
yarn add joiful reflect-metadata
.
You must enable experimental decorators and metadata in your TypeScript configuration.
tsconfig.json
{
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true
}
}
Ensure you import reflect-metadata
as the first import in your application's entry point.
index.ts
import 'reflect-metadata';
...
Then you can start using joiful like this.
import * as jf from 'joiful';
class SignUp {
@jf.string().required()
username: string;
@jf
.string()
.required()
.min(8)
password: string;
@jf.date()
dateOfBirth: Date;
@jf.boolean().required()
subscribedToNewsletter: boolean;
}
const signUp = new SignUp();
signUp.username = 'rick.sanchez';
signUp.password = 'wubbalubbadubdub';
const { error } = jf.validate(signUp);
console.log(error); // Error will either be undefined or a standard joi validation error
Don't like creating instances of classes? Don't worry, you don't have to. You can validate a plain old javascript object as if it were an instance of a class.
const signUp = {
username: 'rick.sanchez',
password: 'wubbalubbadubdub',
};
const result = jf.validateAsClass(signUp, SignUp);
Want to create your own shorthand versions of decorators? Simply create a function like below.
customDecorators.ts
import * as jf from 'joiful';
const password = () =>
jf
.string()
.min(8)
.regex(/[a-z]/)
.regex(/[A-Z]/)
.regex(/[0-9]/)
.required();
changePassword.ts
import { password } from './customDecorators';
class ChangePassword {
@password()
newPassword: string;
}
class SimpleTodoList {
@jf.array().items(joi => joi.string())
todos?: string[];
}
To validate an array of objects that have their own joiful validation:
class Actor {
@string().required()
name!: string;
}
class Movie {
@string().required()
name!: string;
@array({ elementClass: Actor }).required()
actors!: Actor[];
}
To validate an object subproperty that has its own joiful validation:
class Address {
@string()
line1?: string;
@string()
line2?: string;
@string().required()
city!: string;
@string().required()
state!: string;
@string().required()
country!: string;
}
class Contact {
@string().required()
name!: string;
@object().optional()
address?: Address;
}
The joiful API is designed to closely match the joi API. One exception is validating the length of a string
, array
, etc, which is performed using .exactLength(n)
rather than .length(n)
. If you're familiar with the joi API, you should find joiful very easy to pickup.
If there's something you're not sure of you can see how it's done by looking at the unit tests. There is 100% coverage so most likely you'll find your scenario there. Otherwise feel free to open an issue.
Got an issue or a feature request? Log it.
Pull-requests are also very welcome.
FAQs
TypeScript Declarative Validation. Decorate your class properties to validate them using Joi.
The npm package joiful receives a total of 5,447 weekly downloads. As such, joiful popularity was classified as popular.
We found that joiful demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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 researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.