@naturalcycles/nodejs-lib 13.45.4 → 14.0.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/bin/generate-build-info.js +15 -18
- package/dist/bin/json2env.js +6 -9
- package/dist/bin/kpy.js +6 -9
- package/dist/bin/secrets-decrypt.d.ts +1 -1
- package/dist/bin/secrets-decrypt.js +9 -12
- package/dist/bin/secrets-encrypt.d.ts +1 -1
- package/dist/bin/secrets-encrypt.js +9 -12
- package/dist/bin/secrets-gen-key.js +8 -11
- package/dist/bin/slack-this.js +6 -9
- package/dist/buffer/buffer.util.js +1 -4
- package/dist/colors/colors.d.ts +26 -27
- package/dist/colors/colors.js +29 -33
- package/dist/csv/csvReader.js +4 -8
- package/dist/csv/csvWriter.js +6 -12
- package/dist/csv/transformToCSV.d.ts +2 -2
- package/dist/csv/transformToCSV.js +5 -8
- package/dist/diff/tableDiff.js +4 -7
- package/dist/fs/fs2.d.ts +1 -1
- package/dist/fs/fs2.js +82 -86
- package/dist/fs/json2env.js +18 -28
- package/dist/fs/kpy.js +28 -33
- package/dist/index.d.ts +71 -71
- package/dist/index.js +73 -78
- package/dist/infra/process.util.js +22 -28
- package/dist/jwt/jwt.service.js +16 -21
- package/dist/log/log.util.js +4 -7
- package/dist/script/runScript.js +11 -15
- package/dist/secret/secrets-decrypt.util.js +18 -22
- package/dist/secret/secrets-encrypt.util.js +17 -21
- package/dist/security/crypto.util.js +24 -34
- package/dist/security/hash.util.js +19 -37
- package/dist/security/id.util.js +11 -18
- package/dist/security/nanoid.js +15 -20
- package/dist/security/secret.util.js +20 -30
- package/dist/slack/slack.service.d.ts +1 -1
- package/dist/slack/slack.service.js +11 -16
- package/dist/slack/slack.service.model.d.ts +1 -1
- package/dist/slack/slack.service.model.js +1 -2
- package/dist/stream/ndjson/ndjson.model.js +7 -11
- package/dist/stream/ndjson/ndjsonMap.d.ts +1 -1
- package/dist/stream/ndjson/ndjsonMap.js +11 -14
- package/dist/stream/ndjson/ndjsonStreamForEach.d.ts +1 -1
- package/dist/stream/ndjson/ndjsonStreamForEach.js +9 -12
- package/dist/stream/ndjson/transformJsonParse.d.ts +1 -1
- package/dist/stream/ndjson/transformJsonParse.js +4 -9
- package/dist/stream/ndjson/transformToNDJson.d.ts +1 -1
- package/dist/stream/ndjson/transformToNDJson.js +5 -8
- package/dist/stream/pipeline/pipeline.js +11 -17
- package/dist/stream/progressLogger.d.ts +1 -1
- package/dist/stream/progressLogger.js +22 -28
- package/dist/stream/readable/readableCreate.d.ts +1 -1
- package/dist/stream/readable/readableCreate.js +5 -9
- package/dist/stream/readable/readableForEach.d.ts +2 -2
- package/dist/stream/readable/readableForEach.js +7 -11
- package/dist/stream/readable/readableFromArray.d.ts +1 -1
- package/dist/stream/readable/readableFromArray.js +4 -7
- package/dist/stream/readable/readableToArray.d.ts +1 -1
- package/dist/stream/readable/readableToArray.js +1 -4
- package/dist/stream/sizeStack.js +7 -11
- package/dist/stream/stream.model.js +1 -2
- package/dist/stream/stream.util.js +1 -4
- package/dist/stream/transform/transformChunk.d.ts +1 -1
- package/dist/stream/transform/transformChunk.js +3 -6
- package/dist/stream/transform/transformFilter.d.ts +2 -2
- package/dist/stream/transform/transformFilter.js +6 -10
- package/dist/stream/transform/transformLimit.d.ts +2 -2
- package/dist/stream/transform/transformLimit.js +6 -11
- package/dist/stream/transform/transformLogProgress.d.ts +2 -2
- package/dist/stream/transform/transformLogProgress.js +5 -8
- package/dist/stream/transform/transformMap.d.ts +1 -1
- package/dist/stream/transform/transformMap.js +18 -22
- package/dist/stream/transform/transformMapSimple.d.ts +1 -1
- package/dist/stream/transform/transformMapSimple.js +6 -9
- package/dist/stream/transform/transformMapSync.d.ts +3 -3
- package/dist/stream/transform/transformMapSync.js +14 -19
- package/dist/stream/transform/transformNoOp.d.ts +1 -1
- package/dist/stream/transform/transformNoOp.js +3 -6
- package/dist/stream/transform/transformOffset.d.ts +1 -1
- package/dist/stream/transform/transformOffset.js +5 -8
- package/dist/stream/transform/transformSplit.d.ts +1 -1
- package/dist/stream/transform/transformSplit.js +5 -9
- package/dist/stream/transform/transformTap.d.ts +1 -1
- package/dist/stream/transform/transformTap.js +3 -6
- package/dist/stream/transform/transformTee.d.ts +1 -1
- package/dist/stream/transform/transformTee.js +7 -10
- package/dist/stream/transform/transformThrottle.d.ts +1 -1
- package/dist/stream/transform/transformThrottle.js +9 -12
- package/dist/stream/transform/transformToArray.d.ts +1 -1
- package/dist/stream/transform/transformToArray.js +3 -6
- package/dist/stream/transform/worker/baseWorkerClass.d.ts +1 -1
- package/dist/stream/transform/worker/baseWorkerClass.js +1 -5
- package/dist/stream/transform/worker/transformMultiThreaded.d.ts +1 -1
- package/dist/stream/transform/worker/transformMultiThreaded.js +12 -11
- package/dist/stream/transform/worker/transformMultiThreaded.model.js +1 -2
- package/dist/stream/transform/worker/workerClassProxy.js +6 -4
- package/dist/stream/writable/writableForEach.d.ts +2 -2
- package/dist/stream/writable/writableForEach.js +6 -10
- package/dist/stream/writable/writableFork.d.ts +1 -1
- package/dist/stream/writable/writableFork.js +6 -9
- package/dist/stream/writable/writableLimit.d.ts +1 -1
- package/dist/stream/writable/writableLimit.js +3 -6
- package/dist/stream/writable/writablePushToArray.d.ts +1 -1
- package/dist/stream/writable/writablePushToArray.js +3 -6
- package/dist/stream/writable/writableVoid.d.ts +1 -1
- package/dist/stream/writable/writableVoid.js +3 -6
- package/dist/string/inspect.js +8 -13
- package/dist/util/buildInfo.util.js +12 -15
- package/dist/util/env.util.js +5 -9
- package/dist/util/exec2.js +17 -21
- package/dist/util/git2.js +17 -20
- package/dist/util/lruMemoCache.js +3 -7
- package/dist/util/zip.util.js +14 -25
- package/dist/validation/ajv/ajv.util.d.ts +3 -3
- package/dist/validation/ajv/ajv.util.js +6 -10
- package/dist/validation/ajv/ajvSchema.d.ts +2 -2
- package/dist/validation/ajv/ajvSchema.js +14 -18
- package/dist/validation/ajv/ajvValidationError.js +2 -6
- package/dist/validation/ajv/getAjv.d.ts +1 -1
- package/dist/validation/ajv/getAjv.js +9 -9
- package/dist/validation/joi/joi.extensions.d.ts +2 -2
- package/dist/validation/joi/joi.extensions.js +6 -10
- package/dist/validation/joi/joi.model.js +1 -2
- package/dist/validation/joi/joi.shared.schemas.d.ts +2 -2
- package/dist/validation/joi/joi.shared.schemas.js +61 -74
- package/dist/validation/joi/joi.validation.error.js +2 -6
- package/dist/validation/joi/joi.validation.util.d.ts +1 -1
- package/dist/validation/joi/joi.validation.util.js +10 -17
- package/dist/validation/joi/number.extensions.js +1 -4
- package/dist/validation/joi/string.extensions.js +4 -7
- package/dist/yargs.util.js +5 -8
- package/package.json +5 -4
- package/src/bin/generate-build-info.ts +3 -3
- package/src/bin/json2env.ts +2 -2
- package/src/bin/kpy.ts +2 -2
- package/src/bin/secrets-decrypt.ts +7 -8
- package/src/bin/secrets-encrypt.ts +7 -8
- package/src/bin/secrets-gen-key.ts +2 -2
- package/src/bin/slack-this.ts +2 -2
- package/src/csv/transformToCSV.ts +3 -3
- package/src/fs/fs2.ts +4 -4
- package/src/fs/json2env.ts +2 -2
- package/src/fs/kpy.ts +2 -2
- package/src/index.ts +71 -71
- package/src/jwt/jwt.service.ts +2 -2
- package/src/log/log.util.ts +1 -1
- package/src/script/runScript.ts +4 -3
- package/src/secret/secrets-decrypt.util.ts +3 -3
- package/src/secret/secrets-encrypt.util.ts +3 -3
- package/src/security/crypto.util.ts +1 -1
- package/src/security/id.util.ts +1 -1
- package/src/security/secret.util.ts +2 -2
- package/src/slack/slack.service.model.ts +1 -1
- package/src/slack/slack.service.ts +3 -3
- package/src/stream/ndjson/ndjson.model.ts +1 -1
- package/src/stream/ndjson/ndjsonMap.ts +2 -2
- package/src/stream/ndjson/ndjsonStreamForEach.ts +2 -2
- package/src/stream/ndjson/transformJsonParse.ts +1 -1
- package/src/stream/ndjson/transformToNDJson.ts +1 -1
- package/src/stream/pipeline/pipeline.ts +1 -1
- package/src/stream/progressLogger.ts +3 -3
- package/src/stream/readable/readableCreate.ts +1 -1
- package/src/stream/readable/readableForEach.ts +4 -4
- package/src/stream/readable/readableFromArray.ts +1 -1
- package/src/stream/readable/readableToArray.ts +1 -1
- package/src/stream/sizeStack.ts +2 -2
- package/src/stream/transform/transformChunk.ts +1 -1
- package/src/stream/transform/transformFilter.ts +3 -3
- package/src/stream/transform/transformLimit.ts +3 -3
- package/src/stream/transform/transformLogProgress.ts +3 -3
- package/src/stream/transform/transformMap.ts +5 -5
- package/src/stream/transform/transformMapSimple.ts +1 -1
- package/src/stream/transform/transformMapSync.ts +5 -5
- package/src/stream/transform/transformNoOp.ts +1 -1
- package/src/stream/transform/transformOffset.ts +2 -2
- package/src/stream/transform/transformSplit.ts +1 -1
- package/src/stream/transform/transformTap.ts +1 -1
- package/src/stream/transform/transformTee.ts +3 -3
- package/src/stream/transform/transformThrottle.ts +1 -1
- package/src/stream/transform/transformToArray.ts +1 -1
- package/src/stream/transform/worker/baseWorkerClass.ts +1 -1
- package/src/stream/transform/worker/transformMultiThreaded.ts +8 -3
- package/src/stream/transform/worker/workerClassProxy.js +6 -4
- package/src/stream/writable/writableForEach.ts +3 -3
- package/src/stream/writable/writableFork.ts +3 -3
- package/src/stream/writable/writableLimit.ts +1 -1
- package/src/stream/writable/writablePushToArray.ts +1 -1
- package/src/stream/writable/writableVoid.ts +1 -1
- package/src/util/buildInfo.util.ts +2 -2
- package/src/util/exec2.ts +1 -1
- package/src/util/git2.ts +2 -2
- package/src/validation/ajv/ajv.util.ts +4 -4
- package/src/validation/ajv/ajvSchema.ts +4 -5
- package/src/validation/ajv/getAjv.ts +7 -3
- package/src/validation/joi/joi.extensions.ts +4 -4
- package/src/validation/joi/joi.shared.schemas.ts +3 -3
- package/src/validation/joi/joi.validation.util.ts +2 -2
- package/src/yargs.util.ts +1 -0
package/dist/util/zip.util.js
CHANGED
|
@@ -1,58 +1,47 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
exports.gzipBuffer = gzipBuffer;
|
|
8
|
-
exports.gunzipBuffer = gunzipBuffer;
|
|
9
|
-
exports.gzipString = gzipString;
|
|
10
|
-
exports.gunzipToString = gunzipToString;
|
|
11
|
-
const tslib_1 = require("tslib");
|
|
12
|
-
const node_util_1 = require("node:util");
|
|
13
|
-
const node_zlib_1 = tslib_1.__importDefault(require("node:zlib"));
|
|
14
|
-
const deflate = (0, node_util_1.promisify)(node_zlib_1.default.deflate.bind(node_zlib_1.default));
|
|
15
|
-
const inflate = (0, node_util_1.promisify)(node_zlib_1.default.inflate.bind(node_zlib_1.default));
|
|
16
|
-
const gzip = (0, node_util_1.promisify)(node_zlib_1.default.gzip.bind(node_zlib_1.default));
|
|
17
|
-
const gunzip = (0, node_util_1.promisify)(node_zlib_1.default.gunzip.bind(node_zlib_1.default));
|
|
1
|
+
import { promisify } from 'node:util';
|
|
2
|
+
import zlib from 'node:zlib';
|
|
3
|
+
const deflate = promisify(zlib.deflate.bind(zlib));
|
|
4
|
+
const inflate = promisify(zlib.inflate.bind(zlib));
|
|
5
|
+
const gzip = promisify(zlib.gzip.bind(zlib));
|
|
6
|
+
const gunzip = promisify(zlib.gunzip.bind(zlib));
|
|
18
7
|
// string > compressed buffer
|
|
19
8
|
/**
|
|
20
9
|
* deflateBuffer uses `deflate`.
|
|
21
10
|
* It's 9 bytes shorter than `gzip`.
|
|
22
11
|
*/
|
|
23
|
-
async function deflateBuffer(buf, options = {}) {
|
|
12
|
+
export async function deflateBuffer(buf, options = {}) {
|
|
24
13
|
return await deflate(buf, options);
|
|
25
14
|
}
|
|
26
|
-
async function inflateBuffer(buf, options = {}) {
|
|
15
|
+
export async function inflateBuffer(buf, options = {}) {
|
|
27
16
|
return await inflate(buf, options);
|
|
28
17
|
}
|
|
29
18
|
/**
|
|
30
19
|
* deflateString uses `deflate`.
|
|
31
20
|
* It's 9 bytes shorter than `gzip`.
|
|
32
21
|
*/
|
|
33
|
-
async function deflateString(s, options) {
|
|
22
|
+
export async function deflateString(s, options) {
|
|
34
23
|
return await deflateBuffer(Buffer.from(s), options);
|
|
35
24
|
}
|
|
36
|
-
async function inflateToString(buf, options) {
|
|
25
|
+
export async function inflateToString(buf, options) {
|
|
37
26
|
return (await inflateBuffer(buf, options)).toString();
|
|
38
27
|
}
|
|
39
28
|
/**
|
|
40
29
|
* gzipBuffer uses `gzip`
|
|
41
30
|
* It's 9 bytes longer than `deflate`.
|
|
42
31
|
*/
|
|
43
|
-
async function gzipBuffer(buf, options = {}) {
|
|
32
|
+
export async function gzipBuffer(buf, options = {}) {
|
|
44
33
|
return await gzip(buf, options);
|
|
45
34
|
}
|
|
46
|
-
async function gunzipBuffer(buf, options = {}) {
|
|
35
|
+
export async function gunzipBuffer(buf, options = {}) {
|
|
47
36
|
return await gunzip(buf, options);
|
|
48
37
|
}
|
|
49
38
|
/**
|
|
50
39
|
* gzipString uses `gzip`.
|
|
51
40
|
* It's 9 bytes longer than `deflate`.
|
|
52
41
|
*/
|
|
53
|
-
async function gzipString(s, options) {
|
|
42
|
+
export async function gzipString(s, options) {
|
|
54
43
|
return await gzipBuffer(Buffer.from(s), options);
|
|
55
44
|
}
|
|
56
|
-
async function gunzipToString(buf, options) {
|
|
45
|
+
export async function gunzipToString(buf, options) {
|
|
57
46
|
return (await gunzipBuffer(buf, options)).toString();
|
|
58
47
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { JsonSchema } from '@naturalcycles/js-lib';
|
|
2
|
-
import type { FastGlobOptions } from '
|
|
3
|
-
import type { AjvSchemaCfg } from './ajvSchema';
|
|
4
|
-
import { AjvSchema } from './ajvSchema';
|
|
2
|
+
import type { FastGlobOptions } from '../../index.js';
|
|
3
|
+
import type { AjvSchemaCfg } from './ajvSchema.js';
|
|
4
|
+
import { AjvSchema } from './ajvSchema.js';
|
|
5
5
|
/**
|
|
6
6
|
* Does fs.readFileSync + JSON.parse for ALL files matching the passed `glob` pattern.
|
|
7
7
|
* E.g `someDir/**\/*.schema.json`
|
|
@@ -1,9 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.readJsonSchemas = readJsonSchemas;
|
|
4
|
-
exports.readAjvSchemas = readAjvSchemas;
|
|
5
|
-
const __1 = require("../..");
|
|
6
|
-
const ajvSchema_1 = require("./ajvSchema");
|
|
1
|
+
import { fastGlob, fs2 } from '../../index.js';
|
|
2
|
+
import { AjvSchema } from './ajvSchema.js';
|
|
7
3
|
/**
|
|
8
4
|
* Does fs.readFileSync + JSON.parse for ALL files matching the passed `glob` pattern.
|
|
9
5
|
* E.g `someDir/**\/*.schema.json`
|
|
@@ -12,8 +8,8 @@ const ajvSchema_1 = require("./ajvSchema");
|
|
|
12
8
|
*
|
|
13
9
|
* @experimental
|
|
14
10
|
*/
|
|
15
|
-
function readJsonSchemas(patterns, opt) {
|
|
16
|
-
return
|
|
11
|
+
export function readJsonSchemas(patterns, opt) {
|
|
12
|
+
return fastGlob.sync(patterns, opt).map(fileName => fs2.readJson(fileName));
|
|
17
13
|
}
|
|
18
14
|
/**
|
|
19
15
|
* Reads json schemas from given dir (glob pattern).
|
|
@@ -22,9 +18,9 @@ function readJsonSchemas(patterns, opt) {
|
|
|
22
18
|
*
|
|
23
19
|
* @experimental
|
|
24
20
|
*/
|
|
25
|
-
function readAjvSchemas(patterns, cfg) {
|
|
21
|
+
export function readAjvSchemas(patterns, cfg) {
|
|
26
22
|
const schemas = readJsonSchemas(patterns);
|
|
27
|
-
return schemas.map(schema =>
|
|
23
|
+
return schemas.map(schema => AjvSchema.create(schema, {
|
|
28
24
|
schemas,
|
|
29
25
|
...cfg,
|
|
30
26
|
}));
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { CommonLogger, JsonSchema, JsonSchemaBuilder } from '@naturalcycles/js-lib';
|
|
2
|
-
import type Ajv from 'ajv';
|
|
3
|
-
import { AjvValidationError } from './ajvValidationError';
|
|
2
|
+
import type { Ajv } from 'ajv';
|
|
3
|
+
import { AjvValidationError } from './ajvValidationError.js';
|
|
4
4
|
export interface AjvValidationOptions {
|
|
5
5
|
objectName?: string;
|
|
6
6
|
objectId?: string;
|
|
@@ -1,17 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const index_1 = require("../../index");
|
|
6
|
-
const ajvValidationError_1 = require("./ajvValidationError");
|
|
7
|
-
const getAjv_1 = require("./getAjv");
|
|
1
|
+
import { _filterNullishValues, _isObject, _substringBefore, JsonSchemaAnyBuilder, } from '@naturalcycles/js-lib';
|
|
2
|
+
import { _inspect, fs2, requireFileToExist } from '../../index.js';
|
|
3
|
+
import { AjvValidationError } from './ajvValidationError.js';
|
|
4
|
+
import { getAjv } from './getAjv.js';
|
|
8
5
|
/**
|
|
9
6
|
* On creation - compiles ajv validation function.
|
|
10
7
|
* Provides convenient methods, error reporting, etc.
|
|
11
8
|
*
|
|
12
9
|
* @experimental
|
|
13
10
|
*/
|
|
14
|
-
class AjvSchema {
|
|
11
|
+
export class AjvSchema {
|
|
15
12
|
schema;
|
|
16
13
|
constructor(schema, cfg = {}) {
|
|
17
14
|
this.schema = schema;
|
|
@@ -21,11 +18,11 @@ class AjvSchema {
|
|
|
21
18
|
separator: '\n',
|
|
22
19
|
...cfg,
|
|
23
20
|
ajv: cfg.ajv ||
|
|
24
|
-
|
|
21
|
+
getAjv({
|
|
25
22
|
schemas: cfg.schemas?.map(s => {
|
|
26
23
|
if (s instanceof AjvSchema)
|
|
27
24
|
return s.schema;
|
|
28
|
-
if (s instanceof
|
|
25
|
+
if (s instanceof JsonSchemaAnyBuilder)
|
|
29
26
|
return s.build();
|
|
30
27
|
return s;
|
|
31
28
|
}),
|
|
@@ -33,7 +30,7 @@ class AjvSchema {
|
|
|
33
30
|
// verbose: true,
|
|
34
31
|
}),
|
|
35
32
|
// Auto-detecting "ObjectName" from $id of the schema (e.g "Address.schema.json")
|
|
36
|
-
objectName: cfg.objectName || (schema.$id ?
|
|
33
|
+
objectName: cfg.objectName || (schema.$id ? _substringBefore(schema.$id, '.') : undefined),
|
|
37
34
|
};
|
|
38
35
|
this.validateFunction = this.cfg.ajv.compile(schema);
|
|
39
36
|
}
|
|
@@ -49,7 +46,7 @@ class AjvSchema {
|
|
|
49
46
|
static create(schema, cfg = {}) {
|
|
50
47
|
if (schema instanceof AjvSchema)
|
|
51
48
|
return schema;
|
|
52
|
-
if (schema instanceof
|
|
49
|
+
if (schema instanceof JsonSchemaAnyBuilder) {
|
|
53
50
|
return new AjvSchema(schema.build(), cfg);
|
|
54
51
|
}
|
|
55
52
|
return new AjvSchema(schema, cfg);
|
|
@@ -59,8 +56,8 @@ class AjvSchema {
|
|
|
59
56
|
* Convenient method that just does fs.readFileSync for you.
|
|
60
57
|
*/
|
|
61
58
|
static readJsonSync(filePath, cfg = {}) {
|
|
62
|
-
|
|
63
|
-
const schema =
|
|
59
|
+
requireFileToExist(filePath);
|
|
60
|
+
const schema = fs2.readJson(filePath);
|
|
64
61
|
return new AjvSchema(schema, cfg);
|
|
65
62
|
}
|
|
66
63
|
cfg;
|
|
@@ -83,18 +80,18 @@ class AjvSchema {
|
|
|
83
80
|
if (this.isValid(obj))
|
|
84
81
|
return;
|
|
85
82
|
const errors = this.validateFunction.errors;
|
|
86
|
-
const { objectId =
|
|
83
|
+
const { objectId = _isObject(obj) ? obj['id'] : undefined, objectName = this.cfg.objectName, logErrors = this.cfg.logErrors, separator = this.cfg.separator, } = opt;
|
|
87
84
|
const name = [objectName || 'Object', objectId].filter(Boolean).join('.');
|
|
88
85
|
let message = this.cfg.ajv.errorsText(errors, {
|
|
89
86
|
dataVar: name,
|
|
90
87
|
separator,
|
|
91
88
|
});
|
|
92
|
-
const strValue =
|
|
89
|
+
const strValue = _inspect(obj, { maxLen: 1000 });
|
|
93
90
|
message = [message, 'Input: ' + strValue].join(separator);
|
|
94
91
|
if (logErrors) {
|
|
95
92
|
this.cfg.logger.error(errors);
|
|
96
93
|
}
|
|
97
|
-
return new
|
|
94
|
+
return new AjvValidationError(message, _filterNullishValues({
|
|
98
95
|
errors,
|
|
99
96
|
userFriendly: true,
|
|
100
97
|
objectName,
|
|
@@ -105,4 +102,3 @@ class AjvSchema {
|
|
|
105
102
|
return this.validateFunction(obj);
|
|
106
103
|
}
|
|
107
104
|
}
|
|
108
|
-
exports.AjvSchema = AjvSchema;
|
|
@@ -1,12 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.AjvValidationError = void 0;
|
|
4
|
-
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
5
|
-
class AjvValidationError extends js_lib_1.AppError {
|
|
1
|
+
import { AppError } from '@naturalcycles/js-lib';
|
|
2
|
+
export class AjvValidationError extends AppError {
|
|
6
3
|
constructor(message, data) {
|
|
7
4
|
super(message, data, {
|
|
8
5
|
name: 'AjvValidationError',
|
|
9
6
|
});
|
|
10
7
|
}
|
|
11
8
|
}
|
|
12
|
-
exports.AjvValidationError = AjvValidationError;
|
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const ajv_1 = tslib_1.__importDefault(require("ajv"));
|
|
1
|
+
import { Ajv } from 'ajv';
|
|
2
|
+
import ajvFormats from 'ajv-formats';
|
|
3
|
+
import ajvKeywords from 'ajv-keywords';
|
|
6
4
|
const AJV_OPTIONS = {
|
|
7
5
|
removeAdditional: true,
|
|
8
6
|
allErrors: true,
|
|
@@ -17,8 +15,8 @@ const AJV_OPTIONS = {
|
|
|
17
15
|
*
|
|
18
16
|
* https://ajv.js.org/options.html
|
|
19
17
|
*/
|
|
20
|
-
function getAjv(opt) {
|
|
21
|
-
const ajv = new
|
|
18
|
+
export function getAjv(opt) {
|
|
19
|
+
const ajv = new Ajv({
|
|
22
20
|
...AJV_OPTIONS,
|
|
23
21
|
...opt,
|
|
24
22
|
});
|
|
@@ -26,9 +24,11 @@ function getAjv(opt) {
|
|
|
26
24
|
addCustomAjvFormats(ajv);
|
|
27
25
|
// Adds ajv "formats"
|
|
28
26
|
// https://ajv.js.org/guide/formats.html
|
|
29
|
-
|
|
27
|
+
// @ts-expect-error types are wrong
|
|
28
|
+
ajvFormats(ajv);
|
|
30
29
|
// https://ajv.js.org/packages/ajv-keywords.html
|
|
31
|
-
|
|
30
|
+
// @ts-expect-error types are wrong
|
|
31
|
+
ajvKeywords(ajv, [
|
|
32
32
|
'transform', // trim, toLowerCase, etc.
|
|
33
33
|
'uniqueItemProperties',
|
|
34
34
|
'instanceof',
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import JoiLib from 'joi';
|
|
2
|
-
import type { NumberSchema } from './number.extensions';
|
|
3
|
-
import type { StringSchema } from './string.extensions';
|
|
2
|
+
import type { NumberSchema } from './number.extensions.js';
|
|
3
|
+
import type { StringSchema } from './string.extensions.js';
|
|
4
4
|
export interface ExtendedJoi extends JoiLib.Root {
|
|
5
5
|
string: <TSchema = string>() => StringSchema<TSchema>;
|
|
6
6
|
number: <TSchema = number>() => NumberSchema<TSchema>;
|
|
@@ -1,15 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const joi_1 = tslib_1.__importDefault(require("joi"));
|
|
6
|
-
const number_extensions_1 = require("./number.extensions");
|
|
7
|
-
const string_extensions_1 = require("./string.extensions");
|
|
1
|
+
import JoiLib from 'joi';
|
|
2
|
+
import { numberExtensions } from './number.extensions.js';
|
|
3
|
+
import { stringExtensions } from './string.extensions.js';
|
|
8
4
|
/**
|
|
9
5
|
* This is the only right place to import Joi from
|
|
10
6
|
*/
|
|
11
7
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
12
|
-
|
|
8
|
+
export const Joi = JoiLib.defaults(schema => {
|
|
13
9
|
// hack to prevent infinite recursion due to .empty('') where '' is a stringSchema itself
|
|
14
10
|
if (schema.type === 'string') {
|
|
15
11
|
return (schema
|
|
@@ -25,5 +21,5 @@ exports.Joi = joi_1.default.defaults(schema => {
|
|
|
25
21
|
// return schema.empty(null)
|
|
26
22
|
return schema;
|
|
27
23
|
})
|
|
28
|
-
.extend((joi) =>
|
|
29
|
-
.extend((joi) =>
|
|
24
|
+
.extend((joi) => stringExtensions(joi))
|
|
25
|
+
.extend((joi) => numberExtensions(joi));
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
// export interface ObjectSchema<T = any> extends JoiObjectSchema<T> {
|
|
3
2
|
// // Open-ended index signature to allow for easier .concat(baseDBEntitySchema)
|
|
4
3
|
// [k: string]: any
|
|
5
4
|
// }
|
|
6
|
-
|
|
5
|
+
export {};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { BaseDBEntity, IANATimezone, IsoDateTime, NumberEnum, StringEnum, UnixTimestamp, UnixTimestampMillis } from '@naturalcycles/js-lib';
|
|
2
2
|
import type { AlternativesSchema, AnySchema, ArraySchema, ObjectSchema } from 'joi';
|
|
3
|
-
import type { NumberSchema } from './number.extensions';
|
|
4
|
-
import type { StringSchema } from './string.extensions';
|
|
3
|
+
import type { NumberSchema } from './number.extensions.js';
|
|
4
|
+
import type { StringSchema } from './string.extensions.js';
|
|
5
5
|
export declare const booleanSchema: import("joi").BooleanSchema<boolean>;
|
|
6
6
|
export declare const booleanDefaultToFalseSchema: import("joi").BooleanSchema<boolean>;
|
|
7
7
|
export declare const stringSchema: StringSchema<string>;
|
|
@@ -1,150 +1,137 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
exports.numberSchema = joi_extensions_1.Joi.number();
|
|
15
|
-
const numberSchemaTyped = () => joi_extensions_1.Joi.number();
|
|
16
|
-
exports.numberSchemaTyped = numberSchemaTyped;
|
|
17
|
-
exports.integerSchema = joi_extensions_1.Joi.number().integer();
|
|
18
|
-
exports.percentageSchema = joi_extensions_1.Joi.number().integer().min(0).max(100);
|
|
19
|
-
exports.dateStringSchema = exports.stringSchema.dateString();
|
|
20
|
-
exports.binarySchema = joi_extensions_1.Joi.binary();
|
|
21
|
-
exports.dateObjectSchema = joi_extensions_1.Joi.object().instance(Date);
|
|
1
|
+
import { _numberEnumKeys, _numberEnumValues, _stringEnumKeys, _stringEnumValues, } from '@naturalcycles/js-lib';
|
|
2
|
+
import { Joi } from './joi.extensions.js';
|
|
3
|
+
export const booleanSchema = Joi.boolean();
|
|
4
|
+
export const booleanDefaultToFalseSchema = Joi.boolean().default(false);
|
|
5
|
+
export const stringSchema = Joi.string();
|
|
6
|
+
export const stringSchemaTyped = () => Joi.string();
|
|
7
|
+
export const numberSchema = Joi.number();
|
|
8
|
+
export const numberSchemaTyped = () => Joi.number();
|
|
9
|
+
export const integerSchema = Joi.number().integer();
|
|
10
|
+
export const percentageSchema = Joi.number().integer().min(0).max(100);
|
|
11
|
+
export const dateStringSchema = stringSchema.dateString();
|
|
12
|
+
export const binarySchema = Joi.binary();
|
|
13
|
+
export const dateObjectSchema = Joi.object().instance(Date);
|
|
22
14
|
const DATE_INTERVAL_REGEX = /^\d{4}-\d{2}-\d{2}\/\d{4}-\d{2}-\d{2}$/;
|
|
23
|
-
|
|
15
|
+
export const dateIntervalStringSchema = stringSchema.regex(DATE_INTERVAL_REGEX).messages({
|
|
24
16
|
'string.pattern.base': `must be a DateInterval string`,
|
|
25
17
|
});
|
|
26
|
-
|
|
27
|
-
|
|
18
|
+
export const DATE_TIME_STRING_REGEX = /^\d{4}-\d{2}-\d{2}(?:T\d{2}:\d{2}:\d{2}(?:\.\d{1,3})?(?:Z|[+-]\d{2}:\d{2})?)?$/;
|
|
19
|
+
export const dateTimeStringSchema = stringSchema.regex(DATE_TIME_STRING_REGEX).messages({
|
|
28
20
|
'string.pattern.base': `must be a DateTime string`,
|
|
29
21
|
});
|
|
30
22
|
/**
|
|
31
23
|
* Allows all values of a String Enum.
|
|
32
24
|
*/
|
|
33
|
-
const stringEnumValueSchema = (en) =>
|
|
34
|
-
exports.stringEnumValueSchema = stringEnumValueSchema;
|
|
25
|
+
export const stringEnumValueSchema = (en) => Joi.string().valid(..._stringEnumValues(en));
|
|
35
26
|
/**
|
|
36
27
|
* Allows all keys of a String Enum.
|
|
37
28
|
*/
|
|
38
|
-
const stringEnumKeySchema = (en) =>
|
|
39
|
-
exports.stringEnumKeySchema = stringEnumKeySchema;
|
|
29
|
+
export const stringEnumKeySchema = (en) => Joi.string().valid(..._stringEnumKeys(en));
|
|
40
30
|
/**
|
|
41
31
|
* Allows all values of a String Enum.
|
|
42
32
|
*/
|
|
43
|
-
const numberEnumValueSchema = (en) =>
|
|
44
|
-
exports.numberEnumValueSchema = numberEnumValueSchema;
|
|
33
|
+
export const numberEnumValueSchema = (en) => Joi.number().valid(..._numberEnumValues(en));
|
|
45
34
|
/**
|
|
46
35
|
* Allows all keys of a Number Enum.
|
|
47
36
|
*/
|
|
48
|
-
const numberEnumKeySchema = (en) =>
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
function arraySchema(items) {
|
|
53
|
-
return items ? joi_extensions_1.Joi.array().items(items) : joi_extensions_1.Joi.array();
|
|
37
|
+
export const numberEnumKeySchema = (en) => Joi.string().valid(..._numberEnumKeys(en));
|
|
38
|
+
export const urlSchema = (scheme = 'https') => Joi.string().uri({ scheme });
|
|
39
|
+
export function arraySchema(items) {
|
|
40
|
+
return items ? Joi.array().items(items) : Joi.array();
|
|
54
41
|
}
|
|
55
|
-
function objectSchema(schema) {
|
|
56
|
-
return
|
|
42
|
+
export function objectSchema(schema) {
|
|
43
|
+
return Joi.object(schema);
|
|
57
44
|
}
|
|
58
|
-
function oneOfSchema(...schemas) {
|
|
59
|
-
return
|
|
45
|
+
export function oneOfSchema(...schemas) {
|
|
46
|
+
return Joi.alternatives(schemas);
|
|
60
47
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
48
|
+
export const anySchema = Joi.any();
|
|
49
|
+
export const anyObjectSchema = Joi.object().options({ stripUnknown: false });
|
|
50
|
+
export const BASE62_REGEX = /^[a-zA-Z0-9]+$/;
|
|
51
|
+
export const BASE64_REGEX = /^[a-zA-Z0-9+/]+={0,2}$/;
|
|
52
|
+
export const BASE64URL_REGEX = /^[a-zA-Z0-9_-]+$/;
|
|
53
|
+
export const base62Schema = stringSchema.regex(BASE62_REGEX);
|
|
54
|
+
export const base64Schema = stringSchema.regex(BASE64_REGEX);
|
|
55
|
+
export const base64UrlSchema = stringSchema.regex(BASE64URL_REGEX);
|
|
56
|
+
export const JWT_REGEX = /^[\w-]+\.[\w-]+\.[\w-]+$/;
|
|
57
|
+
export const jwtSchema = stringSchema.regex(JWT_REGEX);
|
|
71
58
|
// 1g498efj5sder3324zer
|
|
72
59
|
/**
|
|
73
60
|
* [a-zA-Z0-9_]*
|
|
74
61
|
* 6-64 length
|
|
75
62
|
*/
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
63
|
+
export const idSchema = stringSchema.regex(/^[a-zA-Z0-9_]{6,64}$/);
|
|
64
|
+
export const idBase62Schema = base62Schema.min(8).max(64);
|
|
65
|
+
export const idBase64Schema = base64Schema.min(8).max(64);
|
|
66
|
+
export const idBase64UrlSchema = base64UrlSchema.min(8).max(64);
|
|
80
67
|
/**
|
|
81
68
|
* `_` should NOT be allowed to be able to use slug-ids as part of natural ids with `_` separator.
|
|
82
69
|
*/
|
|
83
|
-
|
|
70
|
+
export const SLUG_REGEX = /^[a-z0-9-]*$/;
|
|
84
71
|
/**
|
|
85
72
|
* "Slug" - a valid URL, filename, etc.
|
|
86
73
|
*/
|
|
87
|
-
|
|
74
|
+
export const slugSchema = stringSchema.regex(SLUG_REGEX).min(1).max(255);
|
|
88
75
|
const TS_2500 = 16725225600; // 2500-01-01
|
|
89
76
|
const TS_2000 = 946684800; // 2000-01-01
|
|
90
77
|
/**
|
|
91
78
|
* Between years 1970 and 2050
|
|
92
79
|
*/
|
|
93
|
-
|
|
80
|
+
export const unixTimestampSchema = numberSchema
|
|
94
81
|
.integer()
|
|
95
82
|
.min(0)
|
|
96
83
|
.max(TS_2500);
|
|
97
84
|
/**
|
|
98
85
|
* Between years 2000 and 2050
|
|
99
86
|
*/
|
|
100
|
-
|
|
87
|
+
export const unixTimestamp2000Schema = numberSchema
|
|
101
88
|
.integer()
|
|
102
89
|
.min(TS_2000)
|
|
103
90
|
.max(TS_2500);
|
|
104
91
|
/**
|
|
105
92
|
* Between years 1970 and 2050
|
|
106
93
|
*/
|
|
107
|
-
|
|
94
|
+
export const unixTimestampMillisSchema = numberSchema
|
|
108
95
|
.integer()
|
|
109
96
|
.min(0)
|
|
110
97
|
.max(TS_2500 * 1000);
|
|
111
98
|
/**
|
|
112
99
|
* Between years 2000 and 2050
|
|
113
100
|
*/
|
|
114
|
-
|
|
101
|
+
export const unixTimestampMillis2000Schema = numberSchema
|
|
115
102
|
.integer()
|
|
116
103
|
.min(TS_2000 * 1000)
|
|
117
104
|
.max(TS_2500 * 1000);
|
|
118
105
|
// 2
|
|
119
|
-
|
|
106
|
+
export const verSchema = numberSchema.optional().integer().min(1).max(100);
|
|
120
107
|
/**
|
|
121
108
|
* Be careful, by default emailSchema does TLD validation. To disable it - use `stringSchema.email({tld: false}).lowercase()`
|
|
122
109
|
*/
|
|
123
|
-
|
|
110
|
+
export const emailSchema = stringSchema.email().lowercase();
|
|
124
111
|
/**
|
|
125
112
|
* Pattern is simplified for our use, it's not a canonical SemVer.
|
|
126
113
|
*/
|
|
127
|
-
|
|
128
|
-
|
|
114
|
+
export const SEM_VER_REGEX = /^[0-9]+\.[0-9]+\.[0-9]+$/;
|
|
115
|
+
export const semVerSchema = stringSchema.regex(SEM_VER_REGEX);
|
|
129
116
|
// todo: .error(() => 'should be SemVer')
|
|
130
|
-
|
|
117
|
+
export const userAgentSchema = stringSchema
|
|
131
118
|
.min(5) // I've seen UA of `Android` (7 characters)
|
|
132
119
|
.max(400);
|
|
133
|
-
|
|
120
|
+
export const utcOffsetSchema = numberSchema
|
|
134
121
|
.min(-14 * 60)
|
|
135
122
|
.max(14 * 60)
|
|
136
123
|
.dividable(15);
|
|
137
|
-
|
|
124
|
+
export const ianaTimezoneSchema = stringSchema
|
|
138
125
|
// UTC is added to assist unit-testing, which uses UTC by default (not technically a valid Iana timezone identifier)
|
|
139
126
|
.valid(...Intl.supportedValuesOf('timeZone'), 'UTC')
|
|
140
127
|
.messages({
|
|
141
128
|
'any.only': `must be a valid IANA timezone string`,
|
|
142
129
|
});
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
id:
|
|
146
|
-
created:
|
|
147
|
-
updated:
|
|
130
|
+
export const ipAddressSchema = stringSchema.ip();
|
|
131
|
+
export const baseDBEntitySchema = objectSchema({
|
|
132
|
+
id: stringSchema.optional(),
|
|
133
|
+
created: unixTimestamp2000Schema.optional(),
|
|
134
|
+
updated: unixTimestamp2000Schema.optional(),
|
|
148
135
|
});
|
|
149
|
-
|
|
150
|
-
|
|
136
|
+
export const macAddressSchema = stringSchema.regex(/^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/);
|
|
137
|
+
export const uuidSchema = stringSchema.uuid();
|
|
@@ -1,12 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.JoiValidationError = void 0;
|
|
4
|
-
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
5
|
-
class JoiValidationError extends js_lib_1.AppError {
|
|
1
|
+
import { AppError } from '@naturalcycles/js-lib';
|
|
2
|
+
export class JoiValidationError extends AppError {
|
|
6
3
|
constructor(message, data) {
|
|
7
4
|
super(message, data, {
|
|
8
5
|
name: 'JoiValidationError',
|
|
9
6
|
});
|
|
10
7
|
}
|
|
11
8
|
}
|
|
12
|
-
exports.JoiValidationError = JoiValidationError;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { AnySchema, ValidationOptions } from 'joi';
|
|
2
|
-
import { JoiValidationError } from './joi.validation.error';
|
|
2
|
+
import { JoiValidationError } from './joi.validation.error.js';
|
|
3
3
|
export interface JoiValidationResult<T = any> {
|
|
4
4
|
value: T;
|
|
5
5
|
error?: JoiValidationError;
|