@astral/validations 3.0.0-beta.3 → 3.0.0-beta.4

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 CHANGED
@@ -86,7 +86,8 @@ import {
86
86
  string,
87
87
  optional,
88
88
  min,
89
- number
89
+ number,
90
+ toPrettyError,
90
91
  } from '@astral/validations';
91
92
 
92
93
  type Permission = {
@@ -125,13 +126,15 @@ validate({
125
126
  },
126
127
  });
127
128
 
128
- // Error in info.permissions.0.description: Обязательно
129
- validate({
130
- name: 'Vasya',
131
- info: {
132
- permissions: [{ id: 1 }],
133
- },
134
- });
129
+ // { info: { permissions: [{ description: 'Обязательно' }] } }
130
+ toPrettyError(
131
+ validate({
132
+ name: 'Vasya',
133
+ info: {
134
+ permissions: [{ id: 1 }],
135
+ },
136
+ })
137
+ );
135
138
  ```
136
139
 
137
140
  Валидация отдельных value
@@ -658,7 +661,8 @@ import {
658
661
  string,
659
662
  optional,
660
663
  min,
661
- number
664
+ number,
665
+ toPrettyError
662
666
  } from '@astral/validations';
663
667
 
664
668
  type User = {
@@ -692,13 +696,15 @@ validate({
692
696
  },
693
697
  });
694
698
 
695
- // Error in info.permissions.0.description: Обязательно
696
- validate({
697
- name: 'Vasya',
698
- info: {
699
- permissions: [{ id: 1 }],
700
- },
701
- });
699
+ // { info: { permissions: [{ description: 'Обязательно' }] } }
700
+ toPrettyError(
701
+ validate({
702
+ name: 'Vasya',
703
+ info: {
704
+ permissions: [{ id: 1 }],
705
+ },
706
+ })
707
+ );
702
708
  ```
703
709
 
704
710
  ### partial
