@cofondateurauchomage/libs 1.0.39 → 1.0.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/db.model.d.ts +6 -6
- package/lib/db.model.js +0 -6
- package/lib/index.d.ts +5 -3
- package/lib/index.js +19 -7
- package/lib/regex.d.ts +23 -0
- package/lib/regex.js +34 -0
- package/lib/validate.d.ts +85 -0
- package/lib/validate.js +142 -0
- package/package.json +1 -1
package/lib/db.model.d.ts
CHANGED
|
@@ -12,19 +12,19 @@ export interface IUser {
|
|
|
12
12
|
business: string;
|
|
13
13
|
city: string;
|
|
14
14
|
email: string;
|
|
15
|
-
lastName: string;
|
|
16
15
|
firstName: string;
|
|
17
16
|
invest?: number;
|
|
18
17
|
joblessDate: string;
|
|
19
|
-
|
|
18
|
+
lastName: string;
|
|
20
19
|
linkedin?: string;
|
|
21
20
|
motivations: string;
|
|
22
21
|
partner: string;
|
|
23
22
|
skills: TSkill[];
|
|
24
23
|
tel?: string;
|
|
25
24
|
zipCode: number;
|
|
26
|
-
creationDate: number;
|
|
27
25
|
clicks: IClicks;
|
|
26
|
+
creationDate: number;
|
|
27
|
+
likes: string[];
|
|
28
28
|
}
|
|
29
29
|
export interface IProject {
|
|
30
30
|
city: string;
|
|
@@ -33,15 +33,15 @@ export interface IProject {
|
|
|
33
33
|
firstName: string;
|
|
34
34
|
invest?: number;
|
|
35
35
|
lastName: string;
|
|
36
|
-
likes: string[];
|
|
37
36
|
linkedin?: string;
|
|
38
37
|
name: string;
|
|
38
|
+
partner: string;
|
|
39
39
|
skills: TSkill[];
|
|
40
40
|
tel?: string;
|
|
41
41
|
zipCode: number;
|
|
42
|
-
partner: string;
|
|
43
42
|
active: boolean;
|
|
44
|
-
creationDate: number;
|
|
45
43
|
clicks: IClicks;
|
|
44
|
+
creationDate: number;
|
|
45
|
+
likes: string[];
|
|
46
46
|
stripeId?: string;
|
|
47
47
|
}
|
package/lib/db.model.js
CHANGED
|
@@ -1,8 +1,2 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Collections = void 0;
|
|
4
|
-
var Collections;
|
|
5
|
-
(function (Collections) {
|
|
6
|
-
Collections["users"] = "users";
|
|
7
|
-
Collections["projects"] = "projects";
|
|
8
|
-
})(Collections = exports.Collections || (exports.Collections = {}));
|
package/lib/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
export
|
|
1
|
+
export * from "./api";
|
|
2
|
+
export * from "./const";
|
|
3
|
+
export * from "./db.model";
|
|
4
|
+
export * from "./regex";
|
|
5
|
+
export * from "./validate";
|
package/lib/index.js
CHANGED
|
@@ -1,9 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
2
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
var regex_1 = require("./utils/regex");
|
|
9
|
-
Object.defineProperty(exports, "RGX", { enumerable: true, get: function () { return regex_1.RGX; } });
|
|
17
|
+
__exportStar(require("./api"), exports);
|
|
18
|
+
__exportStar(require("./const"), exports);
|
|
19
|
+
__exportStar(require("./db.model"), exports);
|
|
20
|
+
__exportStar(require("./regex"), exports);
|
|
21
|
+
__exportStar(require("./validate"), exports);
|
package/lib/regex.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export declare const RGX: {
|
|
2
|
+
Date: {
|
|
3
|
+
regex: RegExp;
|
|
4
|
+
};
|
|
5
|
+
Email: {
|
|
6
|
+
regex: RegExp;
|
|
7
|
+
};
|
|
8
|
+
Linkedin: {
|
|
9
|
+
regex: RegExp;
|
|
10
|
+
pattern: string;
|
|
11
|
+
message: string;
|
|
12
|
+
};
|
|
13
|
+
Name: {
|
|
14
|
+
regex: RegExp;
|
|
15
|
+
pattern: string;
|
|
16
|
+
message: string;
|
|
17
|
+
};
|
|
18
|
+
Tel: {
|
|
19
|
+
regex: RegExp;
|
|
20
|
+
pattern: string;
|
|
21
|
+
message: string;
|
|
22
|
+
};
|
|
23
|
+
};
|
package/lib/regex.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RGX = void 0;
|
|
4
|
+
const regex = {
|
|
5
|
+
date: /^([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))$/,
|
|
6
|
+
email: /^[\w.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/,
|
|
7
|
+
linkedin: /^(http(s)?:\/\/)?([\w]+\.)?linkedin\.com\/(pub|in|profile)\/.+$/,
|
|
8
|
+
name: /^[A-Za-zÜ-ü\s\-]{1,50}$/,
|
|
9
|
+
tel: /^([+][1-9]{2,3}[ .\-]?)?[0-9]{1,3}([ .\-]?[0-9]{2,3}){3,6}$/,
|
|
10
|
+
};
|
|
11
|
+
const pattern = (regex) => String(regex).replace("/^", "").replace("$/", "");
|
|
12
|
+
exports.RGX = {
|
|
13
|
+
Date: {
|
|
14
|
+
regex: regex.date,
|
|
15
|
+
},
|
|
16
|
+
Email: {
|
|
17
|
+
regex: regex.email,
|
|
18
|
+
},
|
|
19
|
+
Linkedin: {
|
|
20
|
+
regex: regex.linkedin,
|
|
21
|
+
pattern: pattern(regex.linkedin),
|
|
22
|
+
message: `Le lien doit être un URL LinkedIn valide. (ex: https://www.linkedin.com/in/valentin-lemaire/)`,
|
|
23
|
+
},
|
|
24
|
+
Name: {
|
|
25
|
+
regex: regex.name,
|
|
26
|
+
pattern: pattern(regex.name),
|
|
27
|
+
message: `Accepte les noms composés (espace ou -) et les accents jusqu'à 50 caractères. Les caractères spéciaux et les chiffres ne sont pas autorisés.`,
|
|
28
|
+
},
|
|
29
|
+
Tel: {
|
|
30
|
+
regex: regex.tel,
|
|
31
|
+
pattern: pattern(regex.tel),
|
|
32
|
+
message: `Veuillez entrer un numéro de téléphone valide.`,
|
|
33
|
+
},
|
|
34
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { BodyForO, CloudFunctionNames, RouteNames } from "./api";
|
|
2
|
+
export type ValidatorFn = (value: unknown) => boolean;
|
|
3
|
+
/**
|
|
4
|
+
* Class representing a custom validator.
|
|
5
|
+
*/
|
|
6
|
+
export declare class Validator {
|
|
7
|
+
private fn;
|
|
8
|
+
/**
|
|
9
|
+
* Flag indicating if the field is required.
|
|
10
|
+
* @private
|
|
11
|
+
*/
|
|
12
|
+
private required;
|
|
13
|
+
private min?;
|
|
14
|
+
private max?;
|
|
15
|
+
/**
|
|
16
|
+
* Create a new Validator.
|
|
17
|
+
* @param fn - The validator function for validate the value.
|
|
18
|
+
*/
|
|
19
|
+
constructor(fn: ValidatorFn);
|
|
20
|
+
/**
|
|
21
|
+
* Method to validate the value based on the validator function and the chainable methods called prior.
|
|
22
|
+
* @param value - Value to validate.
|
|
23
|
+
* @returns
|
|
24
|
+
*/
|
|
25
|
+
validate(value: any): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Validator function is called even if the value is null.
|
|
28
|
+
*/
|
|
29
|
+
isRequired(): this;
|
|
30
|
+
/**
|
|
31
|
+
* Sets the minimum allowable value or length.
|
|
32
|
+
* @param min - Minimum value for the length of the value or the value itself.
|
|
33
|
+
* @returns
|
|
34
|
+
*/
|
|
35
|
+
setMin(min: number): this;
|
|
36
|
+
/**
|
|
37
|
+
* Sets the maximum allowable value or length.
|
|
38
|
+
* @param max - Maximum value for the length of the value or the value itself.
|
|
39
|
+
* @returns
|
|
40
|
+
*/
|
|
41
|
+
setMax(max: number): this;
|
|
42
|
+
private checkMinMax;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Creates a new validator that tests a string value against a given regex pattern.
|
|
46
|
+
* @param pattern - The regex pattern to test.
|
|
47
|
+
* @returns
|
|
48
|
+
*/
|
|
49
|
+
export declare const setRegex: (pattern: RegExp) => Validator;
|
|
50
|
+
/**
|
|
51
|
+
* Create a new validator for a number.
|
|
52
|
+
*/
|
|
53
|
+
export declare const isNumber: Validator;
|
|
54
|
+
/**
|
|
55
|
+
* Create a new validator for a string.
|
|
56
|
+
*/
|
|
57
|
+
export declare const isString: Validator;
|
|
58
|
+
/**
|
|
59
|
+
* Create a new validator for a boolean.
|
|
60
|
+
*/
|
|
61
|
+
export declare const isBoolean: Validator;
|
|
62
|
+
/**
|
|
63
|
+
* Create a new validator for an array.
|
|
64
|
+
*/
|
|
65
|
+
export declare const isArray: Validator;
|
|
66
|
+
/**
|
|
67
|
+
* Custom error class to handle validation errors.
|
|
68
|
+
*/
|
|
69
|
+
export declare class ValidationError extends Error {
|
|
70
|
+
code: number;
|
|
71
|
+
/**
|
|
72
|
+
* Creates a new ValidationError.
|
|
73
|
+
* @param code - The HTTP status code.
|
|
74
|
+
* @param message - The error message.
|
|
75
|
+
*/
|
|
76
|
+
constructor(code: number, message: string);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Validate the request body against the provided validators.
|
|
80
|
+
* @param body - Body to validate.
|
|
81
|
+
* @param validators - validator for each key of the body.
|
|
82
|
+
* @returns {BodyForO<O>} - The validated body.
|
|
83
|
+
* @throws {ValidationError} - Throws a ValidationError exception if validation fails.
|
|
84
|
+
*/
|
|
85
|
+
export declare const validateBodyForO: <O extends CloudFunctionNames | RouteNames>(body: unknown, validators: Record<keyof BodyForO<O>, Validator>) => BodyForO<O>;
|
package/lib/validate.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateBodyForO = exports.ValidationError = exports.isArray = exports.isBoolean = exports.isString = exports.isNumber = exports.setRegex = exports.Validator = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Class representing a custom validator.
|
|
6
|
+
*/
|
|
7
|
+
class Validator {
|
|
8
|
+
fn;
|
|
9
|
+
/**
|
|
10
|
+
* Flag indicating if the field is required.
|
|
11
|
+
* @private
|
|
12
|
+
*/
|
|
13
|
+
required = false;
|
|
14
|
+
min;
|
|
15
|
+
max;
|
|
16
|
+
/**
|
|
17
|
+
* Create a new Validator.
|
|
18
|
+
* @param fn - The validator function for validate the value.
|
|
19
|
+
*/
|
|
20
|
+
constructor(fn) {
|
|
21
|
+
this.fn = fn;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Method to validate the value based on the validator function and the chainable methods called prior.
|
|
25
|
+
* @param value - Value to validate.
|
|
26
|
+
* @returns
|
|
27
|
+
*/
|
|
28
|
+
validate(value) {
|
|
29
|
+
if (value == null && !this.required) {
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
if (!this.fn(value)) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
if (this.min !== undefined || this.max !== undefined) {
|
|
36
|
+
return this.checkMinMax(value, this.min, this.max);
|
|
37
|
+
}
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Validator function is called even if the value is null.
|
|
42
|
+
*/
|
|
43
|
+
isRequired() {
|
|
44
|
+
this.required = true;
|
|
45
|
+
return this;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Sets the minimum allowable value or length.
|
|
49
|
+
* @param min - Minimum value for the length of the value or the value itself.
|
|
50
|
+
* @returns
|
|
51
|
+
*/
|
|
52
|
+
setMin(min) {
|
|
53
|
+
this.min = min;
|
|
54
|
+
return this;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Sets the maximum allowable value or length.
|
|
58
|
+
* @param max - Maximum value for the length of the value or the value itself.
|
|
59
|
+
* @returns
|
|
60
|
+
*/
|
|
61
|
+
setMax(max) {
|
|
62
|
+
this.max = max;
|
|
63
|
+
return this;
|
|
64
|
+
}
|
|
65
|
+
checkMinMax(input, min, max) {
|
|
66
|
+
if (typeof input !== "string" && typeof input !== "number") {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
const nb = typeof input === "string" ? input.length : input;
|
|
70
|
+
if (min !== undefined && nb < min) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
if (max !== undefined && nb > max) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
exports.Validator = Validator;
|
|
80
|
+
/**
|
|
81
|
+
* Creates a new validator that tests a string value against a given regex pattern.
|
|
82
|
+
* @param pattern - The regex pattern to test.
|
|
83
|
+
* @returns
|
|
84
|
+
*/
|
|
85
|
+
const setRegex = (pattern) => new Validator((value) => typeof value === "string" && pattern.test(value));
|
|
86
|
+
exports.setRegex = setRegex;
|
|
87
|
+
/**
|
|
88
|
+
* Create a new validator for a number.
|
|
89
|
+
*/
|
|
90
|
+
exports.isNumber = new Validator((value) => typeof value === "number");
|
|
91
|
+
/**
|
|
92
|
+
* Create a new validator for a string.
|
|
93
|
+
*/
|
|
94
|
+
exports.isString = new Validator((value) => typeof value === "string");
|
|
95
|
+
/**
|
|
96
|
+
* Create a new validator for a boolean.
|
|
97
|
+
*/
|
|
98
|
+
exports.isBoolean = new Validator((value) => typeof value === "boolean");
|
|
99
|
+
/**
|
|
100
|
+
* Create a new validator for an array.
|
|
101
|
+
*/
|
|
102
|
+
exports.isArray = new Validator((value) => Array.isArray(value));
|
|
103
|
+
/////////////
|
|
104
|
+
// Validate
|
|
105
|
+
/**
|
|
106
|
+
* Custom error class to handle validation errors.
|
|
107
|
+
*/
|
|
108
|
+
class ValidationError extends Error {
|
|
109
|
+
code;
|
|
110
|
+
/**
|
|
111
|
+
* Creates a new ValidationError.
|
|
112
|
+
* @param code - The HTTP status code.
|
|
113
|
+
* @param message - The error message.
|
|
114
|
+
*/
|
|
115
|
+
constructor(code, message) {
|
|
116
|
+
super(message);
|
|
117
|
+
this.code = code;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
exports.ValidationError = ValidationError;
|
|
121
|
+
/**
|
|
122
|
+
* Validate the request body against the provided validators.
|
|
123
|
+
* @param body - Body to validate.
|
|
124
|
+
* @param validators - validator for each key of the body.
|
|
125
|
+
* @returns {BodyForO<O>} - The validated body.
|
|
126
|
+
* @throws {ValidationError} - Throws a ValidationError exception if validation fails.
|
|
127
|
+
*/
|
|
128
|
+
const validateBodyForO = (body, validators) => {
|
|
129
|
+
if (typeof body !== "object" || body === null) {
|
|
130
|
+
throw new ValidationError(400, "Body non fourni ou invalide");
|
|
131
|
+
}
|
|
132
|
+
const result = {};
|
|
133
|
+
for (const [key, validator] of Object.entries(validators)) {
|
|
134
|
+
const value = body[key];
|
|
135
|
+
if (!validator.validate(value)) {
|
|
136
|
+
throw new ValidationError(400, `${key} non fourni ou invalide`);
|
|
137
|
+
}
|
|
138
|
+
result[key] = value;
|
|
139
|
+
}
|
|
140
|
+
return result;
|
|
141
|
+
};
|
|
142
|
+
exports.validateBodyForO = validateBodyForO;
|