Sending Session Cookies with Axios
Set cookies with Axios from a browser If you want to use axios to send the browser’s SessionCookie and other information directly to the server, …
This is about class-validator, nodejs.
https://github.com/typestack/class-validator
This is a validation library that is used annotations to set the validation rules in a model class properties.
It is easy to use this library with document. But Unique check was gotcha for me.
For example Hoge class is the model for “hoge” table, the following is to check a email address unique.
import {
validate,
registerDecorator,
ValidationOptions,
ValidatorConstraint,
ValidatorConstraintInterface,
ValidationArguments
} from "class-validator";
export class Hoge {
@Unique({
message: "This email address is already in use"
})
public email: string | null = null;
}
@ValidatorConstraint({async: true})
export class UniqueConstraint implements ValidatorConstraintInterface {
async validate(value: any, args: ValidationArguments) {
// get records that equal the `email` value from DB.
// If exist the `email` value in target table, return false. (unique validation error)
return true
}
}
export function Unique(validationOptions?: ValidationOptions) {
return function (object: Object, propertyName: string) {
registerDecorator({
target: object.constructor,
propertyName: propertyName,
options: validationOptions,
constraints: [],
validator: UniqueConstraint
});
};
}
It can validate a value unique with “validate" method.
const hoge = new Hoge()
hoge.email = '[email protected]' // already using email address
validate(hoge).then(errors => {
// this email address is already in use
});
(Article migrated from another blog)
Set cookies with Axios from a browser If you want to use axios to send the browser’s SessionCookie and other information directly to the server, …
As of April 2022, almost all infrastructure and network engineers have probably never heard of NYM. On the other hand, some of you may have arrived at …