@@ -706,7 +712,7 @@ validate({
706
712
  Позволяет сделать все поля объекта optional.
707
713
 
708
714
  ```ts
709
- import { partial, object, string } from '@astral/validations';
715
+ import { partial, object, string, toPrettyError } from '@astral/validations';
710
716
 
711
717
  type Values = {
712
718
  name: string;
@@ -718,8 +724,10 @@ const validateRequired = object<Values>({
718
724
  surname: string()
719
725
  })
720
726
 
721
- // { message: 'Ошибка в свойстве name: Обязательно' }
722
- validateRequired({});
727
+ // { name: 'Обязательно' }
728
+ toPrettyError(
729
+ validateRequired({})
730
+ );
723
731
 
724
732
  const validatePartial = partial(
725
733
  object<Values>({
@@ -800,7 +808,8 @@ validate({
800
808
  import {
801
809
  array,
802
810
  arrayItem,
803
- min
811
+ min,
812
+ toPrettyError
804
813
  } from '@astral/validations';
805
814
 
806
815
  type User = {
@@ -824,8 +833,10 @@ validate([{ name: 'Vasya' }]);
824
833
  // { message: 'Не меньше: 1' }
825
834
  validate([]);
826
835
 
827
- // { cause: { errorArray: [{ name: { message: 'Не является строкой' } }] } }
828
- validate([{ name: 22 }]);
836
+ // [{ name: 'Не является строкой' }]
837
+ toPrettyError(
838
+ validate([{ name: 22 }])
839
+ );
829
840
  ```
830
841
 
831
842
  ### arrayItem
@@ -833,7 +844,7 @@ validate([{ name: 22 }]);
833
844
  Применяет переданные правила валидации к каждому элементу массива.
834
845
 
835
846
  ```ts
836
- import { array, arrayItem, object, string, optional } from '@astral/validations';
847
+ import { array, arrayItem, object, string, optional, toPrettyError } from '@astral/validations';
837
848
 
838
849
  type User = {
839
850
  name: string;
@@ -853,16 +864,20 @@ const validate = array(
853
864
  validate([{ name: 'Vasya' }]);
854
865
 
855
866
  // { cause: { errorArray: [{ name: { message: 'Не является строкой' } }] } }
856
- validate([{ name: 22 }]);
867
+ toPrettyError(
868
+ validate([{ name: 22 }])
869
+ );
857
870
  ```
858
871
 
859
872
  ```ts
860
- import { array, arrayItem, string, min } from '@astral/validations';
873
+ import { array, arrayItem, string, min, toPrettyError } from '@astral/validations';
861
874
 
862
875
  const validate = array(arrayItem(string(min(3))));
863
876
 
864
- // { cause: { arrayError: [undefined, { message: 'Мин. символов: 3' }] } }
865
- validate(['vasya', 'ma']);
877
+ // [undefined, 'Мин. символов: 3']
878
+ toPrettyError(
879
+ validate(['vasya', 'ma'])
880
+ );
866
881
  ```
867
882
 
868
883
  ---
@@ -920,10 +935,12 @@ const validate = object<Values, Values>({
920
935
  });
921
936
 
922
937
  // undefined
923
- const result1 = validate({ isAgree: false, name: '' });
938
+ validate({ isAgree: false, name: '' });
924
939
 
925
- // Required error для name
926
- const result2 = validate({ isAgree: true, name: '' });
940
+ // { name: 'Обязательно' }
941
+ toPrettyError(
942
+ validate({ isAgree: true, name: '' })
943
+ );
927
944
  ```
928
945
 
929
946
  ---
@@ -959,7 +976,7 @@ validateCustomString(20);
959
976
  ## Базовый пример
960
977
 
961
978
  ```ts
962
- import { string, object } from '@astral/validations';
979
+ import { string, object, toPrettyError } from '@astral/validations';
963
980
 
964
981
  type Values = {
965
982
  name: string;
@@ -980,8 +997,10 @@ const validate = object<Values, Values>({
980
997
  }),
981
998
  });
982
999
 
983
- // { cause: { errorMap: { nickname: { message: 'Символ "_" запрещен', code: 'nickname-symbol' } } } }
984
- validate({ name: 'Vasya', nickname: 'va_sya' });
1000
+ // { nickname: 'Символ "_" запрещен' }
1001
+ toPrettyError(
1002
+ validate({ name: 'Vasya', nickname: 'va_sya' })
1003
+ );
985
1004
  ```
986
1005
 
987
1006
  ## Связанные поля
@@ -989,7 +1008,7 @@ validate({ name: 'Vasya', nickname: 'va_sya' });
989
1008
  В ```ctx.global.values``` находится value, принятое самым верхнеуровневым guard'ом.
990
1009
 
991
1010
  ```ts
992
- import { object, string } from '@astral/validations';
1011
+ import { object, string, toPrettyError } from '@astral/validations';
993
1012
 
994
1013
  type Values = {
995
1014
  password: string;
@@ -1010,8 +1029,10 @@ const validate = object<Values, Values>({
1010
1029
  }),
1011
1030
  });
1012
1031
 
1013
- // Error.message "Пароли не совпадают" для repeatPassword
1014
- validate({ password: 'qywerty123', repeatPassword: 'qywerty1234' });
1032
+ // { repeatPassword: 'Пароли не совпадают' }
1033
+ toPrettyError(
1034
+ validate({ password: 'qywerty123', repeatPassword: 'qywerty1234' })
1035
+ );
1015
1036
  ```
1016
1037
 
1017
1038
  ## Переиспользуемое правило
@@ -1099,10 +1120,12 @@ const validate = object<Values, Values>({
1099
1120
  });
1100
1121
 
1101
1122
  // undefined
1102
- const result1 = validate({ isAgree: false, name: '' });
1123
+ validate({ isAgree: false, name: '' });
1103
1124
 
1104
- // Required error для name
1105
- const result2 = validate({ isAgree: true, name: '' });
1125
+ // { name: 'Обязательно' }
1126
+ toPrettyError(
1127
+ validate({ isAgree: true, name: '' })
1128
+ );
1106
1129
  ```
1107
1130
 
1108
1131
  When для ветки объекта:
@@ -1123,12 +1146,13 @@ const validate = object<Values, Values>({
1123
1146
  }),
1124
1147
  });
1125
1148
 
