@naturalcycles/nodejs-lib 13.1.3 → 13.1.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.
@@ -11,7 +11,7 @@ export interface JoiValidationResult<T = any> {
11
11
  *
12
12
  * If `schema` is undefined - returns value as is.
13
13
  */
14
- export declare function validate<T>(value: T, schema?: AnySchema<T>, objectName?: string, options?: ValidationOptions): T;
14
+ export declare function validate<T>(input: any, schema?: AnySchema<T>, objectName?: string, opt?: ValidationOptions): T;
15
15
  /**
16
16
  * Validates with Joi.
17
17
  * Returns JoiValidationResult with converted value and error (if any).
@@ -19,15 +19,15 @@ export declare function validate<T>(value: T, schema?: AnySchema<T>, objectName?
19
19
  *
20
20
  * If `schema` is undefined - returns value as is.
21
21
  */
22
- export declare function getValidationResult<T>(value: T, schema?: AnySchema<T>, objectName?: string, options?: ValidationOptions): JoiValidationResult<T>;
22
+ export declare function getValidationResult<T>(input: any, schema?: AnySchema<T>, objectName?: string, options?: ValidationOptions): JoiValidationResult<T>;
23
23
  /**
24
24
  * Convenience function that returns true if !error.
25
25
  */
26
- export declare function isValid<T>(value: T, schema?: AnySchema<T>): boolean;
27
- export declare function undefinedIfInvalid<T>(value: T, schema?: AnySchema<T>): T | undefined;
26
+ export declare function isValid<T>(input: T, schema?: AnySchema<T>): boolean;
27
+ export declare function undefinedIfInvalid<T>(input: any, schema?: AnySchema<T>): T | undefined;
28
28
  /**
29
- * Will do joi-convertation, regardless of error/validity of value.
29
+ * Will do joi-conversion, regardless of error/validity of value.
30
30
  *
31
31
  * @returns converted value
32
32
  */
33
- export declare function convert<T>(value: T, schema?: AnySchema<T>): T;
33
+ export declare function convert<T>(input: any, schema?: AnySchema<T>): T;
@@ -40,8 +40,8 @@ const defaultOptions = {
40
40
  *
41
41
  * If `schema` is undefined - returns value as is.
42
42
  */
43
- function validate(value, schema, objectName, options = {}) {
44
- const { value: returnValue, error } = getValidationResult(value, schema, objectName, options);
43
+ function validate(input, schema, objectName, opt = {}) {
44
+ const { value: returnValue, error } = getValidationResult(input, schema, objectName, opt);
45
45
  if (error) {
46
46
  throw error;
47
47
  }
@@ -55,18 +55,18 @@ exports.validate = validate;
55
55
  *
56
56
  * If `schema` is undefined - returns value as is.
57
57
  */
58
- function getValidationResult(value, schema, objectName, options = {}) {
58
+ function getValidationResult(input, schema, objectName, options = {}) {
59
59
  if (!schema)
60
- return { value };
61
- const { value: returnValue, error } = schema.validate(value, {
60
+ return { value: input };
61
+ const { value, error } = schema.validate(input, {
62
62
  ...defaultOptions,
63
63
  ...options,
64
64
  });
65
65
  const vr = {
66
- value: returnValue,
66
+ value,
67
67
  };
68
68
  if (error) {
69
- vr.error = createError(value, error, objectName);
69
+ vr.error = createError(input, error, objectName);
70
70
  }
71
71
  return vr;
72
72
  }
@@ -74,35 +74,33 @@ exports.getValidationResult = getValidationResult;
74
74
  /**
75
75
  * Convenience function that returns true if !error.
76
76
  */
77
- function isValid(value, schema) {
77
+ function isValid(input, schema) {
78
78
  if (!schema)
79
- return { value };
80
- const { error } = schema.validate(value, defaultOptions);
79
+ return true;
80
+ const { error } = schema.validate(input, defaultOptions);
81
81
  return !error;
82
82
  }
83
83
  exports.isValid = isValid;
84
- function undefinedIfInvalid(value, schema) {
84
+ function undefinedIfInvalid(input, schema) {
85
85
  if (!schema)
86
- return { value };
87
- const { value: returnValue, error } = schema.validate(value, defaultOptions);
88
- return error ? undefined : returnValue;
86
+ return input;
87
+ const { value, error } = schema.validate(input, defaultOptions);
88
+ return error ? undefined : value;
89
89
  }
90
90
  exports.undefinedIfInvalid = undefinedIfInvalid;
91
91
  /**
92
- * Will do joi-convertation, regardless of error/validity of value.
92
+ * Will do joi-conversion, regardless of error/validity of value.
93
93
  *
94
94
  * @returns converted value
95
95
  */
96
- function convert(value, schema) {
96
+ function convert(input, schema) {
97
97
  if (!schema)
98
- return value;
99
- const { value: returnValue } = schema.validate(value, defaultOptions);
100
- return returnValue;
98
+ return input;
99
+ const { value } = schema.validate(input, defaultOptions);
100
+ return value;
101
101
  }
102
102
  exports.convert = convert;
103
103
  function createError(value, err, objectName) {
104
- if (!err)
105
- return undefined;
106
104
  const tokens = [];
107
105
  const objectId = (0, js_lib_1._isObject)(value) ? value['id'] : undefined;
108
106
  if (objectId || objectName) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
- "version": "13.1.3",
3
+ "version": "13.1.4",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "docs-serve": "vuepress dev docs",
@@ -49,12 +49,12 @@ const defaultOptions: ValidationOptions = {
49
49
  * If `schema` is undefined - returns value as is.
50
50
  */
51
51
  export function validate<T>(
52
- value: T,
52
+ input: any,
53
53
  schema?: AnySchema<T>,
54
54
  objectName?: string,
55
- options: ValidationOptions = {},
55
+ opt: ValidationOptions = {},
56
56
  ): T {
57
- const { value: returnValue, error } = getValidationResult(value, schema, objectName, options)
57
+ const { value: returnValue, error } = getValidationResult(input, schema, objectName, opt)
58
58
 
59
59
  if (error) {
60
60
  throw error
@@ -71,24 +71,24 @@ export function validate<T>(
71
71
  * If `schema` is undefined - returns value as is.
72
72
  */
73
73
  export function getValidationResult<T>(
74
- value: T,
74
+ input: any,
75
75
  schema?: AnySchema<T>,
76
76
  objectName?: string,
77
77
  options: ValidationOptions = {},
78
78
  ): JoiValidationResult<T> {
79
- if (!schema) return { value } as any
79
+ if (!schema) return { value: input }
80
80
 
81
- const { value: returnValue, error } = schema.validate(value, {
81
+ const { value, error } = schema.validate(input, {
82
82
  ...defaultOptions,
83
83
  ...options,
84
84
  })
85
85
 
86
86
  const vr: JoiValidationResult<T> = {
87
- value: returnValue,
87
+ value,
88
88
  }
89
89
 
90
90
  if (error) {
91
- vr.error = createError(value, error, objectName)
91
+ vr.error = createError(input, error, objectName)
92
92
  }
93
93
 
94
94
  return vr
@@ -97,34 +97,33 @@ export function getValidationResult<T>(
97
97
  /**
98
98
  * Convenience function that returns true if !error.
99
99
  */
100
- export function isValid<T>(value: T, schema?: AnySchema<T>): boolean {
101
- if (!schema) return { value } as any
100
+ export function isValid<T>(input: T, schema?: AnySchema<T>): boolean {
101
+ if (!schema) return true
102
102
 
103
- const { error } = schema.validate(value, defaultOptions)
103
+ const { error } = schema.validate(input, defaultOptions)
104
104
  return !error
105
105
  }
106
106
 
107
- export function undefinedIfInvalid<T>(value: T, schema?: AnySchema<T>): T | undefined {
108
- if (!schema) return { value } as any
107
+ export function undefinedIfInvalid<T>(input: any, schema?: AnySchema<T>): T | undefined {
108
+ if (!schema) return input
109
109
 
110
- const { value: returnValue, error } = schema.validate(value, defaultOptions)
110
+ const { value, error } = schema.validate(input, defaultOptions)
111
111
 
112
- return error ? undefined : returnValue
112
+ return error ? undefined : value
113
113
  }
114
114
 
115
115
  /**
116
- * Will do joi-convertation, regardless of error/validity of value.
116
+ * Will do joi-conversion, regardless of error/validity of value.
117
117
  *
118
118
  * @returns converted value
119
119
  */
120
- export function convert<T>(value: T, schema?: AnySchema<T>): T {
121
- if (!schema) return value as any
122
- const { value: returnValue } = schema.validate(value, defaultOptions)
123
- return returnValue
120
+ export function convert<T>(input: any, schema?: AnySchema<T>): T {
121
+ if (!schema) return input
122
+ const { value } = schema.validate(input, defaultOptions)
123
+ return value
124
124
  }
125
125
 
126
126
  function createError(value: any, err: ValidationError, objectName?: string): JoiValidationError {
127
- if (!err) return undefined as any
128
127
  const tokens: string[] = []
129
128
 
130
129
  const objectId = _isObject(value) ? (value['id'] as string) : undefined