5htp-core 0.2.5-2 → 0.2.6

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.
@@ -18,18 +18,18 @@ type TSchemaOptions = {
18
18
  opt?: boolean
19
19
  }
20
20
 
21
- type TOptsValider = {
21
+ export type TValidateOptions<TFields extends TSchemaFields = {}> = {
22
22
  debug?: boolean,
23
23
  throwError?: boolean,
24
-
25
- validateAll?: boolean,
24
+ ignoreMissing?: boolean,
25
+ only?: (keyof TFields)[],
26
26
  validateDeps?: boolean,
27
27
  autoCorrect?: boolean,
28
28
  }
29
29
 
30
30
  export type TValidationResult<TFields extends TSchemaFields> = {
31
31
  values: TValidatedData<TFields>,
32
- nbErreurs: number,
32
+ errorsCount: number,
33
33
  erreurs: TListeErreursSaisie
34
34
  }
35
35
 
@@ -62,7 +62,7 @@ export default class Schema<TFields extends TSchemaFields> {
62
62
  allData: TDonnees,
63
63
  output: TObjetDonnees = {},
64
64
 
65
- opts: TOptsValider = {},
65
+ opts: TValidateOptions<TFields> = {},
66
66
  chemin: string[] = []
67
67
 
68
68
  ): TValidationResult<TFields> {
@@ -70,45 +70,44 @@ export default class Schema<TFields extends TSchemaFields> {
70
70
  opts = {
71
71
  debug: false,
72
72
  throwError: false,
73
- validateAll: false,
74
73
  validateDeps: true,
75
74
  autoCorrect: false,
76
75
  ...opts,
77
76
  }
78
77
 
79
- const clesAvalider = Object.keys(opts.validateAll === true ? this.fields : dataToValidate);
80
-
81
78
  let outputSchema = output;
82
79
  for (const branche of chemin)
83
80
  outputSchema = outputSchema[branche];
81
+
82
+ const keysToValidate = opts.only || Object.keys(this.fields);
84
83
 
85
84
  // Validation de chacune d'entre elles
86
85
  let erreurs: TListeErreursSaisie = {};
87
- let nbErreurs = 0;
88
- for (const champ of clesAvalider) {
86
+ let errorsCount = 0;
87
+ for (const fieldName of keysToValidate) {
89
88
 
90
89
  // La donnée est répertoriée dans le schema
91
- const field = this.fields[champ];
90
+ const field = this.fields[fieldName];
92
91
  if (field === undefined) {
93
- opts.debug && console.warn(LogPrefix, '[' + champ + ']', 'Exclusion (pas présent dans le schéma)');
92
+ opts.debug && console.warn(LogPrefix, '[' + fieldName + ']', 'Exclusion (pas présent dans le schéma)');
94
93
  continue;
95
94
  }
96
95
 
97
- const cheminA = [...chemin, champ]
96
+ const cheminA = [...chemin, fieldName]
98
97
  const cheminAstr = cheminA.join('.')
99
98
 
100
99
  // Sous-schema
101
100
  if (field instanceof Schema) {
102
101
 
103
102
  // Initialise la structure pour permettre l'assignement d'outputSchema
104
- if (outputSchema[champ] === undefined)
105
- outputSchema[champ] = {}
103
+ if (outputSchema[fieldName] === undefined)
104
+ outputSchema[fieldName] = {}
106
105
 
107
106
  // The corresponding data should be an object
108
- const schemadata = dataToValidate[champ];
107
+ const schemadata = dataToValidate[fieldName];
109
108
  if (typeof schemadata !== 'object') {
110
109
  erreurs[ cheminAstr ] = [`Should be an object`];
111
- nbErreurs++;
110
+ errorsCount++;
112
111
  continue;
113
112
  }
114
113
 
@@ -123,30 +122,30 @@ export default class Schema<TFields extends TSchemaFields> {
123
122
  cheminA
124
123
  );
125
124
  erreurs = { ...erreurs, ...validationSchema.erreurs };
126
- nbErreurs += validationSchema.nbErreurs;
125
+ errorsCount += validationSchema.errorsCount;
127
126
 
128
127
  // Pas besoin d'assigner, car output est passé en référence
129
- //output[champ] = validationSchema.values;
128
+ //output[fieldName] = validationSchema.values;
130
129
 
131
130
 
132
131
  // I don't remind what is options.activer about
133
132
  /*} else if (field.activer !== undefined && field.activer(allData) === false) {
134
133
 
135
- delete outputSchema[champ];*/
134
+ delete outputSchema[fieldName];*/
136
135
 
137
136
  // Validator
138
137
  } else {
139
138
 
140
139
  // Champ composé de plusieurs values
141
140
  const valOrigine = field.options.as === undefined
142
- ? dataToValidate[champ]
143
- // Le champ regroupe plusieurs values (ex: Periode)
141
+ ? dataToValidate[fieldName]
142
+ // Le fieldName regroupe plusieurs values (ex: Periode)
144
143
  : field.options.as.map((nomVal: string) => dataToValidate[nomVal])
145
144
 
146
145
  // Validation
147
146
  try {
148
147
 
149
- const val = field.validate(valOrigine, allData, output, opts.autoCorrect);
148
+ const val = field.validate(valOrigine, allData, output, opts);
150
149
 
151
150
  // Exclusion seulement si explicitement demandé
152
151
  // IMPORTANT: Conserver les values undefined
@@ -155,7 +154,7 @@ export default class Schema<TFields extends TSchemaFields> {
155
154
  if (val === EXCLUDE_VALUE)
156
155
  opts.debug && console.log(LogPrefix, '[' + cheminA + '] Exclusion demandée');
157
156
  else
158
- outputSchema[champ] = val;
157
+ outputSchema[fieldName] = val;
159
158
 
160
159
  opts.debug && console.log(LogPrefix, '[' + cheminA + ']', valOrigine, '=>', val);
161
160
 
@@ -167,7 +166,7 @@ export default class Schema<TFields extends TSchemaFields> {
167
166
 
168
167
  // Référencement erreur
169
168
  erreurs[cheminAstr] = [error.message]
170
- nbErreurs++;
169
+ errorsCount++;
171
170
 
172
171
  } else
173
172
  throw error;
@@ -175,7 +174,7 @@ export default class Schema<TFields extends TSchemaFields> {
175
174
  }
176
175
  }
177
176
 
178
- if (nbErreurs !== 0 && opts.throwError === true) {
177
+ if (errorsCount !== 0 && opts.throwError === true) {
179
178
  throw new InputErrorSchema(erreurs);
180
179
  }
181
180
 
@@ -184,7 +183,7 @@ export default class Schema<TFields extends TSchemaFields> {
184
183
  return {
185
184
  values: output as TValidatedData<TFields>,
186
185
  erreurs,
187
- nbErreurs,
186
+ errorsCount,
188
187
  };
189
188
 
190
189
  }
@@ -8,6 +8,9 @@ import type { ComponentChild } from 'preact'
8
8
  // Core
9
9
  import { InputError } from '@common/errors';
10
10
 
11
+ // Specific
12
+ import type { TValidateOptions } from './schema';
13
+
11
14
  /*----------------------------------
12
15
  - TYPES
13
16
  ----------------------------------*/
@@ -41,12 +44,12 @@ type TValidationArgs<TValue, TAllValues extends {}> = [
41
44
  val: TNonEmptyValue,
42
45
  input: TAllValues,
43
46
  output: Partial<TAllValues>,
44
- corriger?: boolean
47
+ validateOptions?: TValidateOptions
45
48
  ]
46
49
 
47
50
  type TValidationFunction<TValue, TAllValues extends {} = {}> = (
48
51
  ...args: TValidationArgs<TValue, TAllValues>
49
- ) => TValue | typeof EXCLUDE_VALUE;
52
+ ) => TValue | typeof EXCLUDE_VALUE;
50
53
 
51
54
  type TValidateReturnType<
52
55
  TOptions extends TValidator<TValue>,
@@ -76,12 +79,18 @@ export default class Validator<TValue, TOptions extends TValidator<TValue> = TVa
76
79
 
77
80
  public isEmpty = (val: any) => val === undefined || val === '' || val === null
78
81
 
79
- public validate(...[ val, input, output, correct ]: TValidationArgs<TValue, {}>): TValidateReturnType<TOptions, TValue> {
82
+ public validate(...[
83
+ val, input, output, validateOptions
84
+ ]: TValidationArgs<TValue, {}>): TValidateReturnType<TOptions, TValue> {
80
85
 
81
86
  // Required value
82
87
  if (this.isEmpty(val)) {
83
88
  // Optionnel, on skip
84
- if (this.options.opt === true)
89
+ if (this.options.opt === true || (
90
+ validateOptions?.ignoreMissing === true
91
+ &&
92
+ val === undefined
93
+ ))
85
94
  return undefined as TValidateReturnType<TOptions, TValue>;
86
95
  // Requis
87
96
  else
@@ -89,7 +98,7 @@ export default class Validator<TValue, TOptions extends TValidator<TValue> = TVa
89
98
  }
90
99
 
91
100
  // Validate type
92
- return this.validateType(val, input, output, correct) as TValidateReturnType<TOptions, TValue>;
101
+ return this.validateType(val, input, output, validateOptions) as TValidateReturnType<TOptions, TValue>;
93
102
  }
94
103
 
95
104
  }
@@ -192,9 +192,13 @@ export default abstract class Application extends Service<Config, Hooks, /* TODO
192
192
  // Start service
193
193
  if (service.start) {
194
194
  service.started = service.start();
195
- await service.started;
195
+ await service.started.catch(e => {
196
+ console.error("Catched error while starting service " + serviceClassName + '. Exiting process if mode production.');
197
+ if (this.env.profile === 'prod')
198
+ process.exit();
199
+ })
196
200
  }
197
-
201
+
198
202
  service.status = 'running';
199
203
  }
200
204
 
@@ -54,7 +54,6 @@ export default class RequestValidator extends ServerSchemaValidator implements R
54
54
  {
55
55
  debug: this.config.debug,
56
56
  throwError: true,
57
- validateAll: true,
58
57
  validateDeps: false
59
58
  },
60
59
  []