5htp-core 0.4.9-1 → 0.4.9
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/package.json +1 -1
- package/src/client/components/Form.ts +5 -1
- package/src/client/components/inputv3/index.tsx +1 -1
- package/src/common/validation/index.ts +5 -5
- package/src/common/validation/schema.ts +20 -5
- package/src/common/validation/validator.ts +4 -4
- package/src/common/validation/validators.ts +11 -31
- package/src/server/services/schema/index.ts +3 -3
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "5htp-core",
|
|
3
3
|
"description": "Convenient TypeScript framework designed for Performance and Productivity.",
|
|
4
|
-
"version": "0.4.9
|
|
4
|
+
"version": "0.4.9",
|
|
5
5
|
"author": "Gaetan Le Gac (https://github.com/gaetanlegac)",
|
|
6
6
|
"repository": "git://github.com/gaetanlegac/5htp-core.git",
|
|
7
7
|
"license": "MIT",
|
|
@@ -135,7 +135,11 @@ export default function useForm<TFormData extends {}>(
|
|
|
135
135
|
// Validation
|
|
136
136
|
const validated = validate(allData);
|
|
137
137
|
if (validated.errorsCount !== 0) {
|
|
138
|
-
|
|
138
|
+
context.app.handleError(
|
|
139
|
+
new InputErrorSchema(validated.erreurs)
|
|
140
|
+
);
|
|
141
|
+
console.log("validated", validated.erreurs);
|
|
142
|
+
return;
|
|
139
143
|
}
|
|
140
144
|
|
|
141
145
|
// Callback
|
|
@@ -10,7 +10,7 @@ import TextareaAutosize from 'react-textarea-autosize';
|
|
|
10
10
|
// Core libs
|
|
11
11
|
import { useInput, InputBaseProps, InputWrapper } from './base';
|
|
12
12
|
import { default as Validator } from '../../../common/validation/validator';
|
|
13
|
-
import type
|
|
13
|
+
import type SchemaValidators from '@common/validation/validators';
|
|
14
14
|
|
|
15
15
|
/*----------------------------------
|
|
16
16
|
- TYPES
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
----------------------------------*/
|
|
4
4
|
|
|
5
5
|
import type { TValidatorDefinition } from './validator';
|
|
6
|
-
import type
|
|
6
|
+
import type Validators from './validators';
|
|
7
7
|
|
|
8
8
|
/*----------------------------------
|
|
9
9
|
- EXPORT
|
|
@@ -12,12 +12,12 @@ import type { SchemaValidators } from './validators';
|
|
|
12
12
|
export { default as Schema } from './schema';
|
|
13
13
|
export type { TSchemaFields, TValidatedData } from './schema';
|
|
14
14
|
|
|
15
|
-
export const field = new Proxy<
|
|
15
|
+
export const field = new Proxy<Validators>({} as Validators, {
|
|
16
16
|
get: (target, propKey) => {
|
|
17
17
|
return (...args: any[]) => ([ propKey, args ]);
|
|
18
18
|
}
|
|
19
19
|
}) as unknown as {
|
|
20
|
-
[K in keyof
|
|
21
|
-
? (...args: Parameters<
|
|
22
|
-
:
|
|
20
|
+
[K in keyof Validators]: Validators[K] extends (...args: any[]) => any
|
|
21
|
+
? (...args: Parameters<Validators[K]>) => TValidatorDefinition<K>
|
|
22
|
+
: Validators[K];
|
|
23
23
|
};
|
|
@@ -7,7 +7,8 @@ import { CoreError, TListeErreursSaisie, InputErrorSchema } from '@common/errors
|
|
|
7
7
|
|
|
8
8
|
// Specific
|
|
9
9
|
import { default as Validator, EXCLUDE_VALUE, TValidatorDefinition } from './validator';
|
|
10
|
-
import
|
|
10
|
+
import DefaultValidators from './validators';
|
|
11
|
+
const defaultValidators = new DefaultValidators;
|
|
11
12
|
|
|
12
13
|
/*----------------------------------
|
|
13
14
|
- TYPES
|
|
@@ -28,7 +29,7 @@ export type TValidateOptions<TFields extends TSchemaFields = {}> = {
|
|
|
28
29
|
only?: (keyof TFields)[],
|
|
29
30
|
validateDeps?: boolean,
|
|
30
31
|
autoCorrect?: boolean,
|
|
31
|
-
validators?:
|
|
32
|
+
validators?: DefaultValidators
|
|
32
33
|
}
|
|
33
34
|
|
|
34
35
|
export type TValidationResult<TFields extends TSchemaFields> = {
|
|
@@ -65,7 +66,7 @@ export default class Schema<TFields extends TSchemaFields> {
|
|
|
65
66
|
|
|
66
67
|
public getFieldValidator(
|
|
67
68
|
fieldName: string,
|
|
68
|
-
validators:
|
|
69
|
+
validators: DefaultValidators = defaultValidators
|
|
69
70
|
): null | Validator<any> | Schema<{}> {
|
|
70
71
|
|
|
71
72
|
let field = this.fields[fieldName];
|
|
@@ -74,8 +75,23 @@ export default class Schema<TFields extends TSchemaFields> {
|
|
|
74
75
|
return null;
|
|
75
76
|
|
|
76
77
|
// TValidatorDefinition
|
|
78
|
+
} else if (Array.isArray(field)) {
|
|
79
|
+
|
|
80
|
+
const [validatorName, validatorArgs] = field;
|
|
81
|
+
const getValidator = validators[validatorName];
|
|
82
|
+
if (getValidator === undefined)
|
|
83
|
+
throw new Error('Unknown validator: ' + validatorName);
|
|
84
|
+
|
|
85
|
+
return getValidator(...validatorArgs);
|
|
86
|
+
|
|
87
|
+
// TSchemaFields
|
|
88
|
+
} else if (field.constructor === Object) {
|
|
89
|
+
|
|
90
|
+
return new Schema(field as TSchemaFields);
|
|
91
|
+
|
|
92
|
+
// Schema
|
|
77
93
|
} else
|
|
78
|
-
return
|
|
94
|
+
return field as Validator<any>;
|
|
79
95
|
}
|
|
80
96
|
|
|
81
97
|
public validate<TDonnees extends TObjetDonnees>(
|
|
@@ -156,7 +172,6 @@ export default class Schema<TFields extends TSchemaFields> {
|
|
|
156
172
|
|
|
157
173
|
} else {
|
|
158
174
|
|
|
159
|
-
console.error(LogPrefix, '[' + cheminA + ']', error);
|
|
160
175
|
erreurs[cheminAstr] = ["Technical error while validating data"];
|
|
161
176
|
errorsCount++;
|
|
162
177
|
}
|
|
@@ -10,17 +10,17 @@ import { InputError } from '@common/errors';
|
|
|
10
10
|
|
|
11
11
|
// Specific
|
|
12
12
|
import type { TValidateOptions } from './schema';
|
|
13
|
-
import type
|
|
13
|
+
import type Validators from './validators';
|
|
14
14
|
import type { InputBaseProps } from '@client/components/inputv3/base';
|
|
15
15
|
|
|
16
16
|
/*----------------------------------
|
|
17
17
|
- TYPES
|
|
18
18
|
----------------------------------*/
|
|
19
19
|
|
|
20
|
-
export type TValidatorDefinition<K extends keyof
|
|
20
|
+
export type TValidatorDefinition<K extends keyof Validators = keyof Validators> = [
|
|
21
21
|
type: K,
|
|
22
|
-
args: Parameters<
|
|
23
|
-
returnType: ReturnType<
|
|
22
|
+
args: Parameters<Validators[K]>,
|
|
23
|
+
returnType: ReturnType<Validators[K]>
|
|
24
24
|
]
|
|
25
25
|
|
|
26
26
|
// TODO: remove
|
|
@@ -17,7 +17,7 @@ import FileToUpload from '@client/components/inputv3/file/FileToUpload';
|
|
|
17
17
|
|
|
18
18
|
// Speciific
|
|
19
19
|
import Schema, { TSchemaFields } from './schema'
|
|
20
|
-
import Validator, { TValidatorOptions, EXCLUDE_VALUE
|
|
20
|
+
import Validator, { TValidatorOptions, EXCLUDE_VALUE } from './validator'
|
|
21
21
|
|
|
22
22
|
/*----------------------------------
|
|
23
23
|
- TYPES
|
|
@@ -29,9 +29,9 @@ export type TFileValidator = TValidatorOptions<FileToUpload> & {
|
|
|
29
29
|
disk?: string, // Disk to upload files to
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
type TSchemaSubtype = Schema<{}> | TSchemaFields
|
|
32
|
+
type TSchemaSubtype = Schema<{}> | TSchemaFields;
|
|
33
33
|
|
|
34
|
-
type TSubtype = TSchemaSubtype | Validator<any
|
|
34
|
+
type TSubtype = TSchemaSubtype | Validator<any>;
|
|
35
35
|
|
|
36
36
|
/*----------------------------------
|
|
37
37
|
- CONST
|
|
@@ -41,27 +41,6 @@ export type TRichTextValidatorOptions = {
|
|
|
41
41
|
attachements?: TFileValidator
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
export const getFieldValidator = (field: TValidatorDefinition) => {
|
|
45
|
-
|
|
46
|
-
if (Array.isArray(field)) {
|
|
47
|
-
|
|
48
|
-
const [validatorName, validatorArgs] = field;
|
|
49
|
-
const getValidator = validators[validatorName];
|
|
50
|
-
if (getValidator === undefined)
|
|
51
|
-
throw new Error('Unknown validator: ' + validatorName);
|
|
52
|
-
|
|
53
|
-
return getValidator(...validatorArgs);
|
|
54
|
-
|
|
55
|
-
// TSchemaFields
|
|
56
|
-
} else if (field.constructor === Object) {
|
|
57
|
-
|
|
58
|
-
return new Schema(field as TSchemaFields);
|
|
59
|
-
|
|
60
|
-
// Schema
|
|
61
|
-
} else
|
|
62
|
-
return field as Validator<any>
|
|
63
|
-
}
|
|
64
|
-
|
|
65
44
|
// Recursive function to validate each node
|
|
66
45
|
function validateLexicalNode(node: any, opts: TRichTextValidatorOptions ) {
|
|
67
46
|
|
|
@@ -103,7 +82,7 @@ function validateLexicalNode(node: any, opts: TRichTextValidatorOptions ) {
|
|
|
103
82
|
/*----------------------------------
|
|
104
83
|
- CLASS
|
|
105
84
|
----------------------------------*/
|
|
106
|
-
export class SchemaValidators {
|
|
85
|
+
export default class SchemaValidators {
|
|
107
86
|
|
|
108
87
|
/*----------------------------------
|
|
109
88
|
- UTILITIES
|
|
@@ -150,7 +129,9 @@ export class SchemaValidators {
|
|
|
150
129
|
return val;
|
|
151
130
|
|
|
152
131
|
// If subtype is a schema
|
|
153
|
-
const schema =
|
|
132
|
+
const schema = subtype.constructor === Object
|
|
133
|
+
? new Schema(subtype as TSchemaFields)
|
|
134
|
+
: subtype as Schema<{}>;
|
|
154
135
|
|
|
155
136
|
// Validate schema
|
|
156
137
|
const value = schema.validate(val, options, path);
|
|
@@ -178,7 +159,9 @@ export class SchemaValidators {
|
|
|
178
159
|
if (subtype === undefined)
|
|
179
160
|
return items;
|
|
180
161
|
|
|
181
|
-
const validator =
|
|
162
|
+
const validator = subtype.constructor === Object
|
|
163
|
+
? new Schema(subtype as TSchemaFields)
|
|
164
|
+
: subtype as Schema<{}> | Validator<any>;
|
|
182
165
|
|
|
183
166
|
items = items.map( item =>
|
|
184
167
|
validator.validate( item, options, path )
|
|
@@ -478,8 +461,5 @@ export class SchemaValidators {
|
|
|
478
461
|
//defaut: new Date,
|
|
479
462
|
...opts,
|
|
480
463
|
})
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
const validators = new SchemaValidators();
|
|
484
464
|
|
|
485
|
-
|
|
465
|
+
}
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
import type { Application } from '@server/app';
|
|
7
7
|
|
|
8
8
|
// Specific
|
|
9
|
-
import {
|
|
9
|
+
import SchemaValidator, { TFileValidator } from '@common/validation/validators';
|
|
10
10
|
import Validator, { TValidatorOptions } from '@common/validation/validator';
|
|
11
11
|
|
|
12
12
|
import type FileToUpload from '@client/components/inputv3/file/FileToUpload';
|
|
@@ -19,9 +19,9 @@ import type FileToUpload from '@client/components/inputv3/file/FileToUpload';
|
|
|
19
19
|
/*----------------------------------
|
|
20
20
|
- SERVICE
|
|
21
21
|
----------------------------------*/
|
|
22
|
-
export default class ServerSchemaValidator extends
|
|
22
|
+
export default class ServerSchemaValidator extends SchemaValidator {
|
|
23
23
|
|
|
24
|
-
public constructor(
|
|
24
|
+
public constructor( private app: Application ) {
|
|
25
25
|
super();
|
|
26
26
|
}
|
|
27
27
|
|