@cofondateurauchomage/libs 1.0.44 → 1.0.45

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.
@@ -0,0 +1,2 @@
1
+ import { BodyForO, CloudFunctionNames, RouteNames } from "./api";
2
+ export declare const ValidateBodyOf: Record<CloudFunctionNames | RouteNames, (body: unknown) => BodyForO<CloudFunctionNames | RouteNames>>;
@@ -0,0 +1,106 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ValidateBodyOf = void 0;
4
+ const const_1 = require("./const");
5
+ const regex_1 = require("./regex");
6
+ const validate_1 = require("./validate");
7
+ //////////
8
+ // Utils
9
+ const business = (0, validate_1.isString)().setMin(10).setMax(300);
10
+ const city = (0, validate_1.isMatchRegex)(regex_1.RGX.Name.regex);
11
+ const description = (0, validate_1.isString)().setMin(10).setMax(500);
12
+ const email = (0, validate_1.isMatchRegex)(regex_1.RGX.Email.regex);
13
+ const firstName = (0, validate_1.isMatchRegex)(regex_1.RGX.Name.regex);
14
+ const invest = (0, validate_1.isNumber)().setMin(0).setMax(1000000).isOptional();
15
+ const lastName = (0, validate_1.isMatchRegex)(regex_1.RGX.Name.regex);
16
+ const linkedin = (0, validate_1.isMatchRegex)(regex_1.RGX.Linkedin.regex);
17
+ const motivations = (0, validate_1.isString)().setMin(10).setMax(500);
18
+ const partner = (0, validate_1.isString)().setMin(10).setMax(300);
19
+ const skills = new validate_1.Validator((value) => value &&
20
+ Array.isArray(value) &&
21
+ value.length >= 1 &&
22
+ value.length <= 2 &&
23
+ value.every((skill) => const_1.SKILLS.includes(skill))
24
+ ? true
25
+ : false);
26
+ const stripeId = (0, validate_1.isString)().setMin(4).setMax(50);
27
+ const tel = (0, validate_1.isMatchRegex)(regex_1.RGX.Tel.regex).isOptional();
28
+ const zipCode = (0, validate_1.isNumber)().setMin(1000).setMax(97680);
29
+ exports.ValidateBodyOf = {
30
+ ///////////////////
31
+ // Cloud functions
32
+ createProjectCall: (body) => (0, validate_1.validateBodyForO)(body, {
33
+ city,
34
+ description,
35
+ email,
36
+ firstName,
37
+ invest,
38
+ lastName,
39
+ linkedin,
40
+ name: (0, validate_1.isString)().setMin(2).setMax(50),
41
+ partner,
42
+ redstart: (0, validate_1.isBoolean)().isOptional(),
43
+ skills,
44
+ tel,
45
+ zipCode,
46
+ }),
47
+ setProjectCall: (body) => (0, validate_1.validateBodyForO)(body, {
48
+ city,
49
+ description,
50
+ invest,
51
+ linkedin,
52
+ partner,
53
+ skills,
54
+ tel,
55
+ zipCode,
56
+ }),
57
+ deleteProjectCall: (body) => (0, validate_1.validateBodyForO)(body, {}),
58
+ setStripeIdProjectCall: (body) => (0, validate_1.validateBodyForO)(body, {
59
+ email,
60
+ stripeId,
61
+ }),
62
+ setActiveProjectCall: (body) => (0, validate_1.validateBodyForO)(body, {
63
+ active: (0, validate_1.isBoolean)(),
64
+ stripeId,
65
+ }),
66
+ createUserCall: (body) => (0, validate_1.validateBodyForO)(body, {
67
+ business,
68
+ city,
69
+ email,
70
+ firstName,
71
+ invest,
72
+ joblessDate: (0, validate_1.isMatchRegex)(regex_1.RGX.Date.regex),
73
+ lastName,
74
+ linkedin,
75
+ motivations,
76
+ partner,
77
+ skills,
78
+ tel,
79
+ zipCode,
80
+ }),
81
+ setUserCall: (body) => (0, validate_1.validateBodyForO)(body, {
82
+ business,
83
+ city,
84
+ invest,
85
+ linkedin,
86
+ motivations,
87
+ partner,
88
+ skills,
89
+ tel,
90
+ zipCode,
91
+ }),
92
+ deleteUserCall: (body) => (0, validate_1.validateBodyForO)(body, {}),
93
+ //////////
94
+ // Routes
95
+ checkout_session: (body) => (0, validate_1.validateBodyForO)(body, {
96
+ stripeId: stripeId.isOptional(),
97
+ email,
98
+ }),
99
+ delete_customer: (body) => (0, validate_1.validateBodyForO)(body, {
100
+ stripeId,
101
+ }),
102
+ portal_session: (body) => (0, validate_1.validateBodyForO)(body, {
103
+ stripeId,
104
+ }),
105
+ webhook: (body) => (0, validate_1.validateBodyForO)(body, {}),
106
+ };
@@ -1,4 +1,5 @@
1
1
  export * from "./api";
