@azure-net/kit 0.8.9 → 0.9.1
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/dist/core/delivery/injectableDependencies/ErrorHandler.d.ts +12 -12
- package/dist/core/delivery/injectableDependencies/ErrorHandler.js +15 -23
- package/dist/core/delivery/schema/Schema.d.ts +32 -20
- package/dist/core/delivery/schema/Schema.js +105 -60
- package/dist/core/delivery/schema/rules/index.d.ts +2 -0
- package/dist/core/delivery/schema/rules/index.js +2 -0
- package/dist/core/index.d.ts +1 -1
- package/dist/core/index.js +1 -1
- package/dist/core/shared/middleware/Middleware.d.ts +4 -2
- package/dist/core/shared/middleware/Middleware.js +9 -9
- package/dist/{svelte → core/svelte}/ActiveForm/ActiveForm.svelte.d.ts +0 -4
- package/dist/{svelte → core/svelte}/ActiveForm/ActiveForm.svelte.js +1 -1
- package/package.json +4 -12
- package/dist/core/shared/request/BaseRequest.d.ts +0 -41
- package/dist/core/shared/request/BaseRequest.js +0 -72
- package/dist/core/shared/request/index.d.ts +0 -1
- package/dist/core/shared/request/index.js +0 -1
- package/dist/core/shared/request/rules/Rules.d.ts +0 -62
- package/dist/core/shared/request/rules/Rules.js +0 -288
- package/dist/core/shared/request/rules/index.d.ts +0 -1
- package/dist/core/shared/request/rules/index.js +0 -1
- package/dist/core/shared/request/rules/messages/En.d.ts +0 -2
- package/dist/core/shared/request/rules/messages/En.js +0 -40
- package/dist/core/shared/request/rules/messages/Ru.d.ts +0 -2
- package/dist/core/shared/request/rules/messages/Ru.js +0 -40
- package/dist/core/shared/request/rules/messages/WithI18n.d.ts +0 -2
- package/dist/core/shared/request/rules/messages/WithI18n.js +0 -40
- package/dist/core/shared/request/rules/messages/index.d.ts +0 -3
- package/dist/core/shared/request/rules/messages/index.js +0 -3
- package/dist/core/shared/request/rules/messages/types.d.ts +0 -41
- package/dist/core/shared/request/rules/messages/types.js +0 -1
- /package/dist/{svelte → core/svelte}/ActiveForm/index.d.ts +0 -0
- /package/dist/{svelte → core/svelte}/ActiveForm/index.js +0 -0
- /package/dist/{svelte → core/svelte}/AsyncSignal/AsyncSignal.svelte.d.ts +0 -0
- /package/dist/{svelte → core/svelte}/AsyncSignal/AsyncSignal.svelte.js +0 -0
- /package/dist/{svelte → core/svelte}/AsyncSignal/index.d.ts +0 -0
- /package/dist/{svelte → core/svelte}/AsyncSignal/index.js +0 -0
- /package/dist/{svelte → core/svelte}/index.d.ts +0 -0
- /package/dist/{svelte → core/svelte}/index.js +0 -0
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import { BaseRequest, type RequestErrors } from '../../shared/request/index.js';
|
|
2
1
|
import { HttpServiceError } from '../../infra/httpService/index.js';
|
|
3
|
-
|
|
2
|
+
import { SchemaFail, type RequestErrors } from '../schema/index.js';
|
|
3
|
+
export type AppErrorType = 'http' | 'app' | 'schema' | 'abort';
|
|
4
4
|
export interface AppError<T = unknown, D = never> {
|
|
5
5
|
type: AppErrorType;
|
|
6
6
|
message: string;
|
|
7
7
|
fields?: RequestErrors<T>;
|
|
8
8
|
status?: number;
|
|
9
|
-
original?: HttpServiceError<T> |
|
|
9
|
+
original?: HttpServiceError<T> | SchemaFail<T> | Error;
|
|
10
10
|
custom?: D;
|
|
11
11
|
}
|
|
12
|
-
export type ErrorType<T = unknown> = Error |
|
|
13
|
-
export declare const
|
|
14
|
-
export declare const
|
|
15
|
-
export declare const
|
|
16
|
-
export declare const createErrorParser: <Custom = unknown>(
|
|
17
|
-
parseBaseError: typeof
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}) =>
|
|
12
|
+
export type ErrorType<T = unknown> = Error | HttpServiceError<T> | SchemaFail<T>;
|
|
13
|
+
export declare const baseParseHttpError: <T = unknown, D = never>(error: HttpServiceError<T>) => AppError<T, D>;
|
|
14
|
+
export declare const baseParseSchemaError: <SchemaData = unknown, D = never>(error: SchemaFail<SchemaData>) => AppError<SchemaData, D>;
|
|
15
|
+
export declare const baseParseBaseError: <D = never>(error: Error) => AppError<never, D>;
|
|
16
|
+
export declare const createErrorParser: <Custom = unknown>(parsers?: {
|
|
17
|
+
parseBaseError: typeof baseParseBaseError;
|
|
18
|
+
parseHttpError: typeof baseParseHttpError;
|
|
19
|
+
parseSchemaError: typeof baseParseSchemaError;
|
|
20
|
+
}) => (<T>(error: ErrorType<T>) => AppError<T, Custom>);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { BaseRequest } from '../../shared/request/index.js';
|
|
2
1
|
import { HttpServiceError } from '../../infra/httpService/index.js';
|
|
3
|
-
|
|
2
|
+
import { SchemaFail } from '../schema/index.js';
|
|
3
|
+
export const baseParseHttpError = (error) => {
|
|
4
4
|
return {
|
|
5
5
|
type: 'http',
|
|
6
6
|
message: error.message ?? 'unexpected error',
|
|
@@ -8,40 +8,32 @@ export const parseHttpError = (error) => {
|
|
|
8
8
|
original: error
|
|
9
9
|
};
|
|
10
10
|
};
|
|
11
|
-
export const
|
|
11
|
+
export const baseParseSchemaError = (error) => {
|
|
12
12
|
return {
|
|
13
|
-
type: '
|
|
14
|
-
message: 'validation error',
|
|
13
|
+
type: 'schema',
|
|
14
|
+
message: 'schema validation error',
|
|
15
15
|
status: 422,
|
|
16
16
|
fields: error.getErrors(),
|
|
17
17
|
original: error
|
|
18
18
|
};
|
|
19
19
|
};
|
|
20
|
-
export const
|
|
20
|
+
export const baseParseBaseError = (error) => {
|
|
21
21
|
return {
|
|
22
22
|
type: 'app',
|
|
23
23
|
message: error.message,
|
|
24
24
|
original: error
|
|
25
25
|
};
|
|
26
26
|
};
|
|
27
|
-
export const createErrorParser = (
|
|
27
|
+
export const createErrorParser = (parsers) => {
|
|
28
28
|
return (error) => {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
switch (true) {
|
|
38
|
-
case error instanceof HttpServiceError:
|
|
39
|
-
return parseHttpError(error);
|
|
40
|
-
case error instanceof BaseRequest:
|
|
41
|
-
return parseRequestError(error);
|
|
42
|
-
default:
|
|
43
|
-
return parseBaseError(error);
|
|
44
|
-
}
|
|
29
|
+
const { parseBaseError = baseParseBaseError, parseHttpError = baseParseHttpError, parseSchemaError = baseParseSchemaError } = parsers ?? {};
|
|
30
|
+
switch (true) {
|
|
31
|
+
case error instanceof HttpServiceError:
|
|
32
|
+
return parseHttpError(error);
|
|
33
|
+
case error instanceof SchemaFail:
|
|
34
|
+
return parseSchemaError(error);
|
|
35
|
+
default:
|
|
36
|
+
return parseBaseError(error);
|
|
45
37
|
}
|
|
46
38
|
};
|
|
47
39
|
};
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
type DeepKeys<
|
|
2
|
-
[K in keyof
|
|
3
|
-
}[keyof
|
|
4
|
-
export type ValidationParams<
|
|
5
|
-
val:
|
|
6
|
-
listValues?: Partial<
|
|
7
|
-
key?:
|
|
1
|
+
type DeepKeys<SchemaData> = SchemaData extends object ? {
|
|
2
|
+
[K in keyof SchemaData & string]: SchemaData[K] extends object ? K | `${K}.${DeepKeys<SchemaData[K]>}` : K;
|
|
3
|
+
}[keyof SchemaData & string] : never;
|
|
4
|
+
export type ValidationParams<SchemaData = unknown, CurrentValue = unknown, CurrentKey = string> = {
|
|
5
|
+
val: CurrentValue;
|
|
6
|
+
listValues?: Partial<SchemaData>;
|
|
7
|
+
key?: CurrentKey;
|
|
8
8
|
};
|
|
9
9
|
export type ValidationMessage = string | {
|
|
10
10
|
key: string;
|
|
@@ -15,19 +15,31 @@ export interface ValidationErrorsMap {
|
|
|
15
15
|
}
|
|
16
16
|
export type ValidationResult = ValidationMessage | ValidationErrorsMap | ValidationErrorsMap[] | undefined;
|
|
17
17
|
export type ValidationRuleResult<CurrentValue, ListValues = unknown, CurrentKey = string> = (params: ValidationParams<CurrentValue, ListValues, CurrentKey>) => ValidationResult;
|
|
18
|
-
type ValidationErrors<
|
|
19
|
-
[K in keyof
|
|
18
|
+
type ValidationErrors<Errors> = Errors extends ReadonlyArray<infer U> ? ValidationErrors<U>[] : Errors extends object ? {
|
|
19
|
+
[K in keyof Errors]?: ValidationErrors<Errors[K]>;
|
|
20
20
|
} : ValidationMessage;
|
|
21
|
-
export type RequestErrors<
|
|
22
|
-
export type RequestRules<
|
|
23
|
-
export declare
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
21
|
+
export type RequestErrors<SchemaData> = Partial<ValidationErrors<SchemaData>>;
|
|
22
|
+
export type RequestRules<SchemaData> = Partial<Record<DeepKeys<SchemaData>, ValidationRuleResult<SchemaData>[]>>;
|
|
23
|
+
export declare class SchemaFail<SchemaData> {
|
|
24
|
+
private _errors;
|
|
25
|
+
constructor(_errors: RequestErrors<SchemaData>);
|
|
26
|
+
getErrors(): Partial<ValidationErrors<SchemaData>>;
|
|
27
|
+
}
|
|
28
|
+
interface SchemaBuilder<SchemaData, Rules = unknown, TransformResult = SchemaData, CustomMethods = unknown> {
|
|
29
|
+
rules(callback: (factory: Rules) => RequestRules<SchemaData>): SchemaBuilder<SchemaData, Rules, TransformResult, CustomMethods>;
|
|
30
|
+
transform<T>(callback: (data: SchemaData) => T): SchemaBuilder<SchemaData, Rules, T, CustomMethods>;
|
|
31
|
+
with<M extends Record<string, unknown>>(callback: () => M): SchemaBuilder<SchemaData, Rules, TransformResult, M>;
|
|
32
|
+
create(): Schema<SchemaData, TransformResult, CustomMethods>;
|
|
33
|
+
}
|
|
34
|
+
interface SchemaInstance<TransformResult> {
|
|
35
|
+
json(): TransformResult;
|
|
36
|
+
formData(): FormData;
|
|
37
|
+
validated(): boolean;
|
|
38
|
+
}
|
|
39
|
+
type Schema<SchemaData, TransformResult, CustomMethods> = CustomMethods & {
|
|
40
|
+
from(data: Partial<SchemaData>): SchemaInstance<TransformResult>;
|
|
41
|
+
getSchemaError(e: unknown): RequestErrors<SchemaData>;
|
|
32
42
|
};
|
|
43
|
+
export declare const createSchemaFactory: <Rules extends Record<string, unknown>>(rules: Rules) => <SchemaData>() => SchemaBuilder<SchemaData, Rules>;
|
|
44
|
+
export declare const schema: <SchemaData>() => SchemaBuilder<SchemaData>;
|
|
33
45
|
export {};
|
|
@@ -1,13 +1,46 @@
|
|
|
1
|
-
import { FormDataUtil } from 'azure-net-tools';
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
import { FormDataUtil, ObjectUtil } from 'azure-net-tools';
|
|
2
|
+
export class SchemaFail {
|
|
3
|
+
_errors;
|
|
4
|
+
constructor(_errors) {
|
|
5
|
+
this._errors = _errors;
|
|
6
|
+
}
|
|
7
|
+
getErrors() {
|
|
8
|
+
return this._errors;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
class SchemaBuilderImpl {
|
|
12
|
+
_rulesFactory;
|
|
13
|
+
_rules;
|
|
14
|
+
_transform;
|
|
15
|
+
_customMethods;
|
|
16
|
+
constructor(_rulesFactory = {}) {
|
|
17
|
+
this._rulesFactory = _rulesFactory;
|
|
18
|
+
}
|
|
19
|
+
rules(callback) {
|
|
20
|
+
this._rules = callback;
|
|
21
|
+
return this;
|
|
22
|
+
}
|
|
23
|
+
transform(callback) {
|
|
24
|
+
this._transform = callback;
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
with(callback) {
|
|
28
|
+
this._customMethods = callback;
|
|
29
|
+
return this;
|
|
30
|
+
}
|
|
31
|
+
create() {
|
|
32
|
+
const rules = this._rules;
|
|
33
|
+
const transform = this._transform || ((data) => data);
|
|
34
|
+
const customMethods = this._customMethods ? this._customMethods() : {};
|
|
35
|
+
const rulesFactory = this._rulesFactory;
|
|
36
|
+
const prepare = (dataToPrepare) => {
|
|
37
|
+
if (dataToPrepare instanceof FormData) {
|
|
38
|
+
return FormDataUtil.toObject(dataToPrepare);
|
|
39
|
+
}
|
|
40
|
+
return ObjectUtil.deepClone(dataToPrepare);
|
|
41
|
+
};
|
|
42
|
+
const getByPath = (path, data) => {
|
|
43
|
+
return path.split('.').reduce((o, k) => (o && typeof o === 'object' ? o[k] : undefined), data);
|
|
11
44
|
};
|
|
12
45
|
const setByPath = (obj, path, value) => {
|
|
13
46
|
const keys = path.split('.');
|
|
@@ -21,58 +54,70 @@ export const createSchemaFactory = (rulesList) => {
|
|
|
21
54
|
}
|
|
22
55
|
current[keys[(keys.length - 1)]] = value;
|
|
23
56
|
};
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
};
|
|
30
|
-
const validate = () => {
|
|
31
|
-
_isValid = true;
|
|
32
|
-
_errors = {};
|
|
33
|
-
if (typeof schemaData !== 'object') {
|
|
34
|
-
throw Error('Data to validate is not an object');
|
|
35
|
-
}
|
|
36
|
-
for (const key in definedSchema) {
|
|
37
|
-
const fieldRules = definedSchema[key] ?? [];
|
|
38
|
-
const value = getByPath(key);
|
|
39
|
-
for (const rule of fieldRules) {
|
|
40
|
-
const failMessage = rule({
|
|
41
|
-
val: value,
|
|
42
|
-
listValues: schemaData,
|
|
43
|
-
key: key
|
|
44
|
-
});
|
|
45
|
-
if (failMessage) {
|
|
46
|
-
setByPath(_errors, key, failMessage);
|
|
47
|
-
_isValid = false;
|
|
48
|
-
break;
|
|
49
|
-
}
|
|
57
|
+
const schema = {
|
|
58
|
+
...customMethods,
|
|
59
|
+
getSchemaError(e) {
|
|
60
|
+
if (e instanceof SchemaFail) {
|
|
61
|
+
return e.getErrors();
|
|
50
62
|
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
63
|
+
},
|
|
64
|
+
from(data) {
|
|
65
|
+
const _preparedData = prepare(data);
|
|
66
|
+
let _errors = {};
|
|
67
|
+
let _isValid = true;
|
|
68
|
+
if (typeof _preparedData !== 'object') {
|
|
69
|
+
throw Error('Data to validate is not an object');
|
|
70
|
+
}
|
|
71
|
+
const validated = () => {
|
|
72
|
+
if (!rules)
|
|
73
|
+
return true;
|
|
74
|
+
_isValid = true;
|
|
75
|
+
_errors = {};
|
|
76
|
+
const definedSchema = rules(rulesFactory);
|
|
77
|
+
for (const key in definedSchema) {
|
|
78
|
+
const fieldRules = definedSchema[key] ?? [];
|
|
79
|
+
const value = getByPath(key, _preparedData);
|
|
80
|
+
for (const rule of fieldRules) {
|
|
81
|
+
const failMessage = rule({
|
|
82
|
+
val: value,
|
|
83
|
+
listValues: _preparedData,
|
|
84
|
+
key: key
|
|
85
|
+
});
|
|
86
|
+
if (failMessage) {
|
|
87
|
+
setByPath(_errors, key, failMessage);
|
|
88
|
+
_isValid = false;
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (!_isValid) {
|
|
94
|
+
throw new SchemaFail(_errors);
|
|
95
|
+
}
|
|
96
|
+
return true;
|
|
97
|
+
};
|
|
98
|
+
const json = () => {
|
|
99
|
+
validated();
|
|
100
|
+
return transform(_preparedData);
|
|
101
|
+
};
|
|
102
|
+
const formData = () => {
|
|
103
|
+
validated();
|
|
104
|
+
return FormDataUtil.fromObject(transform(_preparedData));
|
|
105
|
+
};
|
|
106
|
+
return {
|
|
107
|
+
json,
|
|
108
|
+
formData,
|
|
109
|
+
validated
|
|
110
|
+
};
|
|
75
111
|
}
|
|
76
112
|
};
|
|
113
|
+
return schema;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
export const createSchemaFactory = (rules) => {
|
|
117
|
+
return () => {
|
|
118
|
+
return new SchemaBuilderImpl(rules);
|
|
77
119
|
};
|
|
78
120
|
};
|
|
121
|
+
export const schema = () => {
|
|
122
|
+
return new SchemaBuilderImpl();
|
|
123
|
+
};
|
package/dist/core/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export * from './delivery/index.js';
|
|
2
|
-
export * from './shared/request/index.js';
|
|
3
2
|
export * from './shared/middleware/index.js';
|
|
4
3
|
export * from './shared/classMirror/index.js';
|
|
5
4
|
export * from './shared/cookie/index.js';
|
|
6
5
|
export * from './shared/boundaryProvider/index.js';
|
|
7
6
|
export * from './ui/index.js';
|
|
7
|
+
export * from './svelte/index.js';
|
package/dist/core/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export * from './delivery/index.js';
|
|
2
|
-
export * from './shared/request/index.js';
|
|
3
2
|
export * from './shared/middleware/index.js';
|
|
4
3
|
export * from './shared/classMirror/index.js';
|
|
5
4
|
export * from './shared/cookie/index.js';
|
|
6
5
|
export * from './shared/boundaryProvider/index.js';
|
|
7
6
|
export * from './ui/index.js';
|
|
7
|
+
export * from './svelte/index.js';
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { error, type Page, type RequestEvent } from '@sveltejs/kit';
|
|
2
2
|
import { UniversalCookie } from '../cookie/index.js';
|
|
3
|
+
type RedirectStatus = 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308;
|
|
3
4
|
export type IMiddleware = (middlewareData: {
|
|
4
5
|
to: RequestEvent['url'] | Page['url'];
|
|
5
6
|
from?: RequestEvent['url'] | Page['url'];
|
|
6
7
|
error: typeof error;
|
|
7
|
-
next: (location?: string | URL, status?:
|
|
8
|
+
next: (location?: string | URL, status?: RedirectStatus) => void;
|
|
8
9
|
isServer: boolean;
|
|
9
|
-
cookies: UniversalCookie;
|
|
10
|
+
cookies: typeof UniversalCookie;
|
|
10
11
|
event?: RequestEvent;
|
|
11
12
|
page: Page;
|
|
12
13
|
}) => Promise<void> | void;
|
|
@@ -14,3 +15,4 @@ export declare const createMiddlewareManager: (middlewares: IMiddleware[]) => {
|
|
|
14
15
|
serverMiddleware: () => Promise<void>;
|
|
15
16
|
clientMiddleware: () => void;
|
|
16
17
|
};
|
|
18
|
+
export {};
|
|
@@ -4,16 +4,16 @@ import { beforeNavigate, goto } from '$app/navigation';
|
|
|
4
4
|
import { page } from '$app/state';
|
|
5
5
|
import { EnvironmentUtil } from 'azure-net-tools';
|
|
6
6
|
import { UniversalCookie } from '../cookie/index.js';
|
|
7
|
-
const universalRedirect = (location, status = 301, navigation) => {
|
|
8
|
-
if (EnvironmentUtil.isBrowser && navigation) {
|
|
9
|
-
navigation?.cancel();
|
|
10
|
-
return goto(location);
|
|
11
|
-
}
|
|
12
|
-
else {
|
|
13
|
-
return redirect(status, location);
|
|
14
|
-
}
|
|
15
|
-
};
|
|
16
7
|
export const createMiddlewareManager = (middlewares) => {
|
|
8
|
+
const universalRedirect = (location, status = 301, navigation) => {
|
|
9
|
+
if (EnvironmentUtil.isBrowser && navigation) {
|
|
10
|
+
navigation?.cancel();
|
|
11
|
+
return goto(location);
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
return redirect(status, location);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
17
|
const executeMiddlewares = async (navigation) => {
|
|
18
18
|
let event;
|
|
19
19
|
let from = undefined;
|
|
@@ -1,16 +1,13 @@
|
|
|
1
|
-
import { type RequestErrors, BaseRequest } from '../../core/index.js';
|
|
2
1
|
export interface FormConfig<T> {
|
|
3
2
|
onSubmit: (data: T) => Promise<void> | void;
|
|
4
3
|
initialData?: T;
|
|
5
4
|
validation?: {
|
|
6
|
-
schema?: new (data: Partial<T>) => BaseRequest<T, T>;
|
|
7
5
|
validateOnChange?: boolean;
|
|
8
6
|
validateOnBlur?: boolean;
|
|
9
7
|
};
|
|
10
8
|
}
|
|
11
9
|
export interface ActiveForm<T> {
|
|
12
10
|
data: Partial<T>;
|
|
13
|
-
errors: RequestErrors<T>;
|
|
14
11
|
touched: Partial<Record<keyof T, boolean>>;
|
|
15
12
|
pending: boolean;
|
|
16
13
|
dirty: boolean;
|
|
@@ -19,6 +16,5 @@ export interface ActiveForm<T> {
|
|
|
19
16
|
validate: (field?: keyof T) => void;
|
|
20
17
|
reset: () => void;
|
|
21
18
|
setField: <K extends keyof T>(field: K, value: T[K]) => void;
|
|
22
|
-
setErrors: (errors: RequestErrors<T>) => void;
|
|
23
19
|
touchField: (field: keyof T) => void;
|
|
24
20
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
export {};
|
|
2
2
|
// export function createActiveForm<T extends object>(config: FormConfig<T>): ActiveForm<T> {
|
|
3
3
|
// const initial: Partial<T> = config.initialData ?? {};
|
|
4
4
|
// let data = $state<Partial<T>>(structuredClone(initial));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@azure-net/kit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.1",
|
|
4
4
|
"files": [
|
|
5
5
|
"dist",
|
|
6
6
|
"!dist/**/*.test.*",
|
|
@@ -21,17 +21,9 @@
|
|
|
21
21
|
"types": "./dist/core/infra/index.d.ts",
|
|
22
22
|
"svelte": "./dist/core/infra/index.js"
|
|
23
23
|
},
|
|
24
|
-
"./
|
|
25
|
-
"types": "./dist/
|
|
26
|
-
"svelte": "./dist/
|
|
27
|
-
},
|
|
28
|
-
"./request/rules": {
|
|
29
|
-
"types": "./dist/core/shared/request/rules/index.d.ts",
|
|
30
|
-
"svelte": "./dist/core/shared/request/rules/index.js"
|
|
31
|
-
},
|
|
32
|
-
"./request/messages": {
|
|
33
|
-
"types": "./dist/core/shared/request/rules/messages/index.d.ts",
|
|
34
|
-
"svelte": "./dist/core/shared/request/rules/messages/index.js"
|
|
24
|
+
"./schema": {
|
|
25
|
+
"types": "./dist/core/delivery/schema/rules/index.d.ts",
|
|
26
|
+
"svelte": "./dist/core/delivery/schema/rules/index.js"
|
|
35
27
|
},
|
|
36
28
|
"./edges": {
|
|
37
29
|
"types": "./dist/edges/index.d.ts",
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
type DeepKeys<T> = T extends object ? {
|
|
2
|
-
[K in keyof T & string]: T[K] extends object ? K | `${K}.${DeepKeys<T[K]>}` : K;
|
|
3
|
-
}[keyof T & string] : never;
|
|
4
|
-
export type ValidationParams<T = unknown, V = unknown, K = string> = {
|
|
5
|
-
val: V;
|
|
6
|
-
listValues?: Partial<T>;
|
|
7
|
-
key?: K;
|
|
8
|
-
};
|
|
9
|
-
export type ValidationMessage = string | {
|
|
10
|
-
key: string;
|
|
11
|
-
vars?: Record<string, unknown>;
|
|
12
|
-
};
|
|
13
|
-
export interface ValidationErrorsMap {
|
|
14
|
-
[key: string]: ValidationMessage | ValidationErrorsMap;
|
|
15
|
-
}
|
|
16
|
-
export type ValidationResult = ValidationMessage | ValidationErrorsMap | ValidationErrorsMap[] | undefined;
|
|
17
|
-
export type ValidationRuleResult<CurrentValue, ListValues = unknown, CurrentKey = string> = (params: ValidationParams<CurrentValue, ListValues, CurrentKey>) => ValidationResult;
|
|
18
|
-
type ValidationErrors<T> = T extends ReadonlyArray<infer U> ? ValidationErrors<U>[] : T extends object ? {
|
|
19
|
-
[K in keyof T]?: ValidationErrors<T[K]>;
|
|
20
|
-
} : ValidationMessage;
|
|
21
|
-
export type RequestErrors<Dict> = Partial<ValidationErrors<Dict>>;
|
|
22
|
-
export type RequestRules<D> = Partial<Record<DeepKeys<D>, ValidationRuleResult<D>[]>>;
|
|
23
|
-
export interface IRequest<T, D = T> {
|
|
24
|
-
rules?(): RequestRules<D>;
|
|
25
|
-
}
|
|
26
|
-
export declare class BaseRequest<T, D = T> implements IRequest<T, D> {
|
|
27
|
-
data: D;
|
|
28
|
-
errors: RequestErrors<D>;
|
|
29
|
-
isValid: boolean;
|
|
30
|
-
constructor(data: Partial<D> | FormData);
|
|
31
|
-
rules(): RequestRules<D>;
|
|
32
|
-
private prepareData;
|
|
33
|
-
transform(data: D): T;
|
|
34
|
-
formData(): FormData;
|
|
35
|
-
json(): T;
|
|
36
|
-
getErrors(): Partial<ValidationErrors<D>>;
|
|
37
|
-
private getByPath;
|
|
38
|
-
private setByPath;
|
|
39
|
-
validated(): this;
|
|
40
|
-
}
|
|
41
|
-
export {};
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import { FormDataUtil } from 'azure-net-tools';
|
|
2
|
-
export class BaseRequest {
|
|
3
|
-
data;
|
|
4
|
-
errors = {};
|
|
5
|
-
isValid = true;
|
|
6
|
-
constructor(data) {
|
|
7
|
-
this.data = this.prepareData(data);
|
|
8
|
-
}
|
|
9
|
-
rules() {
|
|
10
|
-
return {};
|
|
11
|
-
}
|
|
12
|
-
prepareData(data) {
|
|
13
|
-
if (data instanceof FormData) {
|
|
14
|
-
return FormDataUtil.toObject(data);
|
|
15
|
-
}
|
|
16
|
-
return data;
|
|
17
|
-
}
|
|
18
|
-
transform(data) {
|
|
19
|
-
return data;
|
|
20
|
-
}
|
|
21
|
-
formData() {
|
|
22
|
-
this.validated();
|
|
23
|
-
return FormDataUtil.fromObject(this.transform(this.data));
|
|
24
|
-
}
|
|
25
|
-
json() {
|
|
26
|
-
this.validated();
|
|
27
|
-
return this.transform(this.data);
|
|
28
|
-
}
|
|
29
|
-
getErrors() {
|
|
30
|
-
return this.errors;
|
|
31
|
-
}
|
|
32
|
-
getByPath(path) {
|
|
33
|
-
return path.split('.').reduce((o, k) => (o && typeof o === 'object' ? o[k] : undefined), this.data);
|
|
34
|
-
}
|
|
35
|
-
setByPath(obj, path, value) {
|
|
36
|
-
const keys = path.split('.');
|
|
37
|
-
let current = obj;
|
|
38
|
-
for (let i = 0; i < keys.length - 1; i++) {
|
|
39
|
-
const k = keys[i];
|
|
40
|
-
if (!(k in current) || typeof current[k] !== 'object') {
|
|
41
|
-
current[k] = {};
|
|
42
|
-
}
|
|
43
|
-
current = current[k];
|
|
44
|
-
}
|
|
45
|
-
current[keys[(keys.length - 1)]] = value;
|
|
46
|
-
}
|
|
47
|
-
validated() {
|
|
48
|
-
this.isValid = true;
|
|
49
|
-
this.errors = {};
|
|
50
|
-
const definedRules = this.rules();
|
|
51
|
-
for (const key in definedRules) {
|
|
52
|
-
const fieldRules = definedRules[key] ?? [];
|
|
53
|
-
const value = this.getByPath(key);
|
|
54
|
-
for (const rule of fieldRules) {
|
|
55
|
-
const failMessage = rule({
|
|
56
|
-
val: value,
|
|
57
|
-
listValues: this.data,
|
|
58
|
-
key: key
|
|
59
|
-
});
|
|
60
|
-
if (failMessage) {
|
|
61
|
-
this.setByPath(this.errors, key, failMessage);
|
|
62
|
-
this.isValid = false;
|
|
63
|
-
break;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
if (!this.isValid) {
|
|
68
|
-
throw this;
|
|
69
|
-
}
|
|
70
|
-
return this;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './BaseRequest.js';
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './BaseRequest.js';
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { type CountryCode } from 'libphonenumber-js';
|
|
2
|
-
import type { BaseValidationMessages } from './messages/types.js';
|
|
3
|
-
import type { ValidationRuleResult, ValidationParams } from '../BaseRequest.js';
|
|
4
|
-
export type ValidationRuleParams<T extends keyof BaseValidationMessages, D = object> = D & {
|
|
5
|
-
message?: BaseValidationMessages[T];
|
|
6
|
-
};
|
|
7
|
-
export type LengthParams = {
|
|
8
|
-
length?: {
|
|
9
|
-
min?: number;
|
|
10
|
-
max?: number;
|
|
11
|
-
};
|
|
12
|
-
};
|
|
13
|
-
export type RangeParams = {
|
|
14
|
-
range?: {
|
|
15
|
-
min?: number;
|
|
16
|
-
max?: number;
|
|
17
|
-
};
|
|
18
|
-
};
|
|
19
|
-
type ArrayItemRules<Context> = ValidationRuleResult<Context>[] | Record<string, ValidationRuleResult<Context>[]>;
|
|
20
|
-
type ArrayParams<Context> = LengthParams & {
|
|
21
|
-
schema?: ArrayItemRules<Context>;
|
|
22
|
-
};
|
|
23
|
-
export declare class Rules<M extends BaseValidationMessages> {
|
|
24
|
-
private readonly validationMessages;
|
|
25
|
-
constructor(validationMessages: M);
|
|
26
|
-
private checkVal;
|
|
27
|
-
string<T = unknown, D = unknown>(params?: ValidationRuleParams<'string', LengthParams>): ValidationRuleResult<T, D>;
|
|
28
|
-
number<T = unknown, D = unknown>(params?: ValidationRuleParams<'number', RangeParams>): ValidationRuleResult<T, D>;
|
|
29
|
-
finite<T = unknown, D = unknown>(params?: ValidationRuleParams<'finite', RangeParams & {
|
|
30
|
-
maxDigitsAfterDot?: number;
|
|
31
|
-
}>): ValidationRuleResult<T, D>;
|
|
32
|
-
boolean<T = unknown, D = unknown>(params?: ValidationRuleParams<'boolean', {
|
|
33
|
-
expected?: boolean;
|
|
34
|
-
}>): ValidationRuleResult<T, D>;
|
|
35
|
-
array<T = unknown>(params?: ValidationRuleParams<'array', ArrayParams<T>>): ValidationRuleResult<T>;
|
|
36
|
-
phone<T = unknown, D = unknown>(params?: ValidationRuleParams<'phone', {
|
|
37
|
-
country: CountryCode;
|
|
38
|
-
}>): ValidationRuleResult<T, D>;
|
|
39
|
-
email<T = unknown, D = unknown>(params?: ValidationRuleParams<'email'>): ValidationRuleResult<T, D>;
|
|
40
|
-
lettersOnly<T = unknown, D = unknown>(params?: ValidationRuleParams<'lettersOnly', {
|
|
41
|
-
whiteSpaces?: boolean;
|
|
42
|
-
}>): ValidationRuleResult<T, D>;
|
|
43
|
-
allowedOnly<T = unknown, D = unknown>(params?: ValidationRuleParams<'allowedOnly', {
|
|
44
|
-
allowed?: unknown[];
|
|
45
|
-
}>): ValidationRuleResult<T, D>;
|
|
46
|
-
sameAs<T = unknown, D = unknown>(params: ValidationRuleParams<'sameAs', {
|
|
47
|
-
key: keyof T | string;
|
|
48
|
-
}>): ValidationRuleResult<T, D>;
|
|
49
|
-
notSameAs<T = unknown, D = unknown>(params: ValidationRuleParams<'sameAs', {
|
|
50
|
-
key: keyof T | string;
|
|
51
|
-
}>): ValidationRuleResult<T, D>;
|
|
52
|
-
required<T = unknown, D = unknown, J = unknown>(params?: ValidationRuleParams<'required', {
|
|
53
|
-
byCondition?: (params: ValidationParams<T, D, J>) => boolean;
|
|
54
|
-
}>): ValidationRuleResult<T, D, J>;
|
|
55
|
-
password<T = unknown, D = unknown>(params?: ValidationRuleParams<'password', {
|
|
56
|
-
length?: number;
|
|
57
|
-
specialChars?: boolean | number;
|
|
58
|
-
numbers?: boolean | number;
|
|
59
|
-
lowerUpperCasePattern?: boolean;
|
|
60
|
-
}>): ValidationRuleResult<T, D>;
|
|
61
|
-
}
|
|
62
|
-
export {};
|
|
@@ -1,288 +0,0 @@
|
|
|
1
|
-
import { isValidNumber } from 'libphonenumber-js';
|
|
2
|
-
export class Rules {
|
|
3
|
-
validationMessages;
|
|
4
|
-
constructor(validationMessages) {
|
|
5
|
-
this.validationMessages = validationMessages;
|
|
6
|
-
}
|
|
7
|
-
checkVal(val) {
|
|
8
|
-
return val !== undefined && val !== null;
|
|
9
|
-
}
|
|
10
|
-
string(params) {
|
|
11
|
-
const { message, length } = { ...params, message: { ...this.validationMessages.string, ...params?.message } };
|
|
12
|
-
return ({ val }) => {
|
|
13
|
-
if (this.checkVal(val)) {
|
|
14
|
-
if (typeof val !== 'string') {
|
|
15
|
-
return message.base();
|
|
16
|
-
}
|
|
17
|
-
switch (true) {
|
|
18
|
-
case length && this.checkVal(length.min) && val.length < length.min:
|
|
19
|
-
return message.min(length.min);
|
|
20
|
-
case length && this.checkVal(length.max) && val.length > length.max:
|
|
21
|
-
return message.max(length.max);
|
|
22
|
-
default:
|
|
23
|
-
return undefined;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
return undefined;
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
number(params) {
|
|
30
|
-
const { message, range } = { ...params, message: { ...this.validationMessages.number, ...params?.message } };
|
|
31
|
-
return ({ val }) => {
|
|
32
|
-
if (this.checkVal(val)) {
|
|
33
|
-
const numberVal = Number(val);
|
|
34
|
-
if (!Number.isInteger(numberVal) || Number.isNaN(numberVal)) {
|
|
35
|
-
return message.base();
|
|
36
|
-
}
|
|
37
|
-
switch (true) {
|
|
38
|
-
case range && this.checkVal(range?.min) && numberVal < range.min:
|
|
39
|
-
return message.min(range.min);
|
|
40
|
-
case range && this.checkVal(range?.max) && numberVal > range.max:
|
|
41
|
-
return message.max(range.max);
|
|
42
|
-
default:
|
|
43
|
-
return undefined;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
return undefined;
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
finite(params) {
|
|
50
|
-
const { message, maxDigitsAfterDot, range } = { ...params, message: { ...this.validationMessages.finite, ...params?.message } };
|
|
51
|
-
return ({ val }) => {
|
|
52
|
-
if (this.checkVal(val)) {
|
|
53
|
-
const numVal = Number(val);
|
|
54
|
-
if (!Number.isFinite(numVal) || String(val)[0] === '.') {
|
|
55
|
-
return message.base();
|
|
56
|
-
}
|
|
57
|
-
if (typeof maxDigitsAfterDot === 'number') {
|
|
58
|
-
const digitsAfterDot = val.toString().split('.')[1]?.length ?? 0;
|
|
59
|
-
if (digitsAfterDot > maxDigitsAfterDot) {
|
|
60
|
-
return message.maxDigitsAfterDot(maxDigitsAfterDot);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
if (range?.min !== undefined && numVal < range.min)
|
|
64
|
-
return message.min(range.min);
|
|
65
|
-
if (range?.max !== undefined && numVal > range.max)
|
|
66
|
-
return message.max(range.max);
|
|
67
|
-
return undefined;
|
|
68
|
-
}
|
|
69
|
-
return undefined;
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
boolean(params) {
|
|
73
|
-
const { message, expected } = { ...params, message: { ...this.validationMessages.boolean, ...params?.message } };
|
|
74
|
-
return ({ val }) => {
|
|
75
|
-
if (this.checkVal(val)) {
|
|
76
|
-
if (typeof val !== 'boolean')
|
|
77
|
-
return message.base();
|
|
78
|
-
if (expected !== undefined && val !== expected)
|
|
79
|
-
return message.expected(String(expected));
|
|
80
|
-
return undefined;
|
|
81
|
-
}
|
|
82
|
-
return undefined;
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
array(params) {
|
|
86
|
-
const { message, length = {}, schema } = { ...params, message: { ...this.validationMessages.array, ...params?.message } };
|
|
87
|
-
return ({ val, listValues, key }) => {
|
|
88
|
-
if (this.checkVal(val)) {
|
|
89
|
-
if (!Array.isArray(val))
|
|
90
|
-
return message.base();
|
|
91
|
-
if (length.min && val.length < length.min)
|
|
92
|
-
return message.min(length.min);
|
|
93
|
-
if (length.max && val.length > length.max)
|
|
94
|
-
return message.max(length.max);
|
|
95
|
-
if (schema) {
|
|
96
|
-
const nestedErrors = [];
|
|
97
|
-
for (let index = 0; index < val.length; index++) {
|
|
98
|
-
const element = val[index];
|
|
99
|
-
const itemErrors = {};
|
|
100
|
-
if (Array.isArray(schema)) {
|
|
101
|
-
for (const rule of schema) {
|
|
102
|
-
const fail = rule({
|
|
103
|
-
val: element,
|
|
104
|
-
listValues,
|
|
105
|
-
key: `${key}[${index}]`
|
|
106
|
-
});
|
|
107
|
-
if (fail) {
|
|
108
|
-
itemErrors['_error'] = fail;
|
|
109
|
-
break;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
else if (typeof schema === 'object' && schema !== null) {
|
|
114
|
-
if (typeof element !== 'object' || element === null) {
|
|
115
|
-
itemErrors['_error'] = message.base();
|
|
116
|
-
}
|
|
117
|
-
else {
|
|
118
|
-
for (const fieldKey in schema) {
|
|
119
|
-
const fieldRules = schema[fieldKey] ?? [];
|
|
120
|
-
const fieldValue = element?.[fieldKey];
|
|
121
|
-
for (const rule of fieldRules) {
|
|
122
|
-
const fail = rule({
|
|
123
|
-
val: fieldValue,
|
|
124
|
-
listValues,
|
|
125
|
-
key: `${key}[${index}].${fieldKey}`
|
|
126
|
-
});
|
|
127
|
-
if (fail) {
|
|
128
|
-
itemErrors[fieldKey] = fail;
|
|
129
|
-
break;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
nestedErrors[index] = itemErrors;
|
|
136
|
-
}
|
|
137
|
-
if (nestedErrors.some((e) => Object.keys(e).length > 0)) {
|
|
138
|
-
return nestedErrors;
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
return undefined;
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
|
-
phone(params) {
|
|
146
|
-
const { message, country = 'RU' } = { ...params, message: params?.message ?? this.validationMessages.phone };
|
|
147
|
-
return ({ val }) => {
|
|
148
|
-
if (this.checkVal(val)) {
|
|
149
|
-
return isValidNumber(String(val), country) ? undefined : message();
|
|
150
|
-
}
|
|
151
|
-
return undefined;
|
|
152
|
-
};
|
|
153
|
-
}
|
|
154
|
-
email(params) {
|
|
155
|
-
const { message } = { message: params?.message ?? this.validationMessages.email };
|
|
156
|
-
const emailRegExp =
|
|
157
|
-
// eslint-disable-next-line no-control-regex
|
|
158
|
-
/^(?:[A-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[A-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|[\x01-\x09\x0B\x0C\x0E-\x7F])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9]{2,}(?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21-\x5A\x53-\x7F]|\\[\x01-\x09\x0B\x0C\x0E-\x7F])+)])$/i;
|
|
159
|
-
return ({ val }) => {
|
|
160
|
-
if (this.checkVal(val)) {
|
|
161
|
-
const stringedVal = typeof val === 'string' ? String(val) : undefined;
|
|
162
|
-
return stringedVal && emailRegExp.test(stringedVal) ? undefined : message();
|
|
163
|
-
}
|
|
164
|
-
return undefined;
|
|
165
|
-
};
|
|
166
|
-
}
|
|
167
|
-
lettersOnly(params) {
|
|
168
|
-
const { message, whiteSpaces = false } = { ...params, message: params?.message ?? this.validationMessages.lettersOnly };
|
|
169
|
-
const lettersRegex = whiteSpaces ? /^[а-яА-Яa-zA-Z\s]+$/ : /^[а-яА-Яa-zA-Z]+$/;
|
|
170
|
-
return ({ val }) => {
|
|
171
|
-
if (this.checkVal(val)) {
|
|
172
|
-
const stringedVal = typeof val === 'string' ? String(val) : undefined;
|
|
173
|
-
return stringedVal && lettersRegex.test(stringedVal) ? undefined : message(whiteSpaces);
|
|
174
|
-
}
|
|
175
|
-
return undefined;
|
|
176
|
-
};
|
|
177
|
-
}
|
|
178
|
-
allowedOnly(params) {
|
|
179
|
-
const { message, allowed } = { ...params, message: params?.message ?? this.validationMessages.allowedOnly };
|
|
180
|
-
return ({ val }) => {
|
|
181
|
-
if (this.checkVal(val)) {
|
|
182
|
-
switch (true) {
|
|
183
|
-
case !!allowed && Array.isArray(allowed):
|
|
184
|
-
if (!allowed?.length) {
|
|
185
|
-
return message('');
|
|
186
|
-
}
|
|
187
|
-
return allowed.includes(val) ? undefined : message(allowed.join(', '));
|
|
188
|
-
default:
|
|
189
|
-
return undefined;
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
return undefined;
|
|
193
|
-
};
|
|
194
|
-
}
|
|
195
|
-
sameAs(params) {
|
|
196
|
-
const { message, key } = { ...params, message: params?.message ?? this.validationMessages.sameAs };
|
|
197
|
-
return ({ val, listValues }) => {
|
|
198
|
-
if (this.checkVal(val)) {
|
|
199
|
-
switch (true) {
|
|
200
|
-
case typeof val === 'object':
|
|
201
|
-
if (!listValues?.[key] || typeof listValues[key] !== 'object') {
|
|
202
|
-
return message(String(key));
|
|
203
|
-
}
|
|
204
|
-
try {
|
|
205
|
-
return JSON.stringify(val) === JSON.stringify(listValues[key]) ? undefined : message(String(key));
|
|
206
|
-
}
|
|
207
|
-
catch {
|
|
208
|
-
return undefined;
|
|
209
|
-
}
|
|
210
|
-
default:
|
|
211
|
-
return String(val ?? '') === String(listValues?.[key] ?? '') ? undefined : message(String(key));
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
return undefined;
|
|
215
|
-
};
|
|
216
|
-
}
|
|
217
|
-
notSameAs(params) {
|
|
218
|
-
const { message, key } = { ...params, message: params?.message ?? this.validationMessages.notSameAs };
|
|
219
|
-
return ({ val, listValues }) => {
|
|
220
|
-
if (this.checkVal(val)) {
|
|
221
|
-
switch (true) {
|
|
222
|
-
case typeof val === 'object':
|
|
223
|
-
if (!listValues?.[key] || typeof listValues[key] !== 'object') {
|
|
224
|
-
return message(String(key));
|
|
225
|
-
}
|
|
226
|
-
try {
|
|
227
|
-
return JSON.stringify(val) !== JSON.stringify(listValues[key]) ? undefined : message(String(key));
|
|
228
|
-
}
|
|
229
|
-
catch {
|
|
230
|
-
return undefined;
|
|
231
|
-
}
|
|
232
|
-
default:
|
|
233
|
-
return String(val) === String(listValues?.[key]) ? undefined : message(String(key));
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
return undefined;
|
|
237
|
-
};
|
|
238
|
-
}
|
|
239
|
-
required(params) {
|
|
240
|
-
const { message, byCondition } = { ...params, message: params?.message ?? this.validationMessages.required };
|
|
241
|
-
return ({ val, listValues, key }) => {
|
|
242
|
-
if (!this.checkVal(val))
|
|
243
|
-
return message();
|
|
244
|
-
if (byCondition && !byCondition({ val, listValues, key }))
|
|
245
|
-
return undefined;
|
|
246
|
-
if (typeof val === 'string' && !val.length)
|
|
247
|
-
return message();
|
|
248
|
-
if (val instanceof File && val.size < 1)
|
|
249
|
-
return message();
|
|
250
|
-
if (typeof val === 'number' && !String(val).length)
|
|
251
|
-
return message();
|
|
252
|
-
return undefined;
|
|
253
|
-
};
|
|
254
|
-
}
|
|
255
|
-
password(params) {
|
|
256
|
-
const { message, length = 8, specialChars, numbers, lowerUpperCasePattern } = { ...params, message: { ...this.validationMessages.password, ...params?.message } };
|
|
257
|
-
return ({ val }) => {
|
|
258
|
-
if (!this.checkVal(val))
|
|
259
|
-
return undefined;
|
|
260
|
-
const str = String(val);
|
|
261
|
-
if (str.length < length) {
|
|
262
|
-
return message.length(length);
|
|
263
|
-
}
|
|
264
|
-
if (specialChars) {
|
|
265
|
-
const minCount = typeof specialChars === 'number' ? specialChars : 1;
|
|
266
|
-
const count = (str.match(/[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]/g) || []).length;
|
|
267
|
-
if (count < minCount) {
|
|
268
|
-
return message.specialChars(minCount);
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
if (numbers) {
|
|
272
|
-
const minCount = typeof numbers === 'number' ? numbers : 1;
|
|
273
|
-
const count = (str.match(/[0-9]/g) || []).length;
|
|
274
|
-
if (count < minCount) {
|
|
275
|
-
return message.numbers(minCount);
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
if (lowerUpperCasePattern) {
|
|
279
|
-
const hasLower = /[a-z]/.test(str);
|
|
280
|
-
const hasUpper = /[A-Z]/.test(str);
|
|
281
|
-
if (!hasLower || !hasUpper) {
|
|
282
|
-
return message.lowerUpperCasePattern();
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
return undefined;
|
|
286
|
-
};
|
|
287
|
-
}
|
|
288
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './Rules.js';
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './Rules.js';
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
export const validationMessagesEn = {
|
|
2
|
-
phone: () => 'Invalid phone number format',
|
|
3
|
-
email: () => 'Invalid email address',
|
|
4
|
-
required: () => 'This field is required',
|
|
5
|
-
lettersOnly: (whiteSpaces) => `This field may contain letters only${whiteSpaces ? '' : ' and must not contain spaces'}`,
|
|
6
|
-
allowedOnly: (value) => value && value.length ? `This field may contain only one of the following values: ${value}` : 'This field has no allowed values',
|
|
7
|
-
sameAs: (value) => `This field must match the ${value} field`,
|
|
8
|
-
notSameAs: (value) => `This field must not match the ${value} field`,
|
|
9
|
-
boolean: {
|
|
10
|
-
base: () => 'This field must be a boolean value',
|
|
11
|
-
expected: (value) => `Expected value: ${value}`
|
|
12
|
-
},
|
|
13
|
-
finite: {
|
|
14
|
-
base: () => 'This field must be a number',
|
|
15
|
-
min: (value) => `The number must be at least ${value}`,
|
|
16
|
-
max: (value) => `The number must be at most ${value}`,
|
|
17
|
-
maxDigitsAfterDot: (value) => `Number of digits after the decimal point must not exceed ${value}`
|
|
18
|
-
},
|
|
19
|
-
number: {
|
|
20
|
-
base: () => 'This field must be an integer',
|
|
21
|
-
min: (value) => `The number must be at least ${value}`,
|
|
22
|
-
max: (value) => `The number must be at most ${value}`
|
|
23
|
-
},
|
|
24
|
-
string: {
|
|
25
|
-
base: () => 'This field must be a string',
|
|
26
|
-
min: (value) => `Minimum string length is ${value}`,
|
|
27
|
-
max: (value) => `Maximum string length is ${value}`
|
|
28
|
-
},
|
|
29
|
-
array: {
|
|
30
|
-
base: () => 'This field must be an array',
|
|
31
|
-
min: (value) => `Minimum array length is ${value}`,
|
|
32
|
-
max: (value) => `Maximum array length is ${value}`
|
|
33
|
-
},
|
|
34
|
-
password: {
|
|
35
|
-
length: (value) => `Minimum password length is ${value}`,
|
|
36
|
-
specialChars: (value) => `Password must contain at least ${value} special character(s)`,
|
|
37
|
-
lowerUpperCasePattern: () => 'Password must contain both uppercase and lowercase letters',
|
|
38
|
-
numbers: (value) => `Password must contain at least ${value} number(s)`
|
|
39
|
-
}
|
|
40
|
-
};
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
export const validationMessagesRu = {
|
|
2
|
-
phone: () => 'Неверный формат номера телефона',
|
|
3
|
-
email: () => 'Невалидный email',
|
|
4
|
-
required: () => 'Поле обязательно к заполнению',
|
|
5
|
-
lettersOnly: (whiteSpaces) => `Поле может содержать только буквы ${whiteSpaces ? '' : 'и не должно содержать пробелы'}`,
|
|
6
|
-
allowedOnly: (value) => value && value.length ? `Поле может содержать только одно из следующих значений:: ${value}` : 'Поле не имеет допустимых значений',
|
|
7
|
-
sameAs: (value) => `Поле должно совпадать с полем ${value}`,
|
|
8
|
-
notSameAs: (value) => `Поле не должно совпадать с полем ${value}`,
|
|
9
|
-
boolean: {
|
|
10
|
-
base: () => 'Поле должно содержать значение типа boolean',
|
|
11
|
-
expected: (value) => `Ожидаемое значение поля - ${value}`
|
|
12
|
-
},
|
|
13
|
-
finite: {
|
|
14
|
-
base: () => 'Поле должно быть числом',
|
|
15
|
-
min: (value) => `Число должно быть не менее ${value}`,
|
|
16
|
-
max: (value) => `Число должно быть не более ${value}`,
|
|
17
|
-
maxDigitsAfterDot: (value) => `Количество символов после точки не более ${value}`
|
|
18
|
-
},
|
|
19
|
-
number: {
|
|
20
|
-
base: () => 'Поле должно быть целым числом',
|
|
21
|
-
min: (value) => `Число должно быть не менее ${value}`,
|
|
22
|
-
max: (value) => `Число должно быть не более ${value}`
|
|
23
|
-
},
|
|
24
|
-
string: {
|
|
25
|
-
base: () => 'Поле должно быть строкой',
|
|
26
|
-
min: (value) => `Минимальная длина строки - ${value}`,
|
|
27
|
-
max: (value) => `Максимальная длина строки - ${value}`
|
|
28
|
-
},
|
|
29
|
-
array: {
|
|
30
|
-
base: () => 'Поле должно быть массивом',
|
|
31
|
-
min: (value) => `Минимальная длина массива - ${value}`,
|
|
32
|
-
max: (value) => `Максимальная длина массива - ${value}`
|
|
33
|
-
},
|
|
34
|
-
password: {
|
|
35
|
-
length: (value) => `Минимальная длина пароля - ${value}`,
|
|
36
|
-
specialChars: (value) => `В пароле должно быть спецсимволы в количестве не менее ${value}`,
|
|
37
|
-
lowerUpperCasePattern: () => 'В пароле должны быть буквы верхнего и нижнего регистра',
|
|
38
|
-
numbers: (value) => `В пароле должно быть цифры в количестве не менее ${value}`
|
|
39
|
-
}
|
|
40
|
-
};
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
export const validationMessagesI18n = {
|
|
2
|
-
phone: () => 'validation.phone',
|
|
3
|
-
email: () => 'validation.email',
|
|
4
|
-
required: () => 'validation.required',
|
|
5
|
-
lettersOnly: (whiteSpaces) => (whiteSpaces ? 'validation.lettersOnlyWithWhiteSpaces' : 'validation.lettersOnly'),
|
|
6
|
-
allowedOnly: (value) => ({ key: value && value.length ? 'validation.allowedOnly.base' : 'validation.allowedOnly.nothing', vars: { value } }),
|
|
7
|
-
sameAs: (value) => ({ key: 'validation.sameAs', vars: { value } }),
|
|
8
|
-
notSameAs: (value) => ({ key: 'validation.notSameAs', vars: { value } }),
|
|
9
|
-
boolean: {
|
|
10
|
-
base: () => 'validation.boolean.base',
|
|
11
|
-
expected: (value) => ({ key: 'validation.boolean.expected', vars: { value } })
|
|
12
|
-
},
|
|
13
|
-
finite: {
|
|
14
|
-
base: () => 'validation.finite.base',
|
|
15
|
-
min: (value) => ({ key: 'validation.finite.minLength', vars: { value } }),
|
|
16
|
-
max: (value) => ({ key: 'validation.finite.maxLength', vars: { value } }),
|
|
17
|
-
maxDigitsAfterDot: (value) => ({ key: 'validation.finite.maxDigitsAfterDot', vars: { value } })
|
|
18
|
-
},
|
|
19
|
-
number: {
|
|
20
|
-
base: () => 'validation.number.base',
|
|
21
|
-
min: (value) => ({ key: 'validation.number.minLength', vars: { value } }),
|
|
22
|
-
max: (value) => ({ key: 'validation.number.maxLength', vars: { value } })
|
|
23
|
-
},
|
|
24
|
-
string: {
|
|
25
|
-
base: () => 'validation.string.base',
|
|
26
|
-
min: (value) => ({ key: 'validation.string.minLength', vars: { value } }),
|
|
27
|
-
max: (value) => ({ key: 'validation.string.maxLength', vars: { value } })
|
|
28
|
-
},
|
|
29
|
-
array: {
|
|
30
|
-
base: () => 'validation.array.base',
|
|
31
|
-
min: (value) => ({ key: 'validation.array.minLength', vars: { value } }),
|
|
32
|
-
max: (value) => ({ key: 'validation.array.maxLength', vars: { value } })
|
|
33
|
-
},
|
|
34
|
-
password: {
|
|
35
|
-
length: (value) => ({ key: 'validation.password.length', vars: { value } }),
|
|
36
|
-
specialChars: (value) => ({ key: 'validation.password.specialChars', vars: { value } }),
|
|
37
|
-
lowerUpperCasePattern: () => ({ key: 'validation.password.lowerUpperCasePattern' }),
|
|
38
|
-
numbers: (value) => ({ key: 'validation.password.numbers', vars: { value } })
|
|
39
|
-
}
|
|
40
|
-
};
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import type { ValidationMessage } from '../../index.js';
|
|
2
|
-
export type BaseValidationMessages = {
|
|
3
|
-
phone: () => ValidationMessage;
|
|
4
|
-
email: () => ValidationMessage;
|
|
5
|
-
required: () => ValidationMessage;
|
|
6
|
-
lettersOnly: (whiteSpaces: boolean) => ValidationMessage;
|
|
7
|
-
allowedOnly: (allowed: string) => ValidationMessage;
|
|
8
|
-
sameAs: (value: string) => ValidationMessage;
|
|
9
|
-
notSameAs: (value: string) => ValidationMessage;
|
|
10
|
-
boolean: {
|
|
11
|
-
base: () => ValidationMessage;
|
|
12
|
-
expected: (value: string) => ValidationMessage;
|
|
13
|
-
};
|
|
14
|
-
finite: {
|
|
15
|
-
base: () => ValidationMessage;
|
|
16
|
-
min: (value: number) => ValidationMessage;
|
|
17
|
-
max: (value: number) => ValidationMessage;
|
|
18
|
-
maxDigitsAfterDot: (value: number) => ValidationMessage;
|
|
19
|
-
};
|
|
20
|
-
number: {
|
|
21
|
-
base: () => ValidationMessage;
|
|
22
|
-
min: (value: number) => ValidationMessage;
|
|
23
|
-
max: (value: number) => ValidationMessage;
|
|
24
|
-
};
|
|
25
|
-
string: {
|
|
26
|
-
base: () => ValidationMessage;
|
|
27
|
-
min: (value: number) => ValidationMessage;
|
|
28
|
-
max: (value: number) => ValidationMessage;
|
|
29
|
-
};
|
|
30
|
-
array: {
|
|
31
|
-
base: () => ValidationMessage;
|
|
32
|
-
min: (value: number) => ValidationMessage;
|
|
33
|
-
max: (value: number) => ValidationMessage;
|
|
34
|
-
};
|
|
35
|
-
password: {
|
|
36
|
-
length: (value: number) => ValidationMessage;
|
|
37
|
-
specialChars: (value: number) => ValidationMessage;
|
|
38
|
-
lowerUpperCasePattern: () => ValidationMessage;
|
|
39
|
-
numbers: (value: number) => ValidationMessage;
|
|
40
|
-
};
|
|
41
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|