@astral/validations 0.1.0 → 1.19.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/compose/compose.d.ts +7 -0
- package/compose/compose.js +10 -0
- package/compose/index.d.ts +1 -0
- package/{isString → compose}/index.js +1 -1
- package/composeAllSettled/composeAllSettled.d.ts +7 -0
- package/composeAllSettled/composeAllSettled.js +25 -0
- package/composeAllSettled/index.d.ts +1 -0
- package/composeAllSettled/index.js +17 -0
- package/composeToString/composeToString.d.ts +8 -0
- package/composeToString/composeToString.js +18 -0
- package/composeToString/index.d.ts +1 -0
- package/composeToString/index.js +17 -0
- package/createRule/createRule.d.ts +1 -1
- package/createRule/createRule.js +2 -2
- package/esm/compose/compose.d.ts +7 -0
- package/esm/compose/compose.js +7 -0
- package/esm/compose/index.d.ts +1 -0
- package/esm/compose/index.js +1 -0
- package/esm/composeAllSettled/composeAllSettled.d.ts +7 -0
- package/esm/composeAllSettled/composeAllSettled.js +22 -0
- package/esm/composeAllSettled/index.d.ts +1 -0
- package/esm/composeAllSettled/index.js +1 -0
- package/esm/composeToString/composeToString.d.ts +8 -0
- package/esm/composeToString/composeToString.js +14 -0
- package/esm/composeToString/index.d.ts +1 -0
- package/esm/composeToString/index.js +1 -0
- package/esm/createRule/createRule.d.ts +1 -1
- package/esm/createRule/createRule.js +2 -2
- package/esm/index.d.ts +6 -0
- package/esm/index.js +6 -0
- package/esm/isRequired/index.d.ts +1 -0
- package/esm/isRequired/index.js +1 -0
- package/esm/isRequired/isRequired.d.ts +8 -0
- package/esm/isRequired/isRequired.js +24 -0
- package/esm/types.d.ts +4 -1
- package/esm/yupAdapter/index.d.ts +1 -0
- package/esm/yupAdapter/index.js +1 -0
- package/esm/yupAdapter/yupAdapter.d.ts +8 -0
- package/esm/yupAdapter/yupAdapter.js +11 -0
- package/index.d.ts +6 -0
- package/index.js +6 -0
- package/isRequired/index.d.ts +1 -0
- package/isRequired/index.js +17 -0
- package/isRequired/isRequired.d.ts +8 -0
- package/isRequired/isRequired.js +30 -0
- package/package.json +14 -4
- package/types.d.ts +4 -1
- package/yupAdapter/index.d.ts +1 -0
- package/yupAdapter/index.js +17 -0
- package/yupAdapter/yupAdapter.d.ts +8 -0
- package/yupAdapter/yupAdapter.js +15 -0
- package/createRule/createRule.test.d.ts +0 -1
- package/createRule/createRule.test.js +0 -9
- package/esm/createRule/createRule.test.d.ts +0 -1
- package/esm/createRule/createRule.test.js +0 -7
- package/esm/isString/index.d.ts +0 -1
- package/esm/isString/index.js +0 -1
- package/esm/isString/isString.d.ts +0 -6
- package/esm/isString/isString.js +0 -12
- package/esm/isString/isString.test.d.ts +0 -1
- package/esm/isString/isString.test.js +0 -13
- package/isString/index.d.ts +0 -1
- package/isString/isString.d.ts +0 -6
- package/isString/isString.js +0 -15
- package/isString/isString.test.d.ts +0 -1
- package/isString/isString.test.js +0 -15
@@ -0,0 +1,7 @@
|
|
1
|
+
import { InitializedRule } from '../types';
|
2
|
+
/**
|
3
|
+
* @description Объединяет переданные правила в цепочку правил, останавливает выполнение цепочки, если появилась ошибка. Выполняет правила слева направо
|
4
|
+
* @example compose(isRequired(), isEmail());
|
5
|
+
* @example compose(isRequired(), compose(isIncludeDot(), isIncludeComma()));
|
6
|
+
*/
|
7
|
+
export declare const compose: import("../types").Rule<InitializedRule[]>;
|
@@ -0,0 +1,10 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.compose = void 0;
|
4
|
+
const createRule_1 = require("../createRule");
|
5
|
+
/**
|
6
|
+
* @description Объединяет переданные правила в цепочку правил, останавливает выполнение цепочки, если появилась ошибка. Выполняет правила слева направо
|
7
|
+
* @example compose(isRequired(), isEmail());
|
8
|
+
* @example compose(isRequired(), compose(isIncludeDot(), isIncludeComma()));
|
9
|
+
*/
|
10
|
+
exports.compose = (0, createRule_1.createRule)((...rules) => (value) => rules.reduce((result, rule) => result || rule(value), undefined));
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './compose';
|
@@ -14,4 +14,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
15
|
};
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
-
__exportStar(require("./
|
17
|
+
__exportStar(require("./compose"), exports);
|
@@ -0,0 +1,7 @@
|
|
1
|
+
import { InitializedRule } from '../types';
|
2
|
+
/**
|
3
|
+
* @description Объединяет переданные правила в цепочку правил, выполняя все переданные правила, независимо от результата выполнения правил. Выполняет правила слева направо
|
4
|
+
* @example composeAllSettled(isIncludeDot(), isIncludeComma());
|
5
|
+
* @example compose(isRequired(), composeAllSettled(isIncludeDot(), isIncludeComma()));
|
6
|
+
*/
|
7
|
+
export declare const composeAllSettled: import("../types").Rule<InitializedRule[]>;
|
@@ -0,0 +1,25 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.composeAllSettled = void 0;
|
4
|
+
const createRule_1 = require("../createRule");
|
5
|
+
/**
|
6
|
+
* @description Объединяет переданные правила в цепочку правил, выполняя все переданные правила, независимо от результата выполнения правил. Выполняет правила слева направо
|
7
|
+
* @example composeAllSettled(isIncludeDot(), isIncludeComma());
|
8
|
+
* @example compose(isRequired(), composeAllSettled(isIncludeDot(), isIncludeComma()));
|
9
|
+
*/
|
10
|
+
exports.composeAllSettled = (0, createRule_1.createRule)((...rules) => (value) => {
|
11
|
+
const errors = [];
|
12
|
+
rules.forEach((rule) => {
|
13
|
+
const error = rule(value);
|
14
|
+
if (Array.isArray(error)) {
|
15
|
+
errors.push(...error);
|
16
|
+
}
|
17
|
+
if (typeof error === 'string') {
|
18
|
+
errors.push(error);
|
19
|
+
}
|
20
|
+
});
|
21
|
+
if (!errors.length) {
|
22
|
+
return undefined;
|
23
|
+
}
|
24
|
+
return errors.length === 1 ? errors[0] : errors;
|
25
|
+
});
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './composeAllSettled';
|
@@ -0,0 +1,17 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
|
+
};
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
+
__exportStar(require("./composeAllSettled"), exports);
|
@@ -0,0 +1,8 @@
|
|
1
|
+
import { InitializedRule } from '../types';
|
2
|
+
/**
|
3
|
+
* @description Объединяет правила в цепочку и форматирует результат в одну строку. Останаваливается при возникновении ошибки. Работает слева направо
|
4
|
+
* Подходит в качестве адаптера к react-final-form
|
5
|
+
* @example composeToString(isIncludeDot(), isIncludeComma());
|
6
|
+
* @example composeToString(isRequired(), composeToString(isIncludeDot(), isIncludeComma()));
|
7
|
+
*/
|
8
|
+
export declare const composeToString: (...rules: InitializedRule[]) => (value?: unknown) => string | undefined;
|
@@ -0,0 +1,18 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.composeToString = void 0;
|
4
|
+
const compose_1 = require("../compose");
|
5
|
+
/**
|
6
|
+
* @description Объединяет правила в цепочку и форматирует результат в одну строку. Останаваливается при возникновении ошибки. Работает слева направо
|
7
|
+
* Подходит в качестве адаптера к react-final-form
|
8
|
+
* @example composeToString(isIncludeDot(), isIncludeComma());
|
9
|
+
* @example composeToString(isRequired(), composeToString(isIncludeDot(), isIncludeComma()));
|
10
|
+
*/
|
11
|
+
const composeToString = (...rules) => (value) => {
|
12
|
+
const validationResult = (0, compose_1.compose)(...rules)(value);
|
13
|
+
if (Array.isArray(validationResult)) {
|
14
|
+
return validationResult[0];
|
15
|
+
}
|
16
|
+
return validationResult;
|
17
|
+
};
|
18
|
+
exports.composeToString = composeToString;
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './composeToString';
|
@@ -0,0 +1,17 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
|
+
};
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
+
__exportStar(require("./composeToString"), exports);
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { Rule } from '../types';
|
2
2
|
/**
|
3
|
-
* @example createRule((max: number, message: string) => value => value > max ? message : undefined)
|
4
3
|
* @description Фабрика по созданию правил валидации.
|
4
|
+
* @example createRule((max: number, message: string) => value => value > max ? message : undefined)
|
5
5
|
*/
|
6
6
|
export declare const createRule: <Args extends unknown[]>(creator: Rule<Args>) => Rule<Args>;
|
package/createRule/createRule.js
CHANGED
@@ -2,8 +2,8 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.createRule = void 0;
|
4
4
|
/**
|
5
|
-
* @example createRule((max: number, message: string) => value => value > max ? message : undefined)
|
6
5
|
* @description Фабрика по созданию правил валидации.
|
6
|
+
* @example createRule((max: number, message: string) => value => value > max ? message : undefined)
|
7
7
|
*/
|
8
|
-
|
8
|
+
const createRule = (creator) => creator;
|
9
9
|
exports.createRule = createRule;
|
@@ -0,0 +1,7 @@
|
|
1
|
+
import { InitializedRule } from '../types';
|
2
|
+
/**
|
3
|
+
* @description Объединяет переданные правила в цепочку правил, останавливает выполнение цепочки, если появилась ошибка. Выполняет правила слева направо
|
4
|
+
* @example compose(isRequired(), isEmail());
|
5
|
+
* @example compose(isRequired(), compose(isIncludeDot(), isIncludeComma()));
|
6
|
+
*/
|
7
|
+
export declare const compose: import("../types").Rule<InitializedRule[]>;
|
@@ -0,0 +1,7 @@
|
|
1
|
+
import { createRule } from '../createRule';
|
2
|
+
/**
|
3
|
+
* @description Объединяет переданные правила в цепочку правил, останавливает выполнение цепочки, если появилась ошибка. Выполняет правила слева направо
|
4
|
+
* @example compose(isRequired(), isEmail());
|
5
|
+
* @example compose(isRequired(), compose(isIncludeDot(), isIncludeComma()));
|
6
|
+
*/
|
7
|
+
export const compose = createRule((...rules) => (value) => rules.reduce((result, rule) => result || rule(value), undefined));
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './compose';
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './compose';
|
@@ -0,0 +1,7 @@
|
|
1
|
+
import { InitializedRule } from '../types';
|
2
|
+
/**
|
3
|
+
* @description Объединяет переданные правила в цепочку правил, выполняя все переданные правила, независимо от результата выполнения правил. Выполняет правила слева направо
|
4
|
+
* @example composeAllSettled(isIncludeDot(), isIncludeComma());
|
5
|
+
* @example compose(isRequired(), composeAllSettled(isIncludeDot(), isIncludeComma()));
|
6
|
+
*/
|
7
|
+
export declare const composeAllSettled: import("../types").Rule<InitializedRule[]>;
|
@@ -0,0 +1,22 @@
|
|
1
|
+
import { createRule } from '../createRule';
|
2
|
+
/**
|
3
|
+
* @description Объединяет переданные правила в цепочку правил, выполняя все переданные правила, независимо от результата выполнения правил. Выполняет правила слева направо
|
4
|
+
* @example composeAllSettled(isIncludeDot(), isIncludeComma());
|
5
|
+
* @example compose(isRequired(), composeAllSettled(isIncludeDot(), isIncludeComma()));
|
6
|
+
*/
|
7
|
+
export const composeAllSettled = createRule((...rules) => (value) => {
|
8
|
+
const errors = [];
|
9
|
+
rules.forEach((rule) => {
|
10
|
+
const error = rule(value);
|
11
|
+
if (Array.isArray(error)) {
|
12
|
+
errors.push(...error);
|
13
|
+
}
|
14
|
+
if (typeof error === 'string') {
|
15
|
+
errors.push(error);
|
16
|
+
}
|
17
|
+
});
|
18
|
+
if (!errors.length) {
|
19
|
+
return undefined;
|
20
|
+
}
|
21
|
+
return errors.length === 1 ? errors[0] : errors;
|
22
|
+
});
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './composeAllSettled';
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './composeAllSettled';
|
@@ -0,0 +1,8 @@
|
|
1
|
+
import { InitializedRule } from '../types';
|
2
|
+
/**
|
3
|
+
* @description Объединяет правила в цепочку и форматирует результат в одну строку. Останаваливается при возникновении ошибки. Работает слева направо
|
4
|
+
* Подходит в качестве адаптера к react-final-form
|
5
|
+
* @example composeToString(isIncludeDot(), isIncludeComma());
|
6
|
+
* @example composeToString(isRequired(), composeToString(isIncludeDot(), isIncludeComma()));
|
7
|
+
*/
|
8
|
+
export declare const composeToString: (...rules: InitializedRule[]) => (value?: unknown) => string | undefined;
|
@@ -0,0 +1,14 @@
|
|
1
|
+
import { compose } from '../compose';
|
2
|
+
/**
|
3
|
+
* @description Объединяет правила в цепочку и форматирует результат в одну строку. Останаваливается при возникновении ошибки. Работает слева направо
|
4
|
+
* Подходит в качестве адаптера к react-final-form
|
5
|
+
* @example composeToString(isIncludeDot(), isIncludeComma());
|
6
|
+
* @example composeToString(isRequired(), composeToString(isIncludeDot(), isIncludeComma()));
|
7
|
+
*/
|
8
|
+
export const composeToString = (...rules) => (value) => {
|
9
|
+
const validationResult = compose(...rules)(value);
|
10
|
+
if (Array.isArray(validationResult)) {
|
11
|
+
return validationResult[0];
|
12
|
+
}
|
13
|
+
return validationResult;
|
14
|
+
};
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './composeToString';
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './composeToString';
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { Rule } from '../types';
|
2
2
|
/**
|
3
|
-
* @example createRule((max: number, message: string) => value => value > max ? message : undefined)
|
4
3
|
* @description Фабрика по созданию правил валидации.
|
4
|
+
* @example createRule((max: number, message: string) => value => value > max ? message : undefined)
|
5
5
|
*/
|
6
6
|
export declare const createRule: <Args extends unknown[]>(creator: Rule<Args>) => Rule<Args>;
|
@@ -1,5 +1,5 @@
|
|
1
1
|
/**
|
2
|
-
* @example createRule((max: number, message: string) => value => value > max ? message : undefined)
|
3
2
|
* @description Фабрика по созданию правил валидации.
|
3
|
+
* @example createRule((max: number, message: string) => value => value > max ? message : undefined)
|
4
4
|
*/
|
5
|
-
export
|
5
|
+
export const createRule = (creator) => creator;
|
package/esm/index.d.ts
CHANGED
package/esm/index.js
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
export * from './isRequired';
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './isRequired';
|
@@ -0,0 +1,8 @@
|
|
1
|
+
export declare const REQUIRED_RULE_DEFAULT_MESSAGE = "\u041D\u0435 \u0437\u0430\u043F\u043E\u043B\u043D\u0435\u043D\u043E";
|
2
|
+
/**
|
3
|
+
* @description Проверяет присутсвует ли value
|
4
|
+
* @example isRequired();
|
5
|
+
* @example isRequired('ИНН обязателен');
|
6
|
+
* @param {string} [message] - 'Не заполнено'
|
7
|
+
*/
|
8
|
+
export declare const isRequired: import("..").Rule<[message?: string | undefined]>;
|
@@ -0,0 +1,24 @@
|
|
1
|
+
import isEmpty from 'lodash.isempty';
|
2
|
+
import { createRule } from '../createRule';
|
3
|
+
export const REQUIRED_RULE_DEFAULT_MESSAGE = 'Не заполнено';
|
4
|
+
/**
|
5
|
+
* @description Проверяет присутсвует ли value
|
6
|
+
* @example isRequired();
|
7
|
+
* @example isRequired('ИНН обязателен');
|
8
|
+
* @param {string} [message] - 'Не заполнено'
|
9
|
+
*/
|
10
|
+
export const isRequired = createRule((message = REQUIRED_RULE_DEFAULT_MESSAGE) => (value) => {
|
11
|
+
if (typeof value === 'number' || value instanceof Date) {
|
12
|
+
return undefined;
|
13
|
+
}
|
14
|
+
if (typeof value === 'string') {
|
15
|
+
return value.trim() ? undefined : message;
|
16
|
+
}
|
17
|
+
if (typeof value === 'boolean') {
|
18
|
+
return value ? undefined : message;
|
19
|
+
}
|
20
|
+
if (!isEmpty(value)) {
|
21
|
+
return undefined;
|
22
|
+
}
|
23
|
+
return message;
|
24
|
+
});
|
package/esm/types.d.ts
CHANGED
@@ -1 +1,4 @@
|
|
1
|
-
export declare type
|
1
|
+
export declare type RuleError = string | string[];
|
2
|
+
export declare type ValidationResult = RuleError | undefined;
|
3
|
+
export declare type InitializedRule = (value?: unknown) => ValidationResult;
|
4
|
+
export declare type Rule<Args extends unknown[]> = (...args: Args) => InitializedRule;
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './yupAdapter';
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './yupAdapter';
|
@@ -0,0 +1,8 @@
|
|
1
|
+
import { InitializedRule } from '../types';
|
2
|
+
declare type YupValidationResult = boolean | string | string[];
|
3
|
+
/**
|
4
|
+
* @description Адаптирует правило созданное createRule к интерфейсу yup
|
5
|
+
* @example yupAdapter(isMinLength(22))('word')
|
6
|
+
*/
|
7
|
+
export declare const yupAdapter: (rule: InitializedRule) => (value: unknown) => YupValidationResult;
|
8
|
+
export {};
|
@@ -0,0 +1,11 @@
|
|
1
|
+
/**
|
2
|
+
* @description Адаптирует правило созданное createRule к интерфейсу yup
|
3
|
+
* @example yupAdapter(isMinLength(22))('word')
|
4
|
+
*/
|
5
|
+
export const yupAdapter = (rule) => (value) => {
|
6
|
+
const error = rule(value);
|
7
|
+
if (!error) {
|
8
|
+
return false;
|
9
|
+
}
|
10
|
+
return error;
|
11
|
+
};
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
@@ -15,3 +15,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
15
15
|
};
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
17
17
|
__exportStar(require("./createRule"), exports);
|
18
|
+
__exportStar(require("./yupAdapter"), exports);
|
19
|
+
__exportStar(require("./compose"), exports);
|
20
|
+
__exportStar(require("./composeAllSettled"), exports);
|
21
|
+
__exportStar(require("./composeToString"), exports);
|
22
|
+
__exportStar(require("./isRequired"), exports);
|
23
|
+
__exportStar(require("./types"), exports);
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './isRequired';
|
@@ -0,0 +1,17 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
|
+
};
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
+
__exportStar(require("./isRequired"), exports);
|
@@ -0,0 +1,8 @@
|
|
1
|
+
export declare const REQUIRED_RULE_DEFAULT_MESSAGE = "\u041D\u0435 \u0437\u0430\u043F\u043E\u043B\u043D\u0435\u043D\u043E";
|
2
|
+
/**
|
3
|
+
* @description Проверяет присутсвует ли value
|
4
|
+
* @example isRequired();
|
5
|
+
* @example isRequired('ИНН обязателен');
|
6
|
+
* @param {string} [message] - 'Не заполнено'
|
7
|
+
*/
|
8
|
+
export declare const isRequired: import("..").Rule<[message?: string | undefined]>;
|
@@ -0,0 +1,30 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.isRequired = exports.REQUIRED_RULE_DEFAULT_MESSAGE = void 0;
|
7
|
+
const lodash_isempty_1 = __importDefault(require("lodash.isempty"));
|
8
|
+
const createRule_1 = require("../createRule");
|
9
|
+
exports.REQUIRED_RULE_DEFAULT_MESSAGE = 'Не заполнено';
|
10
|
+
/**
|
11
|
+
* @description Проверяет присутсвует ли value
|
12
|
+
* @example isRequired();
|
13
|
+
* @example isRequired('ИНН обязателен');
|
14
|
+
* @param {string} [message] - 'Не заполнено'
|
15
|
+
*/
|
16
|
+
exports.isRequired = (0, createRule_1.createRule)((message = exports.REQUIRED_RULE_DEFAULT_MESSAGE) => (value) => {
|
17
|
+
if (typeof value === 'number' || value instanceof Date) {
|
18
|
+
return undefined;
|
19
|
+
}
|
20
|
+
if (typeof value === 'string') {
|
21
|
+
return value.trim() ? undefined : message;
|
22
|
+
}
|
23
|
+
if (typeof value === 'boolean') {
|
24
|
+
return value ? undefined : message;
|
25
|
+
}
|
26
|
+
if (!(0, lodash_isempty_1.default)(value)) {
|
27
|
+
return undefined;
|
28
|
+
}
|
29
|
+
return message;
|
30
|
+
});
|
package/package.json
CHANGED
@@ -2,10 +2,10 @@
|
|
2
2
|
"name": "@astral/validations",
|
3
3
|
"browser": "./src/index.ts",
|
4
4
|
"main": "./index.js",
|
5
|
-
"version": "0.1.0",
|
6
5
|
"dependencies": {
|
7
|
-
"lodash
|
6
|
+
"lodash.isempty": "^4.4.0"
|
8
7
|
},
|
8
|
+
"version": "1.19.0",
|
9
9
|
"author": "Astral.Soft",
|
10
10
|
"license": "MIT",
|
11
11
|
"repository": {
|
@@ -18,5 +18,15 @@
|
|
18
18
|
"keywords": [],
|
19
19
|
"sideEffects": false,
|
20
20
|
"types": "./esm/index.d.ts",
|
21
|
-
"module": "./esm/index.js"
|
22
|
-
|
21
|
+
"module": "./esm/index.js",
|
22
|
+
"exports": {
|
23
|
+
".": {
|
24
|
+
"import": "./esm/index.js",
|
25
|
+
"require": "./index.js"
|
26
|
+
},
|
27
|
+
"./server": {
|
28
|
+
"import": "./esm/server/index.js",
|
29
|
+
"require": "./server/index.js"
|
30
|
+
}
|
31
|
+
}
|
32
|
+
}
|
package/types.d.ts
CHANGED
@@ -1 +1,4 @@
|
|
1
|
-
export declare type
|
1
|
+
export declare type RuleError = string | string[];
|
2
|
+
export declare type ValidationResult = RuleError | undefined;
|
3
|
+
export declare type InitializedRule = (value?: unknown) => ValidationResult;
|
4
|
+
export declare type Rule<Args extends unknown[]> = (...args: Args) => InitializedRule;
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './yupAdapter';
|
@@ -0,0 +1,17 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
|
+
};
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
+
__exportStar(require("./yupAdapter"), exports);
|
@@ -0,0 +1,8 @@
|
|
1
|
+
import { InitializedRule } from '../types';
|
2
|
+
declare type YupValidationResult = boolean | string | string[];
|
3
|
+
/**
|
4
|
+
* @description Адаптирует правило созданное createRule к интерфейсу yup
|
5
|
+
* @example yupAdapter(isMinLength(22))('word')
|
6
|
+
*/
|
7
|
+
export declare const yupAdapter: (rule: InitializedRule) => (value: unknown) => YupValidationResult;
|
8
|
+
export {};
|
@@ -0,0 +1,15 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.yupAdapter = void 0;
|
4
|
+
/**
|
5
|
+
* @description Адаптирует правило созданное createRule к интерфейсу yup
|
6
|
+
* @example yupAdapter(isMinLength(22))('word')
|
7
|
+
*/
|
8
|
+
const yupAdapter = (rule) => (value) => {
|
9
|
+
const error = rule(value);
|
10
|
+
if (!error) {
|
11
|
+
return false;
|
12
|
+
}
|
13
|
+
return error;
|
14
|
+
};
|
15
|
+
exports.yupAdapter = yupAdapter;
|
@@ -1 +0,0 @@
|
|
1
|
-
export {};
|
@@ -1,9 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
var createRule_1 = require("./createRule");
|
4
|
-
describe('createRule', function () {
|
5
|
-
it('Не модифицирует переданный rule', function () {
|
6
|
-
var rule = (0, createRule_1.createRule)(function () { return function () { return 'Ошибка'; }; });
|
7
|
-
expect(rule()()).toBe('Ошибка');
|
8
|
-
});
|
9
|
-
});
|
@@ -1 +0,0 @@
|
|
1
|
-
export {};
|
package/esm/isString/index.d.ts
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
export * from './isString';
|
package/esm/isString/index.js
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
export * from './isString';
|
@@ -1,6 +0,0 @@
|
|
1
|
-
export declare const STRING_RULE_MESSAGE = "\u0414\u043E\u043B\u0436\u043D\u043E \u0431\u044B\u0442\u044C \u0441\u0442\u0440\u043E\u043A\u043E\u0439";
|
2
|
-
/**
|
3
|
-
* @example isString('Не строка')('string')
|
4
|
-
* @description Правило проверки на строку.
|
5
|
-
*/
|
6
|
-
export declare const isString: import("../types").Rule<[message?: string | undefined]>;
|
package/esm/isString/isString.js
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
import { createRule } from '../createRule';
|
2
|
-
export var STRING_RULE_MESSAGE = 'Должно быть строкой';
|
3
|
-
/**
|
4
|
-
* @example isString('Не строка')('string')
|
5
|
-
* @description Правило проверки на строку.
|
6
|
-
*/
|
7
|
-
export var isString = createRule(function (message) {
|
8
|
-
if (message === void 0) { message = STRING_RULE_MESSAGE; }
|
9
|
-
return function (value) {
|
10
|
-
return typeof value === 'string' ? undefined : message;
|
11
|
-
};
|
12
|
-
});
|
@@ -1 +0,0 @@
|
|
1
|
-
export {};
|
@@ -1,13 +0,0 @@
|
|
1
|
-
import { STRING_RULE_MESSAGE, isString } from './isString';
|
2
|
-
describe('isString', function () {
|
3
|
-
it.each(['string', '', '22', 'null', '[Object object]'])('Valid for value: %s', function (value) {
|
4
|
-
expect(isString()(value)).toBe(undefined);
|
5
|
-
});
|
6
|
-
it.each([0, 22, null, {}, undefined, []])('Invalid for value: %s', function (value) {
|
7
|
-
expect(isString()(value)).toBe(STRING_RULE_MESSAGE);
|
8
|
-
});
|
9
|
-
it('Можно прокинуть кастомный текст ошибки', function () {
|
10
|
-
var errorMessage = 'test';
|
11
|
-
expect(isString(errorMessage)(22)).toBe(errorMessage);
|
12
|
-
});
|
13
|
-
});
|
package/isString/index.d.ts
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
export * from './isString';
|
package/isString/isString.d.ts
DELETED
@@ -1,6 +0,0 @@
|
|
1
|
-
export declare const STRING_RULE_MESSAGE = "\u0414\u043E\u043B\u0436\u043D\u043E \u0431\u044B\u0442\u044C \u0441\u0442\u0440\u043E\u043A\u043E\u0439";
|
2
|
-
/**
|
3
|
-
* @example isString('Не строка')('string')
|
4
|
-
* @description Правило проверки на строку.
|
5
|
-
*/
|
6
|
-
export declare const isString: import("../types").Rule<[message?: string | undefined]>;
|
package/isString/isString.js
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.isString = exports.STRING_RULE_MESSAGE = void 0;
|
4
|
-
var createRule_1 = require("../createRule");
|
5
|
-
exports.STRING_RULE_MESSAGE = 'Должно быть строкой';
|
6
|
-
/**
|
7
|
-
* @example isString('Не строка')('string')
|
8
|
-
* @description Правило проверки на строку.
|
9
|
-
*/
|
10
|
-
exports.isString = (0, createRule_1.createRule)(function (message) {
|
11
|
-
if (message === void 0) { message = exports.STRING_RULE_MESSAGE; }
|
12
|
-
return function (value) {
|
13
|
-
return typeof value === 'string' ? undefined : message;
|
14
|
-
};
|
15
|
-
});
|
@@ -1 +0,0 @@
|
|
1
|
-
export {};
|
@@ -1,15 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
var isString_1 = require("./isString");
|
4
|
-
describe('isString', function () {
|
5
|
-
it.each(['string', '', '22', 'null', '[Object object]'])('Valid for value: %s', function (value) {
|
6
|
-
expect((0, isString_1.isString)()(value)).toBe(undefined);
|
7
|
-
});
|
8
|
-
it.each([0, 22, null, {}, undefined, []])('Invalid for value: %s', function (value) {
|
9
|
-
expect((0, isString_1.isString)()(value)).toBe(isString_1.STRING_RULE_MESSAGE);
|
10
|
-
});
|
11
|
-
it('Можно прокинуть кастомный текст ошибки', function () {
|
12
|
-
var errorMessage = 'test';
|
13
|
-
expect((0, isString_1.isString)(errorMessage)(22)).toBe(errorMessage);
|
14
|
-
});
|
15
|
-
});
|