@naturalcycles/nodejs-lib 12.101.2 → 12.102.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/dist/index.d.ts CHANGED
@@ -5,7 +5,7 @@ import * as globby from 'globby';
5
5
  import type { GlobbyOptions } from 'globby';
6
6
  import { RequestError, TimeoutError } from 'got';
7
7
  import type { AfterResponseHook, BeforeErrorHook, BeforeRequestHook, Got } from 'got';
8
- import type { AnySchema, ValidationErrorItem } from 'joi';
8
+ import type { AnySchema, ValidationErrorItem, AlternativesSchema, ArraySchema, BinarySchema, BooleanSchema, DateSchema, FunctionSchema, NumberSchema, ObjectSchema, StringSchema } from 'joi';
9
9
  export * from './buffer/buffer.util';
10
10
  export * from './diff/tableDiff';
11
11
  export * from './got/getGot';
@@ -81,5 +81,5 @@ export * from './validation/joi/joi.validation.util';
81
81
  export * from './script';
82
82
  export * from './jwt/jwt.service';
83
83
  export * from './fs/fs.util';
84
- export type { GlobbyOptions, FastGlobOptions, ValidationErrorItem, AnySchema, Got, AfterResponseHook, BeforeErrorHook, BeforeRequestHook, };
84
+ export type { GlobbyOptions, FastGlobOptions, ValidationErrorItem, Got, AfterResponseHook, BeforeErrorHook, BeforeRequestHook, AlternativesSchema, AnySchema, ArraySchema, BinarySchema, BooleanSchema, DateSchema, FunctionSchema, NumberSchema, ObjectSchema, StringSchema, };
85
85
  export { globby, fastGlob, RequestError, TimeoutError, Ajv };
@@ -1,8 +1,8 @@
1
1
  /// <reference types="node" />
2
2
  import { AnyObject, ErrorData, JWTString } from '@naturalcycles/js-lib';
3
+ import { AnySchema } from 'joi';
3
4
  import type { Algorithm, VerifyOptions, JwtHeader, SignOptions } from 'jsonwebtoken';
4
5
  import * as jsonwebtoken from 'jsonwebtoken';
5
- import { AnySchemaTyped } from '../validation/joi/joi.model';
6
6
  export { jsonwebtoken };
7
7
  export type { Algorithm, VerifyOptions, SignOptions, JwtHeader };