1126
- // Error.message "Обязательно" для info
1127
- validate({ name: 'Vasya' });
1149
+ // { info: 'Обязательно' }
1150
+ toPrettyError(
1151
+ validate({ name: 'Vasya' })
1152
+ );
1128
1153
 
1129
1154
  // undefined
1130
1155
  validate({ name: 'Kolya' });
1131
-
1132
1156
  ```
1133
1157
 
1134
1158
  ---
package/index.d.ts CHANGED
@@ -25,3 +25,4 @@ export { ogrnUL, OGRN_UL_ERROR_INFO } from './ogrnUL';
25
25
  export { ogrnIP, OGRN_IP_ERROR_INFO } from './ogrnIP';
26
26
  export { any } from './any';
27
27
  export { when } from './when';
28
+ export { toPrettyError } from './toPrettyError';
package/index.js CHANGED
@@ -25,3 +25,4 @@ export { ogrnUL, OGRN_UL_ERROR_INFO } from './ogrnUL';
25
25
  export { ogrnIP, OGRN_IP_ERROR_INFO } from './ogrnIP';
26
26
  export { any } from './any';
27
27
  export { when } from './when';
28
+ export { toPrettyError } from './toPrettyError';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astral/validations",
3
- "version": "3.0.0-beta.3",
3
+ "version": "3.0.0-beta.4",
4
4
  "browser": "./index.js",
5
5
  "main": "./index.js",
6
6
  "dependencies": {
@@ -0,0 +1 @@
1
+ export * from './toPrettyError';
@@ -0,0 +1 @@
1
+ export * from './toPrettyError';
@@ -0,0 +1,39 @@
1
+ import { ValidationResult } from '../core';
2
+ import { PlainValidationResult } from '../toPlainError';
3
+ /**
4
+ * @description Преобразует ошибку валидации в формат для вывода (например, в консоль)
5
+ * @param validationResult
6
+ * @example
7
+ * ```ts
8
+ *
9
+ * type ListItem = { description: string };
10
+ *
11
+ * type Values = {
12
+ * info: { name: string };
13
+ * list: ListItem[];
14
+ * };
15
+ *
16
+ * const validate = object<Values>({
17
+ * info: object<Values['info']>({ name: string() }),
18
+ * list: array(
19
+ * arrayItem(
20
+ * object<ListItem>({
21
+ * description: string(),
22
+ * }),
23
+ * ),
24
+ * ),
25
+ * });
26
+ *
27
+ * const error = validate({
28
+ * info: { name: 22 },
29
+ * list: [{}],
30
+ * });
31
+ *
32
+ * // {
33
+ * // info: { name: 'Не является строкой' },
34
+ * // list: [{ description: 'Обязательно' }],
35
+ * // }
36
+ * toPrettyError(error);
37
+ * ```
38
+ */
39
+ export declare const toPrettyError: (validationResult: ValidationResult) => PlainValidationResult<string>;
@@ -0,0 +1,38 @@
1
+ import { toPlainError } from '../toPlainError';
2
+ /**
3
+ * @description Преобразует ошибку валидации в формат для вывода (например, в консоль)
4
+ * @param validationResult
5
+ * @example
6
+ * ```ts
7
+ *
8
+ * type ListItem = { description: string };
9
+ *
10
+ * type Values = {
11
+ * info: { name: string };
12
+ * list: ListItem[];
13
+ * };
14
+ *
15
+ * const validate = object<Values>({
16
+ * info: object<Values['info']>({ name: string() }),
17
+ * list: array(
18
+ * arrayItem(
19
+ * object<ListItem>({
20
+ * description: string(),
21
+ * }),
22
+ * ),
23
+ * ),
24
+ * });
25
+ *
26
+ * const error = validate({
27
+ * info: { name: 22 },
28
+ * list: [{}],
29
+ * });
30
+ *
31
+ * // {
32
+ * // info: { name: 'Не является строкой' },
33
+ * // list: [{ description: 'Обязательно' }],
34
+ * // }
35
+ * toPrettyError(error);
36
+ * ```
37
+ */
38
+ export const toPrettyError = (validationResult) => toPlainError(validationResult, (err) => err.message);