
Research
Malicious npm Packages Impersonate Flashbots SDKs, Targeting Ethereum Wallet Credentials
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
fastest-validator-decorators
Advanced tools
Decorators for fastest-validator
:boom: Coming from 1.x ? See the Migration Guide.
import {
Schema,
Array,
Nested,
UUID,
Enum,
Email,
Number,
getSchema,
validate,
validateOrReject
} from "fastest-validator-decorators";
@Schema({
strict: true
})
class Entity1 {
@Array({ items: "string"})
prop1: string[];
}
@Schema()
class Entity2 {
@UUID()
prop1: string;
@Enum({ values : ["one", "two"] })
prop2: "one" | "two";
@Email()
prop3;
@Number({ positive: true })
prop4: number;
@Nested()
prop5: Entity1;
}
const schema = getSchema(Entity2); // get the fastest-validator schema
{
$$strict: false,
prop1: { type: "uuid" },
prop2: { type: "enum", values: ["one", "two"] },
prop3: { type: "email" },
prop4: { type: "number", positive: true },
prop5: { type: "object", strict: true, props: {
prop1: { type: "array", items: "string" }
}}
}
const entity = new Entity2();
entity.prop1 = "thiswillfail";
entity.prop2 = "one";
entity.prop3 = "some@email.com";
entity.prop4 = -10;
entity.prop5 = new Entity1();
const result = validate(entity); // returns true or fastest-validator errors
const result = await validateOrReject(entity); // returns true or throws fastest-validator errors
Install the package
npm install --save fastest-validator-decorators fastest-validator
:sparkles: fastest-validator is a peerDependency.
Add the following to your tsconfig.json
"experimentalDecorators": true
"emitDecoratorMetadata": true
All decorators accept an object of options that apply to the type being used, for a full list of options please refer to the fastest-validator documentation.
@Schema({ strict = false, async = false }, messages={}) - Schema decorator.
@Field({}) - Generic decorator, no default properties set. Will apply all options to the schema.
@String({}) - Applies { type: "string" }
@Boolean({}) - Applies { type: "boolean" }
@Number({}) - Applies { type: "number" }
@UUID({}) - Applies { type: "uuid" }
@ObjectId({}) - Applies { type: "objectid" }
@Email({}) - Applies { type: "email" }
@Date({}) - Applies { type: "date" }
@Enum({}) - Applies { type: "enum" }
@Array({}) - Applies { type: "array" }
@Equal({}) - Applies { type: "equal" }
@Instance({}) - Applies { type: "class" }
@Currency({}) - Applies { type: "currency" }
@Func({}) - Applies { type: "function" }
@Luhn({}) - Applies { type: "luhn" }
@Mac({}) - Applies { type: "mac" }
@Url({}) - Applies { type: "url" }
@Any({}) - Applies { type: "any" }
@Multi({}) - Applies { type: "multi" }
Also resolves to multi if multiple decorators are stacked on a single field.
@String()
@Number()
prop1: string | number;
@Nested({}) - Applies { type: "object", props: {} } (The props are gathered from the nested schema)
@NestedArray({ validator: <ValidatorSchema>
}) - Applies { type: "array", "items": { type :"object", props: { ... } }} (The props are gathered from the nested schema)
@Custom({}) - Applies { type: "custom" }
@Custom({
check (value: number, errors: {type: string, actual: number}[]){
if (value % 2 !== 0) {
errors.push({ type: "even", actual : value });
}
return value;
}
})
In order to a schema to be async , you must add the async: true
to SchemaOptions.
:warning: Be carefull, when enabling async option, the return of the validate function becomes a Promise.
@Schema({
async: true,
strict: true
})
class User {
@Custom({
async check (value: string, errors: {type: string, actual: string}[]) {
const isUserInDB = await db.checkUserName(value);
if (isUserInDB) {
errors.push({ type: "unique", actual : value });
}
return value;
},
})
username!: string;
}
const user = new User();
user.username = "James Bond";
const result = await validate(user);
getSchema() - Returns the fastest-validator schema for a given class
validate() - Returns true or fastest-validator errors for a given instance
validateOrReject() - Returns true or throws fastest-validator errors for a given instance
Licensed under the MIT license.
FAQs
Fastest validator decorators
The npm package fastest-validator-decorators receives a total of 359 weekly downloads. As such, fastest-validator-decorators popularity was classified as not popular.
We found that fastest-validator-decorators demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 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
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
Security News
Ruby maintainers from Bundler and rbenv teams are building rv to bring Python uv's speed and unified tooling approach to Ruby development.
Security News
Following last week’s supply chain attack, Nx published findings on the GitHub Actions exploit and moved npm publishing to Trusted Publishers.