valia
v4.0.10
Published
Validation library for TypeScript and JavaScript.
Maintainers
Readme
🔌 S’intègre naturellement à vos projets, qu’ils soient front-end ou back-end, et permet de définir des schémas de manière intuitive tout en favorisant leur réutilisation.
💡 Pensée pour allier simplicité et puissance, elle propose des fonctionnalités avancées comme l’inférence de types, ainsi que des validateurs standards tels que isEmail, isUuid ou isIp.
Table des matières
Installation
> npm install valiaimport { Schema } from 'valia';
const userSchema = new Schema({
type: "object",
shape: {
name: { type: "string" },
role: {
type: "string",
literal: ["WORKER", "CUSTOMER"]
}
}
});
let data = {
name: "Alice",
role: "WORKER"
};
if (userSchema.validate(data)) {
console.log(data.name, data.role);
}import type { SchemaInfer } from 'valia';
type User = SchemaInfer<typeof userSchema>;Schema
Instances
Schema
SchemaException
SchemaNodeException
SchemaDataRejection
SchemaDataAdmission
Formats
Number • String • Boolean • Object • Array • Function • Symbol • Union • Null • Undefined
Les formats représentent les noeuds de critères qui pourront être utilisés dans les schémas. L'ordre des propriétés décrites pour chaque formats respecte l'ordre de validation.
Global
Propriétés :
Number
Propriétés :
Exemples :
Validé n'importe quel nombre
const schema = new Schema({
type: "number"
});
✅ schema.validate(0);
✅ schema.validate(10);
✅ schema.validate(-10);Valide des nombres qui appartiennent à une plage spécifique
const schema = new Schema({
type: "number",
min: 0,
max: 10
});
✅ schema.validate(0);
✅ schema.validate(10);
❌ schema.validate(-1);
❌ schema.validate(-10);Validé un nombre spécifique
const schema = new Schema({
type: "number",
literal: 141
});
✅ schema.validate(141);
❌ schema.validate(-1);
❌ schema.validate(-10);Validé des nombres spécifique avec un tableau
const schema = new Schema({
type: "number",
literal: [141, 282]
});
✅ schema.validate(141);
✅ schema.validate(282);
❌ schema.validate(0);
❌ schema.validate(100);
❌ schema.validate(200);String
Propriétés :
Exemples :
Validé n'importe quel chaîne de caractères
const schema = new Schema({
type: "string"
});
✅ schema.validate("");
✅ schema.validate("abc");Validé des chaînes de caractères ayant une longueur spécifique
const schema = new Schema({
type: "string",
min: 3,
max: 3
});
✅ schema.validate("abc");
❌ schema.validate("");
❌ schema.validate("a");
❌ schema.validate("abcd");Validé des chaînes de caractères avec une expression régulière
const schema = new Schema({
type: "string",
regex: /^#[a-fA-F0-9]{6}$/
});
✅ schema.validate("#000000");
✅ schema.validate("#FFFFFF");
❌ schema.validate("");
❌ schema.validate("#000");
❌ schema.validate("#FFF");Validé une chaîne de caractères spécifique
const schema = new Schema({
type: "string",
literal: "ABC"
});
✅ schema.validate("ABC");
❌ schema.validate("");
❌ schema.validate("a");
❌ schema.validate("abc");Validé des chaînes de caractères spécifique avec un tableau
const schema = new Schema({
type: "string",
literal: ["ABC", "XYZ"]
});
✅ schema.validate("ABC");
✅ schema.validate("XYZ");
❌ schema.validate("");
❌ schema.validate("a");
❌ schema.validate("abc");Validé des chaînes de caractères avec un testeur de chaîne
const schema = new Schema({
type: "string",
constraint: {
idIp: { cidr: true }
}
});
✅ schema.validate("127.0.0.1/24");
❌ schema.validate("");
❌ schema.validate("127.0.0.1");Validé des chaînes de caractères avec plusieurs testeurs de chaîne
const schema = new Schema({
type: "string",
constraint: {
isEmail: true,
idIp: { cidr: true }
}
});
✅ schema.validate("foo@bar");
✅ schema.validate("127.0.0.1/24");
❌ schema.validate("");
❌ schema.validate("foo@");
❌ schema.validate("127.0.0.1");Boolean
Propriétés :
Exemples :
Validé n'importe quel booléen
const schema = new Schema({
type: "boolean"
});
✅ schema.validate(true);
✅ schema.validate(false);
❌ schema.validate("");
❌ schema.validate({});Validé un booléen avec un état spécifique
const schema = new Schema({
type: "boolean",
literal: true
});
✅ schema.validate(true);
❌ schema.validate("");
❌ schema.validate({});
❌ schema.validate(false);Object
Propriétés :
Exemples :
Validé n'importe quel objet
const schema = new Schema({
type: "object"
});
✅ schema.validate({});
✅ schema.validate([]);
✅ schema.validate(new Date());
✅ schema.validate(Object.create(null));
❌ schema.validate("");Validé un objet de nature simple
const schema = new Schema({
type: "object",
nature: "PLAIN"
});
✅ schema.validate({});
✅ schema.validate(Object.create(null));
❌ schema.validate("");
❌ schema.validate([]);
❌ schema.validate(new Date());Validé un objet avec des propriétés fixes
const schema = new Schema({
type: "object",
shape: {
foo: { type: "string" },
bar: { type: "string" }
}
});
✅ schema.validate({ foo: "x", bar: "x" });
❌ schema.validate({});
❌ schema.validate({ foo: "x" });
❌ schema.validate({ bar: "x" });
❌ schema.validate({ foo: "x", bar: "x", a: "" });Validé un objet et un sous-objet de propriétés fixes
const schema = new Schema({
type: "object",
shape: {
foo: { type: "string" },
bar: { type: "string" },
baz: {
foo: { type: "number" },
bar: { type: "number" }
}
}
});
✅ schema.validate({ foo: "x", bar: "x", baz: { foo: 0, bar: 0 } });
❌ schema.validate({});
❌ schema.validate({ foo: "x" });
❌ schema.validate({ foo: "x", bar: "x" });
❌ schema.validate({ foo: "x", bar: "x", baz: {} });
❌ schema.validate({ foo: "x", bar: "x", baz: { foo: 0 } });Validé un objet avec des propriétés facultatives
const schema = new Schema({
type: "object",
shape: {
foo: { type: "string" },
bar: { type: "string" }
},
optional: true
});
✅ schema.validate({});
✅ schema.validate({ foo: "x" });
✅ schema.validate({ bar: "x" });
✅ schema.validate({ foo: "x", bar: "x" });
❌ schema.validate({ foo: "x", bar: "x", a: "x" });Validé un objet avec une propriété fixe et une propriété facultative
const schema = new Schema({
type: "object",
shape: {
foo: { type: "string" },
bar: { type: "string" }
},
optional: ["bar"]
});
✅ schema.validate({ foo: "x" });
✅ schema.validate({ foo: "x", bar: "x" });
❌ schema.validate({});
❌ schema.validate({ bar: "x" });
❌ schema.validate({ foo: "x", bar: "x", a: "x" });Validé un objet avec des propriétés fixes et des propriétés dynamiques libres
const schema = new Schema({
type: "object",
shape: {
foo: { type: "string" },
bar: { type: "string" }
},
values: { type: "unknown" }
});
✅ schema.validate({ foo: "x", bar: "x" });
✅ schema.validate({ foo: "x", bar: "x", a: "x", b: 0 });
❌ schema.validate({});
❌ schema.validate({ bar: "x" });
❌ schema.validate({ foo: "x" });Validé un objet avec des propriétés fixes et des propriétés dynamiques contraintes
const schema = new Schema({
type: "object",
shape: {
foo: { type: "string" },
bar: { type: "string" }
},
keys: { type: "string" },
values: { type: "number" }
});
✅ schema.validate({ foo: "x", bar: "x" });
✅ schema.validate({ foo: "x", bar: "x", a: 0 });
✅ schema.validate({ foo: "x", bar: "x", a: 0, b: 0 });
❌ schema.validate({});
❌ schema.validate({ foo: "x" });
❌ schema.validate({ bar: "x" });
❌ schema.validate({ foo: "x", bar: "x", a: "x", b: 0 });Validé un objet avec des propriétés dynamiques contraintes
const schema = new Schema({
type: "object",
keys: { type: "string" },
values: { type: "string" }
});
✅ schema.validate({});
✅ schema.validate({ a: "x" });
✅ schema.validate({ a: "x", b: "x" });
❌ schema.validate({ a: 0 });
❌ schema.validate({ a: "x", b: 0 });Array
Propriétés :
Exemples :
Validé n'importe quel tableau
const schema = new Schema({
type: "array"
});
✅ schema.validate([]);
✅ schema.validate(["x"]);
❌ schema.validate({});
❌ schema.validate("x");Validé un tableau d'éléments fixes
const schema = new Schema({
type: "array",
tuple: [
{ type: "string" },
{ type: "string" }
]
});
✅ schema.validate(["x", "x"]);
❌ schema.validate([]);
❌ schema.validate(["x"]);
❌ schema.validate(["x", "x", "x"]);Validé un tableau et un sous-tableau d'éléments fixes
const schema = new Schema({
type: "array",
tuple: [
{ type: "string" },
{ type: "string" },
[
{ type: "number" },
{ type: "number" }
]
]
});
✅ schema.validate(["x", "x", [0, 0]]);
❌ schema.validate([]);
❌ schema.validate(["x"]);
❌ schema.validate(["x", "x", []]);
❌ schema.validate(["x", "x", [0]]);Validé un tableau d'éléments fixes et des éléments dynamiques libres
const schema = new Schema({
type: "array",
tuple: [
{ type: "string" },
{ type: "string" }
],
items: { type: "unknown" }
});
✅ schema.validate(["x", "x"]);
✅ schema.validate(["x", "x", 0]);
✅ schema.validate(["x", "x", ""]);
✅ schema.validate(["x", "x", {}]);
❌ schema.validate([]);
❌ schema.validate(["x"]);
❌ schema.validate([0, "x"]);Validé un tableau d'éléments fixes et des éléments dynamiques contraints
const schema = new Schema({
type: "array",
tuple: [
{ type: "string" },
{ type: "string" }
],
items: { type: "number" }
});
✅ schema.validate(["x"]);
✅ schema.validate(["x", "x"]);
✅ schema.validate(["x", "x", 0]);
✅ schema.validate(["x", "x", 0, 0]);
❌ schema.validate([]);
❌ schema.validate(["x"]);
❌ schema.validate(["x", "x", "x"]);
❌ schema.validate(["x", "x", "x", "x"]);Validé un tableau avec éléments dynamiques contraints
const schema = new Schema({
type: "array",
items: { type: "string" }
});
✅ schema.validate([]);
✅ schema.validate(["x"]);
✅ schema.validate(["x", "x"]);
❌ schema.validate([0]);
❌ schema.validate(["x", 0]);
❌ schema.validate(["x", "x", 0]);Symbol
Propriétés :
Exemples :
Validé n'importe quel symbole
const xSymbol = Symbol("x");
const ySymbol = Symbol("y");
const schema = new Schema({
type: "symbol"
});
✅ schema.validate(xSymbol);
✅ schema.validate(ySymbol);Validé un symbole spécifique
const xSymbol = Symbol("x");
const ySymbol = Symbol("y");
const schema = new Schema({
type: "symbol",
literal: xSymbol
});
✅ schema.validate(xSymbol);
❌ schema.validate(ySymbol);Validé des symboles spécifiques avec un tableau
const xSymbol = Symbol("x");
const ySymbol = Symbol("y");
const zSymbol = Symbol("z");
const schema = new Schema({
type: "symbol",
literal: [xSymbol, ySymbol]
});
✅ schema.validate(xSymbol);
✅ schema.validate(ySymbol);
❌ schema.validate(zSymbol);Validé des symboles spécifiques avec un enum
enum mySymbol {
X = Symbol("x"),
Y = Symbol("y")
};
enum otherSymbol {
Z = Symbol("z")
};
const schema = new Schema({
type: "symbol",
literal: mySymbol
});
✅ schema.validate(mySymbol.X);
✅ schema.validate(mySymbol.Y);
❌ schema.validate(otherSymbol.Z);Union
Propriétés :
Exemples :
const schema = new Schema({
type: "union",
union: [
{ type: "string" },
{ type: "number" }
]
});
✅ schema.validate(0);
✅ schema.validate("");
❌ schema.validate({});Null
Propriétés :
Exemples :
const schema = new Schema({
type: "null"
});
✅ schema.validate(null);
❌ schema.validate(0);
❌ schema.validate("");
❌ schema.validate({});Undefined
Propriétés :
Exemples :
const schema = new Schema({
type: "undefined"
});
✅ schema.validate(undefined);
❌ schema.validate(0);
❌ schema.validate("");
❌ schema.validate({});Exemples
Schéma simple
const user = new Schema({
type: "object",
shape: {
name: {
type: "string",
min: 3,
max: 32
},
role: {
type: "string",
literal: ["WORKER", "CUSTOMER"]
}
}
});
✅ user.validate({
name: "Alice",
role: "WORKER"
});
❌ user.validate({
name: "Alice",
role: "MANAGER"
});Schéma composite
const name = new Schema({
type: "string",
min: 3,
max: 32
});
const role = new Schema({
type: "string",
literal: ["WORKER", "CUSTOMER"]
});
const user = new Schema({
type: "object",
shape: {
name: name.criteria,
role: role.criteria
}
});
✅ user.validate({
name: "Bob",
role: "CUSTOMER"
});
❌ user.validate({
name: "Bob",
role: "MANAGER"
});Schéma composite profond
const name = new Schema({
type: "string",
min: 3,
max: 32
});
const setting = new Schema({
type: "object",
shape: {
theme: {
type: "string",
literal: ["DARK", "LIGHT"]
},
notification: { type: "boolean" }
}
});
const user = new Schema({
type: "object",
object: {
name: name.criteria,
theme: setting.criteria.shape.theme
}
});
✅ user.validate({
name: "Alice",
theme: "DARK"
});
❌ user.validate({
name: "Alice",
theme: "DEFAULT"
});Testers
Object
isObject(value): boolean
Vérifie si la valeur fournie est de type object.
isPlainObject(value): boolean
Vérifie si la valeur fournie est de type object et dont le prototype est soit Object.prototype, soit null. Par exemple les valeurs créées via le littérale {} ou via Object.create(null) font partie des valeurs acceptées.
isArray(value): boolean
Vérifie si la valeur fournie est de type array.
isTypedArray(value): boolean
Vérifie si la valeur fournie est de type array et si elle est une vue sur un ArrayBuffer, à l’exception des DataView.
isFunction(value): boolean
Vérifie si la valeur fournie est de type function.
isBasicFunction(value): boolean
Vérifie si la valeur fournie est de type function et qu'elle n'est pas de nature async, generator ou async generator.
isAsyncFunction(value): boolean
Vérifie si la valeur fournie est de type function et qu'elle n'est pas de nature basic, generator ou async generator.
isGeneratorFunction(value): boolean
Vérifie si la valeur fournie est de type function et qu'elle n'est pas de nature basic, async ou async generator.
isAsyncGeneratorFunction(value): boolean
Vérifie si la valeur fournie est de type function et qu'elle n'est pas de nature basic, async ou generator.
String
isAscii(str): boolean
Vérifie si la chaîne fournie n'est composée que de caractères ASCII.
isIpV4(str [, options]): boolean
Vérifie si la chaîne fournie correspond à une IPV4.
isIpV6(str [, options]): boolean
Vérifie si la chaîne fournie correspond à une IPV6.
isIp(str [, options]): boolean
Vérifie si la chaîne fournie correspond à une IPV4 ou une IPV6.
Options:
isEmail(str [, options]): boolean
Vérifie si la chaîne fournie correspond à une adresse email.
Options:
Standards: RFC 5321
isDomain(str): boolean
Vérifie si la chaîne fournie correspond un nom de domain.
Standards: RFC 1035
isDataURL(str [, options]): boolean
Vérifie si la chaîne fournie correspond à une DataURL.
Options:
Standards: RFC 2397
isUuid(str [, options]): boolean
Vérifie si la chaîne fournie correspond à un UUID valide.
Options:
Standards: RFC 9562
isBase16(str): boolean
Vérifie si la chaîne fournie correspond à un encodage base16 valide.
Standards: RFC 4648
isBase32(str): boolean
Vérifie si la chaîne fournie correspond à un encodage base32 valide.
Standards: RFC 4648
isBase32Hex(str): boolean
Vérifie si la chaîne fournie correspond à un encodage base32Hex valide.
Standards: RFC 4648
isBase64(str): boolean
Vérifie si la chaîne fournie correspond à un encodage base64 valide.
Standards: RFC 4648
isBase64Url(str): boolean
Vérifie si la chaîne fournie correspond à un encodage base64Url valide.
Standards: RFC 4648
Helpers
Object
getInternalTag(target): string
Retourne le tag interne de la cible. Par exemple pour une cible async () => {} le tag retourné est "AsyncFunction".
String
base16ToBase32(str [, to, padding]): string
Convertie une chaîne en base16 en une chaîne en base32 ou base32Hex.
Arguments:
Standards: RFC 4648
base16ToBase64(str [, to, padding]): string
Convertie une chaîne en base16 en une chaîne en base64 ou base64Url.
Arguments:
Standards: RFC 4648
base32ToBase16(str [, from]): string
Convertie une chaîne en base32 ou base32Hex en une chaîne en base16.
Arguments:
Standards: RFC 4648
base64ToBase16(str [, from]): string
Convertie une chaîne en base64 ou base64Url en une chaîne en base16.
Arguments:
