@astral/validations 4.2.0 → 4.4.0
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/README.md +20 -0
- package/guid/constants.d.ts +3 -0
- package/guid/constants.js +6 -0
- package/guid/guid.d.ts +16 -0
- package/guid/guid.js +16 -0
- package/guid/index.d.ts +2 -0
- package/guid/index.js +2 -0
- package/index.d.ts +2 -1
- package/index.js +2 -1
- package/kpp/constants.d.ts +2 -0
- package/kpp/constants.js +10 -2
- package/kpp/kpp.js +14 -4
- package/package.json +1 -1
package/README.md
CHANGED
@@ -25,6 +25,7 @@
|
|
25
25
|
- [min](#min-string)
|
26
26
|
- [max](#max-string)
|
27
27
|
- [email](#email)
|
28
|
+
- [guid](#guid)
|
28
29
|
- [pattern](#pattern)
|
29
30
|
- [onlyNumber](#onlyNumber)
|
30
31
|
- [snils](#snils)
|
@@ -386,6 +387,25 @@ validateEmail('longlonglong.......')
|
|
386
387
|
|
387
388
|
---
|
388
389
|
|
390
|
+
### guid
|
391
|
+
|
392
|
+
Проверяет валиден ли GUID.
|
393
|
+
|
394
|
+
```ts
|
395
|
+
import { string, guid } from '@astral/validations';
|
396
|
+
|
397
|
+
const validate = string(guid());
|
398
|
+
|
399
|
+
// undefined
|
400
|
+
validate('C56A4180-65AA-42EC-A945-5FD21DEC0538');
|
401
|
+
|
402
|
+
|
403
|
+
// { message: 'Некорректный GUID' }
|
404
|
+
validate('x56a4180-h5aa-42ec-a945-5fd21dec0538');
|
405
|
+
```
|
406
|
+
|
407
|
+
---
|
408
|
+
|
389
409
|
### pattern
|
390
410
|
|
391
411
|
Проверяет строку на соответствие регулярному выражению.
|
@@ -0,0 +1,6 @@
|
|
1
|
+
import { createErrorCode } from '../core';
|
2
|
+
export const GUID_REGEXP = /^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$/;
|
3
|
+
export const INVALID_GUID_ERROR_INFO = {
|
4
|
+
code: createErrorCode('guid-invalid'),
|
5
|
+
message: 'Некорректный GUID',
|
6
|
+
};
|
package/guid/guid.d.ts
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
type GuidParams = {
|
2
|
+
/**
|
3
|
+
* @description Замена стандартного сообщения ошибки.
|
4
|
+
*/
|
5
|
+
message?: string;
|
6
|
+
};
|
7
|
+
/**
|
8
|
+
* @description Проверяет валидность GUID.
|
9
|
+
* @example
|
10
|
+
* ```ts
|
11
|
+
* const validate = string(guid());
|
12
|
+
* validate('C56A4180-65AA-42EC-A945-5FD21DEC0538');
|
13
|
+
* ```
|
14
|
+
*/
|
15
|
+
export declare const guid: <TLastSchemaValues extends Record<string, unknown>>(params?: GuidParams) => (value: string, prevCtx?: import("../core").ValidationContext<TLastSchemaValues> | undefined) => import("../core").ValidationResult;
|
16
|
+
export {};
|
package/guid/guid.js
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
import { createRule } from '../core';
|
2
|
+
import { GUID_REGEXP, INVALID_GUID_ERROR_INFO } from './constants';
|
3
|
+
/**
|
4
|
+
* @description Проверяет валидность GUID.
|
5
|
+
* @example
|
6
|
+
* ```ts
|
7
|
+
* const validate = string(guid());
|
8
|
+
* validate('C56A4180-65AA-42EC-A945-5FD21DEC0538');
|
9
|
+
* ```
|
10
|
+
*/
|
11
|
+
export const guid = (params) => createRule((value, ctx) => {
|
12
|
+
if (!GUID_REGEXP.test(value)) {
|
13
|
+
return ctx.createError(Object.assign(Object.assign({}, INVALID_GUID_ERROR_INFO), { message: (params === null || params === void 0 ? void 0 : params.message) || INVALID_GUID_ERROR_INFO.message }));
|
14
|
+
}
|
15
|
+
return undefined;
|
16
|
+
});
|
package/guid/index.d.ts
ADDED
package/guid/index.js
ADDED
package/index.d.ts
CHANGED
@@ -16,10 +16,11 @@ export { pattern, PATTERN_ERROR_CODE } from './pattern';
|
|
16
16
|
export { onlyNumber, ONLY_NUMBER_ERROR_CODE } from './onlyNumber';
|
17
17
|
export { toPlainError } from './toPlainError';
|
18
18
|
export { email, LENGTH_EMAIL_ERROR_INFO, INVALID_EMAIL_ERROR_INFO, } from './email';
|
19
|
+
export { guid, INVALID_GUID_ERROR_INFO } from './guid';
|
19
20
|
export { mobilePhone, MOBILE_PHONE_ERROR_INFO } from './mobilePhone';
|
20
21
|
export { innUL, INN_UL_ERROR_INFO } from './innUL';
|
21
22
|
export { innIP, INN_IP_ERROR_INFO } from './innIP';
|
22
|
-
export { kpp, INVALID_KPP_ERROR_INFO } from './kpp';
|
23
|
+
export { kpp, INVALID_KPP_ERROR_INFO, KPP_DOUBLE_ZERO_START_ERROR_INFO, KPP_ZEROS_ONLY_ERROR_INFO, } from './kpp';
|
23
24
|
export { snils, SNILS_ERROR_INFO } from './snils';
|
24
25
|
export { createRule, REQUIRED_ERROR_INFO, type ValidationRule } from './core';
|
25
26
|
export { ogrnUL, OGRN_UL_ERROR_INFO } from './ogrnUL';
|
package/index.js
CHANGED
@@ -16,10 +16,11 @@ export { pattern, PATTERN_ERROR_CODE } from './pattern';
|
|
16
16
|
export { onlyNumber, ONLY_NUMBER_ERROR_CODE } from './onlyNumber';
|
17
17
|
export { toPlainError } from './toPlainError';
|
18
18
|
export { email, LENGTH_EMAIL_ERROR_INFO, INVALID_EMAIL_ERROR_INFO, } from './email';
|
19
|
+
export { guid, INVALID_GUID_ERROR_INFO } from './guid';
|
19
20
|
export { mobilePhone, MOBILE_PHONE_ERROR_INFO } from './mobilePhone';
|
20
21
|
export { innUL, INN_UL_ERROR_INFO } from './innUL';
|
21
22
|
export { innIP, INN_IP_ERROR_INFO } from './innIP';
|
22
|
-
export { kpp, INVALID_KPP_ERROR_INFO } from './kpp';
|
23
|
+
export { kpp, INVALID_KPP_ERROR_INFO, KPP_DOUBLE_ZERO_START_ERROR_INFO, KPP_ZEROS_ONLY_ERROR_INFO, } from './kpp';
|
23
24
|
export { snils, SNILS_ERROR_INFO } from './snils';
|
24
25
|
export { createRule, REQUIRED_ERROR_INFO } from './core';
|
25
26
|
export { ogrnUL, OGRN_UL_ERROR_INFO } from './ogrnUL';
|
package/kpp/constants.d.ts
CHANGED
package/kpp/constants.js
CHANGED
@@ -1,6 +1,14 @@
|
|
1
1
|
import { createErrorCode } from '../core';
|
2
2
|
export const INVALID_KPP_ERROR_INFO = {
|
3
|
-
code: createErrorCode('kpp-invalid'),
|
4
|
-
message: '
|
3
|
+
code: createErrorCode('kpp-invalid-pattern'),
|
4
|
+
message: 'Проверьте КПП',
|
5
|
+
};
|
6
|
+
export const KPP_ZEROS_ONLY_ERROR_INFO = {
|
7
|
+
code: createErrorCode('kpp-invalid-zeros-only'),
|
8
|
+
message: 'Не может состоять только из нулей',
|
9
|
+
};
|
10
|
+
export const KPP_DOUBLE_ZERO_START_ERROR_INFO = {
|
11
|
+
code: createErrorCode('kpp-invalid-double-zero-start'),
|
12
|
+
message: 'Не может начинаться с 00',
|
5
13
|
};
|
6
14
|
export const KPP_REGEX = /^(\d{4}([A-Z]|\d){2}\d{3})$/;
|
package/kpp/kpp.js
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
import { createRule, isStringOfZeros } from '../core';
|
2
|
-
import { INVALID_KPP_ERROR_INFO, KPP_REGEX } from './constants';
|
1
|
+
import { createRule, isNoDoubleZeroStart, isStringOfZeros, } from '../core';
|
2
|
+
import { INVALID_KPP_ERROR_INFO, KPP_DOUBLE_ZERO_START_ERROR_INFO, KPP_REGEX, KPP_ZEROS_ONLY_ERROR_INFO, } from './constants';
|
3
3
|
/**
|
4
4
|
* @description Проверяет валидность кода КПП
|
5
5
|
* @example
|
@@ -9,8 +9,18 @@ import { INVALID_KPP_ERROR_INFO, KPP_REGEX } from './constants';
|
|
9
9
|
* ```
|
10
10
|
*/
|
11
11
|
export const kpp = (params) => createRule((value, ctx) => {
|
12
|
-
|
13
|
-
|
12
|
+
const createKppError = (errorInfoObj) => ctx.createError({
|
13
|
+
message: (params === null || params === void 0 ? void 0 : params.message) || errorInfoObj.message,
|
14
|
+
code: errorInfoObj.code,
|
15
|
+
});
|
16
|
+
if (!KPP_REGEX.test(value)) {
|
17
|
+
return createKppError(INVALID_KPP_ERROR_INFO);
|
18
|
+
}
|
19
|
+
if (isStringOfZeros(value)) {
|
20
|
+
return createKppError(KPP_ZEROS_ONLY_ERROR_INFO);
|
21
|
+
}
|
22
|
+
if (!isNoDoubleZeroStart(value)) {
|
23
|
+
return createKppError(KPP_DOUBLE_ZERO_START_ERROR_INFO);
|
14
24
|
}
|
15
25
|
return undefined;
|
16
26
|
}, { exclude: params === null || params === void 0 ? void 0 : params.exclude });
|