@naturalcycles/backend-lib 9.53.0 → 9.55.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,11 +1,11 @@
1
1
  import { _lazyValue } from '@naturalcycles/js-lib';
2
- import { AjvSchema } from '@naturalcycles/nodejs-lib/ajv';
2
+ import { JSchema } from '@naturalcycles/nodejs-lib/ajv';
3
3
  import { fs2 } from '@naturalcycles/nodejs-lib/fs2';
4
4
  import { yaml2 } from '@naturalcycles/nodejs-lib/yaml2';
5
5
  import { resourcesDir } from '../paths.cnst.js';
6
6
  const getBackendCfgSchema = _lazyValue(() => {
7
7
  const schemaJson = fs2.readJson(`${resourcesDir}/backendCfg.schema.json`);
8
- return AjvSchema.create(schemaJson, { inputName: 'backend.cfg.yaml' });
8
+ return new JSchema(schemaJson, { inputName: 'backend.cfg.yaml' });
9
9
  });
10
10
  export function getBackendCfg(projectDir = '.') {
11
11
  const backendCfgYamlPath = `${projectDir}/backend.cfg.yaml`;
@@ -1,4 +1,4 @@
1
- import { AjvSchema, getCoercingAjv } from '@naturalcycles/nodejs-lib/ajv';
1
+ import { AjvSchema, getCoercingAjv, JSchema } from '@naturalcycles/nodejs-lib/ajv';
2
2
  import { handleValidationError } from '../validateRequest.util.js';
3
3
  class AjvValidateRequest {
4
4
  body(req, schema, opt = {}) {
@@ -48,8 +48,11 @@ class AjvValidateRequest {
48
48
  const input = req[reqProperty] || {};
49
49
  const { coerceTypes, mutateInput } = opt;
50
50
  const ajv = coerceTypes ? getCoercingAjv() : undefined;
51
- const ajvSchema = AjvSchema.create(schema, { ajv });
52
- const [error, output] = ajvSchema.getValidationResult(input, {
51
+ const validatable = schema instanceof JSchema || schema instanceof AjvSchema
52
+ ? schema
53
+ : new JSchema(schema);
54
+ const [error, output] = validatable.getValidationResult(input, {
55
+ ajv,
53
56
  inputName: `request.${reqProperty}`,
54
57
  getOriginalInput,
55
58
  mutateInput,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/backend-lib",
3
3
  "type": "module",
4
- "version": "9.53.0",
4
+ "version": "9.55.0",
5
5
  "peerDependencies": {
6
6
  "@sentry/node-core": "^10"
7
7
  },
@@ -46,7 +46,6 @@
46
46
  "./deploy/*.js": "./dist/deploy/*.js",
47
47
  "./express/*.js": "./dist/express/*.js",
48
48
  "./validateRequest": "./dist/validation/ajv/validateRequest.js",
49
- "./zodValidateRequest": "./dist/validation/zod/zodValidateRequest.js",
50
49
  "./onFinished": "./dist/onFinished.js",
51
50
  "./testing": "./dist/testing/index.js"
52
51
  },
@@ -1,6 +1,6 @@
1
1
  import { _lazyValue } from '@naturalcycles/js-lib'
2
2
  import type { StringMap } from '@naturalcycles/js-lib/types'
3
- import { AjvSchema } from '@naturalcycles/nodejs-lib/ajv'
3
+ import { JSchema } from '@naturalcycles/nodejs-lib/ajv'
4
4
  import type { JsonSchema } from '@naturalcycles/nodejs-lib/ajv'
5
5
  import { fs2 } from '@naturalcycles/nodejs-lib/fs2'
6
6
  import { yaml2 } from '@naturalcycles/nodejs-lib/yaml2'
@@ -42,7 +42,7 @@ export interface BackendCfg {
42
42
 
43
43
  const getBackendCfgSchema = _lazyValue(() => {
44
44
  const schemaJson = fs2.readJson<JsonSchema<BackendCfg>>(`${resourcesDir}/backendCfg.schema.json`)
45
- return AjvSchema.create(schemaJson, { inputName: 'backend.cfg.yaml' })
45
+ return new JSchema<BackendCfg, false>(schemaJson, { inputName: 'backend.cfg.yaml' })
46
46
  })
47
47
 
48
48
  export function getBackendCfg(projectDir = '.'): BackendCfg {
@@ -1,4 +1,4 @@
1
- import { AjvSchema, getCoercingAjv } from '@naturalcycles/nodejs-lib/ajv'
1
+ import { AjvSchema, getCoercingAjv, JSchema } from '@naturalcycles/nodejs-lib/ajv'
2
2
  import type { AjvValidationError, SchemaHandledByAjv } from '@naturalcycles/nodejs-lib/ajv'
3
3
  import type { BackendRequest } from '../../server/server.model.js'
4
4
  import { handleValidationError } from '../validateRequest.util.js'
@@ -85,9 +85,14 @@ class AjvValidateRequest {
85
85
 
86
86
  const { coerceTypes, mutateInput } = opt
87
87
  const ajv = coerceTypes ? getCoercingAjv() : undefined
88
- const ajvSchema = AjvSchema.create(schema, { ajv })
89
88
 
90
- const [error, output] = ajvSchema.getValidationResult(input, {
89
+ const validatable =
90
+ schema instanceof JSchema || schema instanceof AjvSchema
91
+ ? schema
92
+ : new JSchema<OUT, false>(schema)
93
+
94
+ const [error, output] = validatable.getValidationResult(input, {
95
+ ajv,
91
96
  inputName: `request.${reqProperty}`,
92
97
  getOriginalInput,
93
98
  mutateInput,
@@ -1,21 +0,0 @@
1
- import type { ZodType, ZodValidationError } from '@naturalcycles/js-lib/zod';
2
- import type { BackendRequest } from '../../server/server.model.js';
3
- import type { ReqValidationOptions } from '../validateRequest.util.js';
4
- declare class ZodValidateRequest {
5
- body<T>(req: BackendRequest, schema: ZodType<T>, opt?: ReqValidationOptions<ZodValidationError>): T;
6
- query<T>(req: BackendRequest, schema: ZodType<T>, opt?: ReqValidationOptions<ZodValidationError>): T;
7
- params<T>(req: BackendRequest, schema: ZodType<T>, opt?: ReqValidationOptions<ZodValidationError>): T;
8
- /**
9
- * Validates `req.headers` against the provided schema.
10
- *
11
- * Note: as opposed to other methods, this method does not mutate `req.headers` in case of success,
12
- * i.e. schemas that cast values will not have any effect.
13
- *
14
- * If you wish to mutate `req.headers` with the validated value, use `keepOriginal: false` option.
15
- * Keep in mind that this will also remove all values that are not in the schema.
16
- */
17
- headers<T>(req: BackendRequest, schema: ZodType<T>, opt?: ReqValidationOptions<ZodValidationError>): T;
18
- private validate;
19
- }
20
- export declare const zodValidateRequest: ZodValidateRequest;
21
- export {};
@@ -1,39 +0,0 @@
1
- import { zSafeValidate } from '@naturalcycles/js-lib/zod';
2
- import { handleValidationError } from '../validateRequest.util.js';
3
- class ZodValidateRequest {
4
- body(req, schema, opt = {}) {
5
- return this.validate(req, 'body', schema, opt);
6
- }
7
- query(req, schema, opt = {}) {
8
- return this.validate(req, 'query', schema, opt);
9
- }
10
- params(req, schema, opt = {}) {
11
- return this.validate(req, 'params', schema, opt);
12
- }
13
- /**
14
- * Validates `req.headers` against the provided schema.
15
- *
16
- * Note: as opposed to other methods, this method does not mutate `req.headers` in case of success,
17
- * i.e. schemas that cast values will not have any effect.
18
- *
19
- * If you wish to mutate `req.headers` with the validated value, use `keepOriginal: false` option.
20
- * Keep in mind that this will also remove all values that are not in the schema.
21
- */
22
- headers(req, schema, opt = {}) {
23
- return this.validate(req, 'headers', schema, opt);
24
- }
25
- validate(req, reqProperty, schema, opt = {}) {
26
- const originalProperty = (req[reqProperty] || {});
27
- // Zod does not mutate the input
28
- const [error, data] = zSafeValidate(originalProperty, schema
29
- // opt2?.itemName,
30
- );
31
- if (error) {
32
- handleValidationError(error, originalProperty, opt);
33
- }
34
- // Kirill: decide to not do it, let's see
35
- // req[reqProperty] = data
36
- return data;
37
- }
38
- }
39
- export const zodValidateRequest = new ZodValidateRequest();
@@ -1,75 +0,0 @@
1
- import { zSafeValidate } from '@naturalcycles/js-lib/zod'
2
- import type { ZodType, ZodValidationError } from '@naturalcycles/js-lib/zod'
3
- import type { BackendRequest } from '../../server/server.model.js'
4
- import { handleValidationError } from '../validateRequest.util.js'
5
- import type { ReqValidationOptions } from '../validateRequest.util.js'
6
-
7
- class ZodValidateRequest {
8
- body<T>(
9
- req: BackendRequest,
10
- schema: ZodType<T>,
11
- opt: ReqValidationOptions<ZodValidationError> = {},
12
- ): T {
13
- return this.validate(req, 'body', schema, opt)
14
- }
15
-
16
- query<T>(
17
- req: BackendRequest,
18
- schema: ZodType<T>,
19
- opt: ReqValidationOptions<ZodValidationError> = {},
20
- ): T {
21
- return this.validate(req, 'query', schema, opt)
22
- }
23
-
24
- params<T>(
25
- req: BackendRequest,
26
- schema: ZodType<T>,
27
- opt: ReqValidationOptions<ZodValidationError> = {},
28
- ): T {
29
- return this.validate(req, 'params', schema, opt)
30
- }
31
-
32
- /**
33
- * Validates `req.headers` against the provided schema.
34
- *
35
- * Note: as opposed to other methods, this method does not mutate `req.headers` in case of success,
36
- * i.e. schemas that cast values will not have any effect.
37
- *
38
- * If you wish to mutate `req.headers` with the validated value, use `keepOriginal: false` option.
39
- * Keep in mind that this will also remove all values that are not in the schema.
40
- */
41
- headers<T>(
42
- req: BackendRequest,
43
- schema: ZodType<T>,
44
- opt: ReqValidationOptions<ZodValidationError> = {},
45
- ): T {
46
- return this.validate(req, 'headers', schema, opt)
47
- }
48
-
49
- private validate<T>(
50
- req: BackendRequest,
51
- reqProperty: 'body' | 'params' | 'query' | 'headers',
52
- schema: ZodType<T>,
53
- opt: ReqValidationOptions<ZodValidationError> = {},
54
- ): T {
55
- const originalProperty = (req[reqProperty] || {}) as T
56
-
57
- // Zod does not mutate the input
58
- const [error, data] = zSafeValidate(
59
- originalProperty,
60
- schema,
61
- // opt2?.itemName,
62
- )
63
-
64
- if (error) {
65
- handleValidationError(error, originalProperty, opt)
66
- }
67
-
68
- // Kirill: decide to not do it, let's see
69
- // req[reqProperty] = data
70
-
71
- return data
72
- }
73
- }
74
-
75
- export const zodValidateRequest = new ZodValidateRequest()