2
+ export * from "./api.validate";
2
3
  export * from "./const";
3
4
  export * from "./db.model";
4
5
  export * from "./regex";
@@ -15,6 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./api"), exports);
18
+ __exportStar(require("./api.validate"), exports);
18
19
  __exportStar(require("./const"), exports);
19
20
  __exportStar(require("./db.model"), exports);
20
21
  __exportStar(require("./regex"), exports);
@@ -22,11 +22,11 @@ export declare class Validator {
22
22
  * @param value - Value to validate.
23
23
  * @returns
24
24
  */
25
- validate(value: any): boolean;
25
+ validate(key: string, value: any): boolean;
26
26
  /**
27
- * Validator function is called even if the value is null.
27
+ * The value is not required.
28
28
  */
29
- isRequired(): this;
29
+ isOptional(): this;
30
30
  /**
31
31
  * Sets the minimum allowable value or length.
32
32
  * @param min - Minimum value for the length of the value or the value itself.
@@ -41,28 +41,28 @@ export declare class Validator {
41
41
  setMax(max: number): this;
42
42
  private checkMinMax;
43
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
44
  /**
51
45
  * Create a new validator for a number.
52
46
  */
53
- export declare const isNumber: Validator;
47
+ export declare const isNumber: () => Validator;
54
48
  /**
55
49
  * Create a new validator for a string.
56
50
  */
57
- export declare const isString: Validator;
51
+ export declare const isString: () => Validator;
58
52
  /**
59
53
  * Create a new validator for a boolean.
60
54
  */
61
- export declare const isBoolean: Validator;
55
+ export declare const isBoolean: () => Validator;
62
56
  /**
63
57
  * Create a new validator for an array.
64
58
  */
65
- export declare const isArray: Validator;
59
+ export declare const isArray: () => Validator;
60
+ /**
61
+ * Creates a new validator that tests a string value against a given regex pattern.
62
+ * @param pattern - The regex pattern to test.
63
+ * @returns
64
+ */
65
+ export declare const isMatchRegex: (pattern: RegExp) => Validator;
66
66
  /**
67
67
  * Custom error class to handle validation errors.
68
68
  */
@@ -82,4 +82,4 @@ export declare class ValidationError extends Error {
82
82
  * @returns {BodyForO<O>} - The validated body.
83
83
  * @throws {ValidationError} - Throws a ValidationError exception if validation fails.
84
84
  */
85
- export declare const validateBodyForO: <O extends CloudFunctionNames | RouteNames>(body: unknown, validators: Record<keyof BodyForO<O>, Validator>) => BodyForO<O>;
85
+ export declare function validateBodyForO<O extends CloudFunctionNames | RouteNames>(body: unknown, validators: Record<keyof BodyForO<O>, Validator>): BodyForO<O>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
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;
3
+ exports.validateBodyForO = exports.ValidationError = exports.isMatchRegex = exports.isArray = exports.isBoolean = exports.isString = exports.isNumber = exports.Validator = void 0;
4
4
  /**
5
5
  * Class representing a custom validator.
6
6
  */
@@ -10,7 +10,7 @@ class Validator {
10
10
  * Flag indicating if the field is required.
11
11
  * @private
12
12
  */
13
- required = false;
13
+ required = true;
14
14
  min;
15
15
  max;
16
16
  /**
@@ -25,23 +25,23 @@ class Validator {
25
25
  * @param value - Value to validate.
26
26
  * @returns
27
27
  */
28
- validate(value) {
29
- if (value == null && !this.required) {
28
+ validate(key, value) {
29
+ if (!this.required && !value) {
30
30
  return true;
31
31
  }
32
32
  if (!this.fn(value)) {
33
- return false;
33
+ throw new ValidationError(400, `${key} non fourni ou invalide`);
34
34
  }
35
35
  if (this.min !== undefined || this.max !== undefined) {
36
- return this.checkMinMax(value, this.min, this.max);
36
+ return this.checkMinMax(key, value, this.min, this.max);
37
37
  }
38
38
  return true;
39
39
  }
40
40
  /**
41
- * Validator function is called even if the value is null.
41
+ * The value is not required.
42
42
  */
43
- isRequired() {
44
- this.required = true;
43
+ isOptional() {
44
+ this.required = false;
45
45
  return this;
46
46
  }
47
47
  /**
@@ -62,44 +62,52 @@ class Validator {
62
62
  this.max = max;
63
63
  return this;
64
64
  }
65
- checkMinMax(input, min, max) {
66
- if (typeof input !== "string" && typeof input !== "number") {
67
- return false;
65
+ checkMinMax(key, value, min, max) {
66
+ if (typeof value !== "string" && typeof value !== "number") {
67
+ throw new ValidationError(400, `${key} n'est ni un nombre ni un texte`);
68
68
  }
69
- const nb = typeof input === "string" ? input.length : input;
69
+ const nb = typeof value === "string" ? value.length : value;
70
70
  if (min !== undefined && nb < min) {
71
- return false;
71
+ throw new ValidationError(400, typeof value === "string"
72
+ ? `Le texte ${key} n'est pas assez long, il manque ${min - nb} caractères`
73
+ : `La valeur de ${key} est trop basse, minimum: ${min}`);
72
74
  }
73
75
  if (max !== undefined && nb > max) {
74
- return false;
76
+ throw new ValidationError(400, typeof value === "string"
77
+ ? `Le texte ${key} est trop long, il y a ${nb - max} caractères en trop`
78
+ : `La valeur de ${key} est trop haute, maximum: ${max}`);
75
79
  }
76
80
  return true;
77
81
  }
78
82
  }
79
83
  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
84
  /**
88
85
  * Create a new validator for a number.
89
86
  */
90
- exports.isNumber = new Validator((value) => typeof value === "number");
87
+ const isNumber = () => new Validator((value) => typeof value === "number");
88
+ exports.isNumber = isNumber;
91
89
  /**
92
90
  * Create a new validator for a string.
93
91
  */
94
- exports.isString = new Validator((value) => typeof value === "string");
92
+ const isString = () => new Validator((value) => typeof value === "string");
93
+ exports.isString = isString;
95
94
  /**
96
95
  * Create a new validator for a boolean.
97
96
  */
98
- exports.isBoolean = new Validator((value) => typeof value === "boolean");
97
+ const isBoolean = () => new Validator((value) => typeof value === "boolean");
98
+ exports.isBoolean = isBoolean;
99
99
  /**
100
100
  * Create a new validator for an array.
101
101
  */
102
- exports.isArray = new Validator((value) => Array.isArray(value));
102
+ const isArray = () => new Validator((value) => Array.isArray(value));
103
+ exports.isArray = isArray;
104
+ /**
105
+ * Creates a new validator that tests a string value against a given regex pattern.
106
+ * @param pattern - The regex pattern to test.
107
+ * @returns
108
+ */
109
+ const isMatchRegex = (pattern) => new Validator((value) => typeof value === "string" && pattern.test(value));
110
+ exports.isMatchRegex = isMatchRegex;
103
111
  /////////////
104
112
  // Validate
105
113
  /**
@@ -125,18 +133,17 @@ exports.ValidationError = ValidationError;
125
133
  * @returns {BodyForO<O>} - The validated body.
126
134
  * @throws {ValidationError} - Throws a ValidationError exception if validation fails.
127
135
  */
128
- const validateBodyForO = (body, validators) => {
136
+ function validateBodyForO(body, validators) {
129
137
  if (typeof body !== "object" || body === null) {
130
138
  throw new ValidationError(400, "Body non fourni ou invalide");
131
139
  }
132
140
  const result = {};
133
141
  for (const [key, validator] of Object.entries(validators)) {
134
142
  const value = body[key];
135
- if (!validator.validate(value)) {
136
- throw new ValidationError(400, `${key} non fourni ou invalide`);
137
- }
138
- result[key] = value;
143
+ validator.validate(key, value);
144
+ if (value !== undefined)
145
+ result[key] = value;
139
146
  }
140
147
  return result;
141
- };
148
+ }
142
149
  exports.validateBodyForO = validateBodyForO;
package/package.json CHANGED
@@ -1,10 +1,12 @@
1
1
  {
2
2
  "name": "@cofondateurauchomage/libs",
3
- "version": "1.0.44",
3
+ "version": "1.0.45",
4
4
  "description": "",
5
- "main": "lib/index",
5
+ "main": "build/index",
6
6
  "scripts": {
7
- "build": "tsc"
7
+ "build": "tsc --project tsconfig.build.json",
8
+ "test": "jest",
9
+ "publishLibs": "pnpm test && npm version patch && pnpm build && npm publish && cd .. && pnpm updateLibs"
8
10
  },
9
11
  "publishConfig": {
10
12
  "access": "public"
@@ -13,9 +15,12 @@
13
15
  "author": "valent1618",
14
16
  "license": "ISC",
15
17
  "devDependencies": {
18
+ "@types/jest": "^29.5.5",
19
+ "jest": "^29.7.0",
20
+ "ts-jest": "^29.1.1",
16
21
  "typescript": "^4.9.5"
17
22
  },
18
23
  "files": [
19
- "lib"
24
+ "build"
20
25
  ]
21
26
  }
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes