@naturalcycles/nodejs-lib 15.10.1 → 15.12.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.
@@ -1,8 +1,8 @@
1
1
  import type { ErrorData } from '@naturalcycles/js-lib/error';
2
2
  import type { AnyObject, JWTString } from '@naturalcycles/js-lib/types';
3
- import type { AnySchema } from 'joi';
4
3
  import type { Algorithm, JwtHeader, SignOptions, VerifyOptions } from 'jsonwebtoken';
5
4
  import jsonwebtoken from 'jsonwebtoken';
5
+ import type { AjvSchema } from '../validation/ajv/ajvSchema.js';
6
6
  export { jsonwebtoken };
7
7
  export type { Algorithm, JwtHeader, SignOptions, VerifyOptions };
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?: 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>): {
53
+ sign<T extends AnyObject>(payload: T, schema?: AjvSchema<T>, opt?: SignOptions): JWTString;
54
+ verify<T extends AnyObject>(token: JWTString, schema?: AjvSchema<T>, opt?: VerifyOptions, publicKey?: string): T;
55
+ decode<T extends AnyObject>(token: JWTString, schema?: AjvSchema<T>): {
56
56
  header: JwtHeader;
57
57
  payload: T;
58
58
  signature: string;
@@ -1,8 +1,6 @@
1
1
  import { _assert } from '@naturalcycles/js-lib/error/assert.js';
2
2
  import { _errorDataAppend } from '@naturalcycles/js-lib/error/error.util.js';
3
3
  import jsonwebtoken from 'jsonwebtoken';
4
- import { anyObjectSchema } from '../validation/joi/joi.shared.schemas.js';
5
- import { validate } from '../validation/joi/joi.validation.util.js';
6
4
  export { jsonwebtoken };
7
5
  // todo: define JWTError and list possible options
8
6
  // jwt expired (TokenExpiredError)
@@ -26,9 +24,9 @@ export class JWTService {
26
24
  }
27
25
  sign(payload, schema, opt = {}) {
28
26
  _assert(this.cfg.privateKey, 'JWTService: privateKey is required to be able to verify, but not provided');
29
- if (schema) {
30
- validate(payload, schema);
31
- }
27
+ schema?.validate(payload, {
28
+ mutateInput: true,
29
+ });
32
30
  return jsonwebtoken.sign(payload, this.cfg.privateKey, {
33
31
  algorithm: this.cfg.algorithm,
34
32
  noTimestamp: true,
@@ -44,9 +42,9 @@ export class JWTService {
44
42
  ...this.cfg.verifyOptions,
45
43
  ...opt,
46
44
  });
47
- if (schema) {
48
- validate(data, schema);
49
- }
45
+ schema?.validate(data, {
46
+ mutateInput: true,
47
+ });
50
48
  return data;
51
49
  }
52
50
  catch (err) {
@@ -65,7 +63,9 @@ export class JWTService {
65
63
  _assert(data?.payload, 'invalid token, decoded value is empty', {
66
64
  ...this.cfg.errorData,
67
65
  });
68
- validate(data.payload, schema || anyObjectSchema);
66
+ schema?.validate(data.payload, {
67
+ mutateInput: true,
68
+ });
69
69
  return data;
70
70
  }
71
71
  }
@@ -1,5 +1,6 @@
1
1
  import { type ValidationFunction, type ValidationFunctionResult } from '@naturalcycles/js-lib';
2
2
  import type { JsonSchema, JsonSchemaBuilder } from '@naturalcycles/js-lib/json-schema';
3
+ import { type ZodType } from '@naturalcycles/js-lib/zod';
3
4
  import type { Ajv } from 'ajv';
4
5
  import { AjvValidationError } from './ajvValidationError.js';
5
6
  export interface AjvValidationOptions {
@@ -38,8 +39,6 @@ export interface AjvSchemaCfg {
38
39
  /**
39
40
  * On creation - compiles ajv validation function.
40
41
  * Provides convenient methods, error reporting, etc.
41
- *
42
- * @experimental
43
42
  */
44
43
  export declare class AjvSchema<T = unknown> {
45
44
  schema: JsonSchema<T>;
@@ -58,6 +57,7 @@ export declare class AjvSchema<T = unknown> {
58
57
  * correctly for some reason.
59
58
  */
60
59
  static create<T>(schema: JsonSchemaBuilder<T> | JsonSchema<T> | AjvSchema<T>, cfg?: Partial<AjvSchemaCfg>): AjvSchema<T>;
60
+ static createFromZod<T>(zodSchema: ZodType<T>, cfg?: Partial<AjvSchemaCfg>): AjvSchema<T>;
61
61
  readonly cfg: AjvSchemaCfg;
62
62
  /**
63
63
  * It returns the original object just for convenience.
@@ -2,14 +2,13 @@ import { _isObject, _lazyValue, } from '@naturalcycles/js-lib';
2
2
  import { JsonSchemaAnyBuilder } from '@naturalcycles/js-lib/json-schema';
3
3
  import { _filterNullishValues } from '@naturalcycles/js-lib/object';
4
4
  import { _substringBefore } from '@naturalcycles/js-lib/string';
5
+ import { z } from '@naturalcycles/js-lib/zod';
5
6
  import { _inspect } from '../../string/inspect.js';
6
7
  import { AjvValidationError } from './ajvValidationError.js';
7
8
  import { getAjv } from './getAjv.js';
8
9
  /**
9
10
  * On creation - compiles ajv validation function.
10
11
  * Provides convenient methods, error reporting, etc.
11
- *
12
- * @experimental
13
12
  */
14
13
  export class AjvSchema {
15
14
  schema;
@@ -29,7 +28,7 @@ export class AjvSchema {
29
28
  /**
30
29
  * Shortcut for AjvSchema.create(schema, { lazy: true })
31
30
  */
32
- static createLazy(schema, cfg = {}) {
31
+ static createLazy(schema, cfg) {
33
32
  return AjvSchema.create(schema, {
34
33
  lazy: true,
35
34
  ...cfg,
@@ -44,7 +43,7 @@ export class AjvSchema {
44
43
  * Implementation note: JsonSchemaBuilder goes first in the union type, otherwise TypeScript fails to infer <T> type
45
44
  * correctly for some reason.
46
45
  */
47
- static create(schema, cfg = {}) {
46
+ static create(schema, cfg) {
48
47
  if (schema instanceof AjvSchema)
49
48
  return schema;
50
49
  if (schema instanceof JsonSchemaAnyBuilder) {
@@ -52,6 +51,10 @@ export class AjvSchema {
52
51
  }
53
52
  return new AjvSchema(schema, cfg);
54
53
  }
54
+ static createFromZod(zodSchema, cfg) {
55
+ const jsonSchema = z.toJSONSchema(zodSchema, { target: 'draft-7' });
56
+ return new AjvSchema(jsonSchema, cfg);
57
+ }
55
58
  cfg;
56
59
  /**
57
60
  * It returns the original object just for convenience.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
3
  "type": "module",
4
- "version": "15.10.1",
4
+ "version": "15.12.0",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@types/js-yaml": "^4",
@@ -80,6 +80,7 @@
80
80
  "license": "MIT",
81
81
  "envByBranch": {
82
82
  "master": "master",
83
+ "main": "master",
83
84
  "*": "branch"
84
85
  },
85
86
  "scripts": {
@@ -2,11 +2,9 @@ import type { ErrorData } from '@naturalcycles/js-lib/error'
2
2
  import { _assert } from '@naturalcycles/js-lib/error/assert.js'
3
3
  import { _errorDataAppend } from '@naturalcycles/js-lib/error/error.util.js'
4
4
  import type { AnyObject, JWTString } from '@naturalcycles/js-lib/types'
5
- import type { AnySchema } from 'joi'
6
5
  import type { Algorithm, JwtHeader, SignOptions, VerifyOptions } from 'jsonwebtoken'
7
6
  import jsonwebtoken from 'jsonwebtoken'
8
- import { anyObjectSchema } from '../validation/joi/joi.shared.schemas.js'
9
- import { validate } from '../validation/joi/joi.validation.util.js'
7
+ import type { AjvSchema } from '../validation/ajv/ajvSchema.js'
10
8
  export { jsonwebtoken }
11
9
  export type { Algorithm, JwtHeader, SignOptions, VerifyOptions }
12
10
 
@@ -65,15 +63,15 @@ export interface JWTServiceCfg {
65
63
  export class JWTService {
66
64
  constructor(public cfg: JWTServiceCfg) {}
67
65
 
68
- sign<T extends AnyObject>(payload: T, schema?: AnySchema<T>, opt: SignOptions = {}): JWTString {
66
+ sign<T extends AnyObject>(payload: T, schema?: AjvSchema<T>, opt: SignOptions = {}): JWTString {
69
67
  _assert(
70
68
  this.cfg.privateKey,
71
69
  'JWTService: privateKey is required to be able to verify, but not provided',
72
70
  )
73
71
 
74
- if (schema) {
75
- validate(payload, schema)
76
- }
72
+ schema?.validate(payload, {
73
+ mutateInput: true,
74
+ })
77
75
 
78
76
  return jsonwebtoken.sign(payload, this.cfg.privateKey, {
79
77
  algorithm: this.cfg.algorithm,
@@ -85,7 +83,7 @@ export class JWTService {
85
83
 
86
84
  verify<T extends AnyObject>(
87
85
  token: JWTString,
88
- schema?: AnySchema<T>,
86
+ schema?: AjvSchema<T>,
89
87
  opt: VerifyOptions = {},
90
88
  publicKey?: string, // allows to override public key
91
89
  ): T {
@@ -101,9 +99,9 @@ export class JWTService {
101
99
  ...opt,
102
100
  }) as T
103
101
 
104
- if (schema) {
105
- validate(data, schema)
106
- }
102
+ schema?.validate(data, {
103
+ mutateInput: true,
104
+ })
107
105
 
108
106
  return data
109
107
  } catch (err) {
@@ -118,7 +116,7 @@ export class JWTService {
118
116
 
119
117
  decode<T extends AnyObject>(
120
118
  token: JWTString,
121
- schema?: AnySchema<T>,
119
+ schema?: AjvSchema<T>,
122
120
  ): {
123
121
  header: JwtHeader
124
122
  payload: T
@@ -136,7 +134,9 @@ export class JWTService {
136
134
  ...this.cfg.errorData,
137
135
  })
138
136
 
139
- validate(data.payload, schema || anyObjectSchema)
137
+ schema?.validate(data.payload, {
138
+ mutateInput: true,
139
+ })
140
140
 
141
141
  return data
142
142
  }
@@ -8,6 +8,7 @@ import type { JsonSchema, JsonSchemaBuilder } from '@naturalcycles/js-lib/json-s
8
8
  import { JsonSchemaAnyBuilder } from '@naturalcycles/js-lib/json-schema'
9
9
  import { _filterNullishValues } from '@naturalcycles/js-lib/object'
10
10
  import { _substringBefore } from '@naturalcycles/js-lib/string'
11
+ import { z, type ZodType } from '@naturalcycles/js-lib/zod'
11
12
  import type { Ajv } from 'ajv'
12
13
  import { _inspect } from '../../string/inspect.js'
13
14
  import { AjvValidationError } from './ajvValidationError.js'
@@ -55,8 +56,6 @@ export interface AjvSchemaCfg {
55
56
  /**
56
57
  * On creation - compiles ajv validation function.
57
58
  * Provides convenient methods, error reporting, etc.
58
- *
59
- * @experimental
60
59
  */
61
60
  export class AjvSchema<T = unknown> {
62
61
  private constructor(
@@ -81,7 +80,7 @@ export class AjvSchema<T = unknown> {
81
80
  */
82
81
  static createLazy<T>(
83
82
  schema: JsonSchemaBuilder<T> | JsonSchema<T> | AjvSchema<T>,
84
- cfg: Partial<AjvSchemaCfg> = {},
83
+ cfg?: Partial<AjvSchemaCfg>,
85
84
  ): AjvSchema<T> {
86
85
  return AjvSchema.create(schema, {
87
86
  lazy: true,
@@ -100,7 +99,7 @@ export class AjvSchema<T = unknown> {
100
99
  */
101
100
  static create<T>(
102
101
  schema: JsonSchemaBuilder<T> | JsonSchema<T> | AjvSchema<T>,
103
- cfg: Partial<AjvSchemaCfg> = {},
102
+ cfg?: Partial<AjvSchemaCfg>,
104
103
  ): AjvSchema<T> {
105
104
  if (schema instanceof AjvSchema) return schema
106
105
  if (schema instanceof JsonSchemaAnyBuilder) {
@@ -109,6 +108,11 @@ export class AjvSchema<T = unknown> {
109
108
  return new AjvSchema<T>(schema as JsonSchema<T>, cfg)
110
109
  }
111
110
 
111
+ static createFromZod<T>(zodSchema: ZodType<T>, cfg?: Partial<AjvSchemaCfg>): AjvSchema<T> {
112
+ const jsonSchema = z.toJSONSchema(zodSchema, { target: 'draft-7' })
113
+ return new AjvSchema<T>(jsonSchema as JsonSchema<T>, cfg)
114
+ }
115
+
112
116
  readonly cfg: AjvSchemaCfg
113
117
 
114
118
  /**