8
8
  export interface JWTServiceCfg {
@@ -50,9 +50,9 @@ export interface JWTServiceCfg {
50
50
  export declare class JWTService {
51
51
  cfg: JWTServiceCfg;
52
52
  constructor(cfg: JWTServiceCfg);
53
- sign<T extends AnyObject>(payload: T, schema?: AnySchemaTyped<T>, opt?: SignOptions): JWTString;
54
- verify<T extends AnyObject>(token: JWTString, schema?: AnySchemaTyped<T>, opt?: VerifyOptions, publicKey?: string): T;
55
- decode<T extends AnyObject>(token: JWTString, schema?: AnySchemaTyped<T>): {
53
+ sign<T extends AnyObject>(payload: T, schema?: AnySchema<T>, opt?: SignOptions): JWTString;
54
+ verify<T extends AnyObject>(token: JWTString, schema?: AnySchema<T>, opt?: VerifyOptions, publicKey?: string): T;
55
+ decode<T extends AnyObject>(token: JWTString, schema?: AnySchema<T>): {
56
56
  header: JwtHeader;
57
57
  payload: T;
58
58
  signature: string;
@@ -2,8 +2,8 @@ import * as JoiLib from 'joi';
2
2
  import { ExtendedNumberSchema } from './number.extensions';
3
3
  import { ExtendedStringSchema } from './string.extensions';
4
4
  export interface ExtendedJoi extends JoiLib.Root {
5
- string: () => ExtendedStringSchema;
6
- number: () => ExtendedNumberSchema;
5
+ string: <TSchema = string>() => ExtendedStringSchema<TSchema>;
6
+ number: <TSchema = number>() => ExtendedNumberSchema<TSchema>;
7
7
  }
8
8
  /**
9
9
  * This is the only right place to import Joi from
@@ -1,30 +1,3 @@
1
- /// <reference types="node" />
2
- import { AlternativesSchema, AnySchema, ArraySchema, BinarySchema, BooleanSchema, DateSchema, FunctionSchema, NumberSchema, ObjectSchema, StringSchema } from 'joi';
3
- export type SchemaTyped<IN, OUT = IN> = AnySchemaTyped<IN, OUT> | ArraySchemaTyped<IN> | AlternativesSchemaTyped<IN> | BinarySchemaTyped | BooleanSchemaTyped | DateSchemaTyped<IN> | FunctionSchemaTyped<IN> | NumberSchemaTyped | ObjectSchemaTyped<IN, OUT> | StringSchemaTyped;
4
- /**
5
- * IN - value before validation/conversion
6
- * OUT - value after validation/conversion (can be different due to conversion, stripping, etc)
7
- */
8
- export interface AnySchemaTyped<IN, OUT = IN> extends AnySchema {
9
- }
10
- export interface ArraySchemaTyped<T> extends ArraySchema, AnySchemaTyped<T[]> {
11
- }
12
- export interface AlternativesSchemaTyped<T> extends AlternativesSchema {
13
- }
14
- export interface BinarySchemaTyped extends BinarySchema, AnySchemaTyped<Buffer> {
15
- }
16
- export interface BooleanSchemaTyped extends BooleanSchema, AnySchemaTyped<boolean> {
17
- }
18
- export interface DateSchemaTyped<T> extends DateSchema {
19
- }
20
- export interface FunctionSchemaTyped<T> extends FunctionSchema {
21
- }
22
- export interface NumberSchemaTyped extends NumberSchema, AnySchemaTyped<number> {
23
- }
24
- export interface ObjectSchemaTyped<IN, OUT = IN> extends ObjectSchema<IN>, AnySchemaTyped<IN, OUT> {
25
- }
26
- export interface StringSchemaTyped extends StringSchema, AnySchemaTyped<string> {
27
- }
28
1
  /**
29
2
  * This type is useful to allow "joi schema merging".
30
3
  * Because by default Joi doesn't allow normal merging.
@@ -1,38 +1,40 @@
1
+ /// <reference types="node" />
1
2
  import { SavedDBEntity } from '@naturalcycles/js-lib';
2
- import { AlternativesSchemaTyped, AnySchemaTyped, ArraySchemaTyped, BooleanSchemaTyped, ObjectSchemaTyped, StringSchemaTyped } from './joi.model';
3
- export declare const booleanSchema: BooleanSchemaTyped;
4
- export declare const booleanDefaultToFalseSchema: BooleanSchemaTyped;
5
- export declare const stringSchema: import("./string.extensions").ExtendedStringSchema;
6
- export declare const numberSchema: import("./number.extensions").ExtendedNumberSchema;
7
- export declare const integerSchema: import("./number.extensions").ExtendedNumberSchema;
8
- export declare const percentageSchema: import("./number.extensions").ExtendedNumberSchema;
9
- export declare const dateStringSchema: import("./string.extensions").ExtendedStringSchema;
10
- export declare const binarySchema: import("joi").BinarySchema;
11
- export declare const dateObjectSchema: import("joi").ObjectSchema<any>;
12
- export declare const urlSchema: (scheme?: string | string[]) => StringSchemaTyped;
13
- export declare function arraySchema<T>(items?: AnySchemaTyped<T, T>): ArraySchemaTyped<T>;
14
- export declare function objectSchema<IN, OUT = IN>(schema?: {
15
- [key in keyof Partial<IN>]: AnySchemaTyped<IN[key]>;
16
- }): ObjectSchemaTyped<IN, OUT>;
17
- export declare function oneOfSchema<T = any>(...schemas: AnySchemaTyped<any>[]): AlternativesSchemaTyped<T>;
18
- export declare const anySchema: import("joi").AnySchema;
19
- export declare const anyObjectSchema: import("joi").ObjectSchema<any>;
3
+ import { AlternativesSchema, AnySchema, ArraySchema, ObjectSchema } from 'joi';
4
+ import { ExtendedStringSchema } from './string.extensions';
5
+ export declare const booleanSchema: import("joi").BooleanSchema<boolean>;
6
+ export declare const booleanDefaultToFalseSchema: import("joi").BooleanSchema<boolean>;
7
+ export declare const stringSchema: ExtendedStringSchema<string>;
8
+ export declare const numberSchema: import("./number.extensions").ExtendedNumberSchema<number>;
9
+ export declare const integerSchema: import("./number.extensions").ExtendedNumberSchema<number>;
10
+ export declare const percentageSchema: import("./number.extensions").ExtendedNumberSchema<number>;
11
+ export declare const dateStringSchema: ExtendedStringSchema<string>;
12
+ export declare const binarySchema: import("joi").BinarySchema<Buffer>;
13
+ export declare const dateObjectSchema: ObjectSchema<any>;
14
+ export declare const urlSchema: (scheme?: string | string[]) => ExtendedStringSchema;
15
+ export declare function arraySchema<T>(items?: AnySchema<T>): ArraySchema<T[]>;
16
+ export declare function objectSchema<T>(schema?: {
17
+ [key in keyof Partial<T>]: AnySchema<T[key]>;
18
+ }): ObjectSchema<T>;
19
+ export declare function oneOfSchema<T = any>(...schemas: AnySchema[]): AlternativesSchema<T>;
20
+ export declare const anySchema: AnySchema<any>;
21
+ export declare const anyObjectSchema: ObjectSchema<any>;
20
22
  export declare const BASE62_REGEX: RegExp;
21
23
  export declare const BASE64_REGEX: RegExp;
22
24
  export declare const BASE64URL_REGEX: RegExp;
23
- export declare const base62Schema: import("./string.extensions").ExtendedStringSchema;
24
- export declare const base64Schema: import("./string.extensions").ExtendedStringSchema;
25
- export declare const base64UrlSchema: import("./string.extensions").ExtendedStringSchema;
25
+ export declare const base62Schema: ExtendedStringSchema<string>;
26
+ export declare const base64Schema: ExtendedStringSchema<string>;
27
+ export declare const base64UrlSchema: ExtendedStringSchema<string>;
26
28
  export declare const JWT_REGEX: RegExp;
27
- export declare const jwtSchema: import("./string.extensions").ExtendedStringSchema;
29
+ export declare const jwtSchema: ExtendedStringSchema<string>;
28
30
  /**
29
31
  * [a-zA-Z0-9_]*
30
32
  * 6-64 length
31
33
  */
32
- export declare const idSchema: import("./string.extensions").ExtendedStringSchema;
33
- export declare const idBase62Schema: import("./string.extensions").ExtendedStringSchema;
34
- export declare const idBase64Schema: import("./string.extensions").ExtendedStringSchema;
35
- export declare const idBase64UrlSchema: import("./string.extensions").ExtendedStringSchema;
34
+ export declare const idSchema: ExtendedStringSchema<string>;
35
+ export declare const idBase62Schema: ExtendedStringSchema<string>;
36
+ export declare const idBase64Schema: ExtendedStringSchema<string>;
37
+ export declare const idBase64UrlSchema: ExtendedStringSchema<string>;
36
38
  /**
37
39
  * `_` should NOT be allowed to be able to use slug-ids as part of natural ids with `_` separator.
38
40
  */
@@ -40,35 +42,35 @@ export declare const SLUG_REGEX: RegExp;
40
42
  /**
41
43
  * "Slug" - a valid URL, filename, etc.
42
44
  */
43
- export declare const slugSchema: import("./string.extensions").ExtendedStringSchema;
45
+ export declare const slugSchema: ExtendedStringSchema<string>;
44
46
  /**
45
47
  * Between years 1970 and 2050
46
48
  */
47
- export declare const unixTimestampSchema: import("./number.extensions").ExtendedNumberSchema;
49
+ export declare const unixTimestampSchema: import("./number.extensions").ExtendedNumberSchema<number>;
48
50
  /**
49
51
  * Between years 2000 and 2050
50
52
  */
51
- export declare const unixTimestamp2000Schema: import("./number.extensions").ExtendedNumberSchema;
53
+ export declare const unixTimestamp2000Schema: import("./number.extensions").ExtendedNumberSchema<number>;
52
54
  /**
53
55
  * Between years 1970 and 2050
54
56
  */
55
- export declare const unixTimestampMillisSchema: import("./number.extensions").ExtendedNumberSchema;
57
+ export declare const unixTimestampMillisSchema: import("./number.extensions").ExtendedNumberSchema<number>;
56
58
  /**
57
59
  * Between years 2000 and 2050
58
60
  */
59
- export declare const unixTimestampMillis2000Schema: import("./number.extensions").ExtendedNumberSchema;
60
- export declare const verSchema: import("./number.extensions").ExtendedNumberSchema;
61
+ export declare const unixTimestampMillis2000Schema: import("./number.extensions").ExtendedNumberSchema<number>;
62
+ export declare const verSchema: import("./number.extensions").ExtendedNumberSchema<number>;
61
63
  /**
62
64
  * Be careful, by default emailSchema does TLD validation. To disable it - use `stringSchema.email({tld: false}).lowercase()`
63
65
  */
64
- export declare const emailSchema: import("./string.extensions").ExtendedStringSchema;
66
+ export declare const emailSchema: ExtendedStringSchema<string>;
65
67
  /**
66
68
  * Pattern is simplified for our use, it's not a canonical SemVer.
67
69
  */
68
70
  export declare const SEM_VER_REGEX: RegExp;
69
- export declare const semVerSchema: import("./string.extensions").ExtendedStringSchema;
70
- export declare const userAgentSchema: import("./string.extensions").ExtendedStringSchema;
71
- export declare const utcOffsetSchema: import("./number.extensions").ExtendedNumberSchema;
72
- export declare const ipAddressSchema: import("./string.extensions").ExtendedStringSchema;
73
- export declare const baseDBEntitySchema: ObjectSchemaTyped<Partial<SavedDBEntity<string>>, Partial<SavedDBEntity<string>>>;
74
- export declare const savedDBEntitySchema: ObjectSchemaTyped<SavedDBEntity<string>, SavedDBEntity<string>>;
71
+ export declare const semVerSchema: ExtendedStringSchema<string>;
72
+ export declare const userAgentSchema: ExtendedStringSchema<string>;
73
+ export declare const utcOffsetSchema: import("./number.extensions").ExtendedNumberSchema<number>;
74
+ export declare const ipAddressSchema: ExtendedStringSchema<string>;
75
+ export declare const baseDBEntitySchema: ObjectSchema<Partial<SavedDBEntity<string>>>;
76
+ export declare const savedDBEntitySchema: ObjectSchema<SavedDBEntity<string>>;
@@ -1,5 +1,4 @@
1
- import { ValidationOptions } from 'joi';
2
- import { AnySchemaTyped } from './joi.model';
1
+ import { AnySchema, ValidationOptions } from 'joi';
3
2
  import { JoiValidationError } from './joi.validation.error';
4
3
  export interface JoiValidationResult<T = any> {
5
4
  value: T;
@@ -12,7 +11,7 @@ export interface JoiValidationResult<T = any> {
12
11
  *
13
12
  * If `schema` is undefined - returns value as is.
14
13
  */
15
- export declare function validate<IN, OUT = IN>(value: IN, schema?: AnySchemaTyped<IN, OUT>, objectName?: string, options?: ValidationOptions): OUT;
14
+ export declare function validate<T>(value: T, schema?: AnySchema<T>, objectName?: string, options?: ValidationOptions): T;
16
15
  /**
17
16
  * Validates with Joi.
18
17
  * Returns JoiValidationResult with converted value and error (if any).
@@ -20,15 +19,15 @@ export declare function validate<IN, OUT = IN>(value: IN, schema?: AnySchemaType
20
19
  *
21
20
  * If `schema` is undefined - returns value as is.
22
21
  */
23
- export declare function getValidationResult<IN, OUT = IN>(value: IN, schema?: AnySchemaTyped<IN, OUT>, objectName?: string, options?: ValidationOptions): JoiValidationResult<OUT>;
22
+ export declare function getValidationResult<T>(value: T, schema?: AnySchema<T>, objectName?: string, options?: ValidationOptions): JoiValidationResult<T>;
24
23
  /**
25
24
  * Convenience function that returns true if !error.
26
25
  */
27
- export declare function isValid<IN, OUT = IN>(value: IN, schema?: AnySchemaTyped<IN, OUT>): boolean;
28
- export declare function undefinedIfInvalid<IN, OUT = IN>(value: IN, schema?: AnySchemaTyped<IN, OUT>): OUT | undefined;
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;
29
28
  /**
30
29
  * Will do joi-convertation, regardless of error/validity of value.
31
30
  *
32
31
  * @returns converted value
33
32
  */
34
- export declare function convert<IN, OUT = IN>(value: IN, schema?: AnySchemaTyped<IN, OUT>): OUT;
33
+ export declare function convert<T>(value: T, schema?: AnySchema<T>): T;
@@ -1,7 +1,6 @@
1
1
  import * as Joi from 'joi';
2
2
  import { Extension, NumberSchema } from 'joi';
3
- import { AnySchemaTyped } from './joi.model';
4
- export interface ExtendedNumberSchema extends NumberSchema, AnySchemaTyped<number> {
3
+ export interface ExtendedNumberSchema<TSchema = number> extends NumberSchema<TSchema> {
5
4
  dividable: (q: number) => this;
6
5
  }
7
6
  export declare function numberExtensions(joi: typeof Joi): Extension;
@@ -1,7 +1,6 @@
1
1
  import { Extension, StringSchema } from 'joi';
2
2
  import * as Joi from 'joi';
3
- import { AnySchemaTyped } from './joi.model';
4
- export interface ExtendedStringSchema extends StringSchema, AnySchemaTyped<string> {
3
+ export interface ExtendedStringSchema<TSchema = string> extends StringSchema<TSchema> {
5
4
  dateString: (min?: string, max?: string) => this;
6
5
  }
7
6
  export interface JoiDateStringOptions {
@@ -24,14 +24,14 @@ function stringExtensions(joi) {
24
24
  args: [
25
25
  {
26
26
  name: 'min',
27
- ref: true,
28
- assert: v => typeof v === 'string',
27
+ // ref: true, // check false
28
+ assert: v => v === undefined || typeof v === 'string',
29
29
  message: 'must be a string',
30
30
  },
31
31
  {
32
32
  name: 'max',
33
- ref: true,
34
- assert: v => typeof v === 'string',
33
+ // ref: true,
34
+ assert: v => v === undefined || typeof v === 'string',
35
35
  message: 'must be a string',
36
36
  },
37
37
  ],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
- "version": "12.101.2",
3
+ "version": "12.102.0",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "docs-serve": "vuepress dev docs",
@@ -28,7 +28,7 @@
28
28
  "fast-glob": "^3.2.11",
29
29
  "globby": "^11.0.0",
30
30
  "got": "^11.0.1",
31
- "joi": "17.4.2",
31
+ "joi": "^17.9.2",
32
32
  "jsonwebtoken": "^9.0.0",
33
33
  "lru-cache": "^10.0.0",
34
34
  "through2-concurrent": "^2.0.0",
package/src/index.ts CHANGED
@@ -5,7 +5,19 @@ import * as globby from 'globby'
5
5
  import type { GlobbyOptions } from 'globby'
6
6
  import { RequestError, TimeoutError } from 'got'
7
7
  import type { AfterResponseHook, BeforeErrorHook, BeforeRequestHook, Got } from 'got'
8
- import type { AnySchema, ValidationErrorItem } from 'joi'
8
+ import type {
9
+ AnySchema,
10
+ ValidationErrorItem,
11
+ AlternativesSchema,
12
+ ArraySchema,
13
+ BinarySchema,
14
+ BooleanSchema,
15
+ DateSchema,
16
+ FunctionSchema,
17
+ NumberSchema,
18
+ ObjectSchema,
19
+ StringSchema,
20
+ } from 'joi'
9
21
  export * from './buffer/buffer.util'
10
22
  export * from './diff/tableDiff'
11
23
  export * from './got/getGot'
@@ -86,11 +98,20 @@ export type {
86
98
  GlobbyOptions,
87
99
  FastGlobOptions,
88
100
  ValidationErrorItem,
89
- AnySchema,
90
101
  Got,
91
102
  AfterResponseHook,
92
103
  BeforeErrorHook,
93
104
  BeforeRequestHook,
105
+ AlternativesSchema,
106
+ AnySchema,
107
+ ArraySchema,
108
+ BinarySchema,
109
+ BooleanSchema,
110
+ DateSchema,
111
+ FunctionSchema,
112
+ NumberSchema,
113
+ ObjectSchema,
114
+ StringSchema,
94
115
  }
95
116
 
96
117
  export { globby, fastGlob, RequestError, TimeoutError, Ajv }
@@ -1,7 +1,7 @@
1
1
  import { _assert, _errorDataAppend, AnyObject, ErrorData, JWTString } from '@naturalcycles/js-lib'
2
+ import { AnySchema } from 'joi'
2
3
  import type { Algorithm, VerifyOptions, JwtHeader, SignOptions } from 'jsonwebtoken'
3
4
  import * as jsonwebtoken from 'jsonwebtoken'
4
- import { AnySchemaTyped } from '../validation/joi/joi.model'
5
5
  import { anyObjectSchema } from '../validation/joi/joi.shared.schemas'
6
6
  import { validate } from '../validation/joi/joi.validation.util'
7
7
  export { jsonwebtoken }
@@ -62,11 +62,7 @@ export interface JWTServiceCfg {
62
62
  export class JWTService {
63
63
  constructor(public cfg: JWTServiceCfg) {}
64
64
 
65
- sign<T extends AnyObject>(
66
- payload: T,
67
- schema?: AnySchemaTyped<T>,
68
- opt: SignOptions = {},
69
- ): JWTString {
65
+ sign<T extends AnyObject>(payload: T, schema?: AnySchema<T>, opt: SignOptions = {}): JWTString {
70
66
  _assert(
71
67
  this.cfg.privateKey,
72
68
  'JWTService: privateKey is required to be able to verify, but not provided',
@@ -86,7 +82,7 @@ export class JWTService {
86
82
 
87
83
  verify<T extends AnyObject>(
88
84
  token: JWTString,
89
- schema?: AnySchemaTyped<T>,
85
+ schema?: AnySchema<T>,
90
86
  opt: VerifyOptions = {},
91
87
  publicKey?: string, // allows to override public key
92
88
  ): T {
@@ -119,7 +115,7 @@ export class JWTService {
119
115
 
120
116
  decode<T extends AnyObject>(
121
117
  token: JWTString,
122
- schema?: AnySchemaTyped<T>,
118
+ schema?: AnySchema<T>,
123
119
  ): {
124
120
  header: JwtHeader
125
121
  payload: T
@@ -5,9 +5,9 @@ import { ExtendedStringSchema, stringExtensions } from './string.extensions'
5
5
 
6
6
  export interface ExtendedJoi extends JoiLib.Root {
7
7
  // eslint-disable-next-line id-blacklist
8
- string: () => ExtendedStringSchema
8
+ string: <TSchema = string>() => ExtendedStringSchema<TSchema>
9
9
  // eslint-disable-next-line id-blacklist
10
- number: () => ExtendedNumberSchema
10
+ number: <TSchema = number>() => ExtendedNumberSchema<TSchema>
11
11
  }
12
12
 
13
13
  /**
@@ -1,48 +1,3 @@
1
- import {
2
- AlternativesSchema,
3
- AnySchema,
4
- ArraySchema,
5
- BinarySchema,
6
- BooleanSchema,
7
- DateSchema,
8
- FunctionSchema,
9
- NumberSchema,
10
- ObjectSchema,
11
- StringSchema,
12
- } from 'joi'
13
-
14
- /* eslint-disable unused-imports/no-unused-vars */
15
-
16
- export type SchemaTyped<IN, OUT = IN> =
17
- | AnySchemaTyped<IN, OUT>
18
- | ArraySchemaTyped<IN>
19
- | AlternativesSchemaTyped<IN>
20
- | BinarySchemaTyped
21
- | BooleanSchemaTyped
22
- | DateSchemaTyped<IN>
23
- | FunctionSchemaTyped<IN>
24
- | NumberSchemaTyped
25
- | ObjectSchemaTyped<IN, OUT>
26
- | StringSchemaTyped
27
-
28
- /**
29
- * IN - value before validation/conversion
30
- * OUT - value after validation/conversion (can be different due to conversion, stripping, etc)
31
- */
32
- export interface AnySchemaTyped<IN, OUT = IN> extends AnySchema {}
33
-
34
- export interface ArraySchemaTyped<T> extends ArraySchema, AnySchemaTyped<T[]> {}
35
- export interface AlternativesSchemaTyped<T> extends AlternativesSchema {}
36
- export interface BinarySchemaTyped extends BinarySchema, AnySchemaTyped<Buffer> {}
37
- export interface BooleanSchemaTyped extends BooleanSchema, AnySchemaTyped<boolean> {}
38
- export interface DateSchemaTyped<T> extends DateSchema {}
39
- export interface FunctionSchemaTyped<T> extends FunctionSchema {}
40
- export interface NumberSchemaTyped extends NumberSchema, AnySchemaTyped<number> {}
41
- export interface ObjectSchemaTyped<IN, OUT = IN>
42
- extends ObjectSchema<IN>,
43
- AnySchemaTyped<IN, OUT> {}
44
- export interface StringSchemaTyped extends StringSchema, AnySchemaTyped<string> {}
45
-
46
1
  /**
47
2
  * This type is useful to allow "joi schema merging".
48
3
  * Because by default Joi doesn't allow normal merging.
@@ -1,16 +1,10 @@
1
1
  import { BaseDBEntity, SavedDBEntity } from '@naturalcycles/js-lib'
2
+ import { AlternativesSchema, AnySchema, ArraySchema, ObjectSchema } from 'joi'
2
3
  import { Joi } from './joi.extensions'
3
- import {
4
- AlternativesSchemaTyped,
5
- AnySchemaTyped,
6
- ArraySchemaTyped,
7
- BooleanSchemaTyped,
8
- ObjectSchemaTyped,
9
- StringSchemaTyped,
10
- } from './joi.model'
11
-
12
- export const booleanSchema = Joi.boolean() as BooleanSchemaTyped
13
- export const booleanDefaultToFalseSchema = Joi.boolean().default(false) as BooleanSchemaTyped
4
+ import { ExtendedStringSchema } from './string.extensions'
5
+
6
+ export const booleanSchema = Joi.boolean()
7
+ export const booleanDefaultToFalseSchema = Joi.boolean().default(false)
14
8
  export const stringSchema = Joi.string()
15
9
  export const numberSchema = Joi.number()
16
10
  export const integerSchema = Joi.number().integer()
@@ -19,22 +13,20 @@ export const dateStringSchema = stringSchema.dateString()
19
13
  export const binarySchema = Joi.binary()
20
14
  export const dateObjectSchema = Joi.object().instance(Date)
21
15
 
22
- export const urlSchema = (scheme: string | string[] = 'https'): StringSchemaTyped =>
16
+ export const urlSchema = (scheme: string | string[] = 'https'): ExtendedStringSchema =>
23
17
  Joi.string().uri({ scheme })
24
18
 
25
- export function arraySchema<T>(items?: AnySchemaTyped<T, T>): ArraySchemaTyped<T> {
19
+ export function arraySchema<T>(items?: AnySchema<T>): ArraySchema<T[]> {
26
20
  return items ? Joi.array().items(items) : Joi.array()
27
21
  }
28
22
 
29
- export function objectSchema<IN, OUT = IN>(schema?: {
30
- [key in keyof Partial<IN>]: AnySchemaTyped<IN[key]>
31
- }): ObjectSchemaTyped<IN, OUT> {
23
+ export function objectSchema<T>(schema?: {
24
+ [key in keyof Partial<T>]: AnySchema<T[key]>
25
+ }): ObjectSchema<T> {
32
26
  return Joi.object(schema)
33
27
  }
34
28
 
35
- export function oneOfSchema<T = any>(
36
- ...schemas: AnySchemaTyped<any>[]
37
- ): AlternativesSchemaTyped<T> {
29
+ export function oneOfSchema<T = any>(...schemas: AnySchema[]): AlternativesSchema<T> {
38
30
  return Joi.alternatives(schemas)
39
31
  }
40
32
 
@@ -7,8 +7,7 @@
7
7
  */
8
8
 
9
9
  import { _hb, _isObject, _truncateMiddle } from '@naturalcycles/js-lib'
10
- import { ValidationError, ValidationOptions } from 'joi'
11
- import { AnySchemaTyped } from './joi.model'
10
+ import { AnySchema, ValidationError, ValidationOptions } from 'joi'
12
11
  import { JoiValidationError, JoiValidationErrorData } from './joi.validation.error'
13
12
 
14
13
  // todo: consider replacing with Tuple of [error, value]
@@ -49,18 +48,13 @@ const defaultOptions: ValidationOptions = {
49
48
  *
50
49
  * If `schema` is undefined - returns value as is.
51
50
  */
52
- export function validate<IN, OUT = IN>(
53
- value: IN,
54
- schema?: AnySchemaTyped<IN, OUT>,
51
+ export function validate<T>(
52
+ value: T,
53
+ schema?: AnySchema<T>,
55
54
  objectName?: string,
56
55
  options: ValidationOptions = {},
57
- ): OUT {
58
- const { value: returnValue, error } = getValidationResult<IN, OUT>(
59
- value,
60
- schema,
61
- objectName,
62
- options,
63
- )
56
+ ): T {
57
+ const { value: returnValue, error } = getValidationResult(value, schema, objectName, options)
64
58
 
65
59
  if (error) {
66
60
  throw error
@@ -76,12 +70,12 @@ export function validate<IN, OUT = IN>(
76
70
  *
77
71
  * If `schema` is undefined - returns value as is.
78
72
  */
79
- export function getValidationResult<IN, OUT = IN>(
80
- value: IN,
81
- schema?: AnySchemaTyped<IN, OUT>,
73
+ export function getValidationResult<T>(
74
+ value: T,
75
+ schema?: AnySchema<T>,
82
76
  objectName?: string,
83
77
  options: ValidationOptions = {},
84
- ): JoiValidationResult<OUT> {
78
+ ): JoiValidationResult<T> {
85
79
  if (!schema) return { value } as any
86
80
 
87
81
  const { value: returnValue, error } = schema.validate(value, {
@@ -89,8 +83,8 @@ export function getValidationResult<IN, OUT = IN>(
89
83
  ...options,
90
84
  })
91
85
 
92
- const vr: JoiValidationResult<OUT> = {
93
- value: returnValue as OUT,
86
+ const vr: JoiValidationResult<T> = {
87
+ value: returnValue!,
94
88
  }
95
89
 
96
90
  if (error) {
@@ -103,17 +97,14 @@ export function getValidationResult<IN, OUT = IN>(
103
97
  /**
104
98
  * Convenience function that returns true if !error.
105
99
  */
106
- export function isValid<IN, OUT = IN>(value: IN, schema?: AnySchemaTyped<IN, OUT>): boolean {
100
+ export function isValid<T>(value: T, schema?: AnySchema<T>): boolean {
107
101
  if (!schema) return { value } as any
108
102
 
109
103
  const { error } = schema.validate(value, defaultOptions)
110
104
  return !error
111
105
  }
112
106
 
113
- export function undefinedIfInvalid<IN, OUT = IN>(
114
- value: IN,
115
- schema?: AnySchemaTyped<IN, OUT>,
116
- ): OUT | undefined {
107
+ export function undefinedIfInvalid<T>(value: T, schema?: AnySchema<T>): T | undefined {
117
108
  if (!schema) return { value } as any
118
109
 
119
110
  const { value: returnValue, error } = schema.validate(value, defaultOptions)
@@ -126,10 +117,10 @@ export function undefinedIfInvalid<IN, OUT = IN>(
126
117
  *
127
118
  * @returns converted value
128
119
  */
129
- export function convert<IN, OUT = IN>(value: IN, schema?: AnySchemaTyped<IN, OUT>): OUT {
120
+ export function convert<T>(value: T, schema?: AnySchema<T>): T {
130
121
  if (!schema) return value as any
131
122
  const { value: returnValue } = schema.validate(value, defaultOptions)
132
- return returnValue
123
+ return returnValue!
133
124
  }
134
125
 
135
126
  function createError(value: any, err: ValidationError, objectName?: string): JoiValidationError {
@@ -1,8 +1,7 @@
1
1
  import * as Joi from 'joi'
2
2
  import { Extension, NumberSchema } from 'joi'
3
- import { AnySchemaTyped } from './joi.model'
4
3
 
5
- export interface ExtendedNumberSchema extends NumberSchema, AnySchemaTyped<number> {
4
+ export interface ExtendedNumberSchema<TSchema = number> extends NumberSchema<TSchema> {
6
5
  dividable: (q: number) => this
7
6
  }
8
7
 
@@ -1,9 +1,8 @@
1
1
  import { LocalDate, localTime } from '@naturalcycles/js-lib'
2
2
  import { Extension, StringSchema } from 'joi'
3
3
  import * as Joi from 'joi'
4
- import { AnySchemaTyped } from './joi.model'
5
4
 
6
- export interface ExtendedStringSchema extends StringSchema, AnySchemaTyped<string> {
5
+ export interface ExtendedStringSchema<TSchema = string> extends StringSchema<TSchema> {
7
6
  dateString: (min?: string, max?: string) => this
8
7
  }
9
8
 
@@ -28,20 +27,20 @@ export function stringExtensions(joi: typeof Joi): Extension {
28
27
  method(min?: string, max?: string) {
29
28
  return this.$_addRule({
30
29
  name: 'dateString',
31
- args: { min, max } as JoiDateStringOptions,
30
+ args: { min, max } satisfies JoiDateStringOptions,
32
31
  })
33
32
  },
34
33
  args: [
35
34
  {
36
35
  name: 'min',
37
- ref: true,
38
- assert: v => typeof v === 'string',
36
+ // ref: true, // check false
37
+ assert: v => v === undefined || typeof v === 'string',
39
38
  message: 'must be a string',
40
39
  },
41
40
  {
42
41
  name: 'max',
43
- ref: true,
44
- assert: v => typeof v === 'string',
42
+ // ref: true,
43
+ assert: v => v === undefined || typeof v === 'string',
45
44
  message: 'must be a string',
46
45
  },
47
46
  ],