@naturalcycles/js-lib 15.46.1 → 15.47.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/promise/pProps.js +1 -1
- package/dist/promise/pTimeout.js +1 -1
- package/dist/semver.js +1 -0
- package/package.json +1 -2
- package/src/promise/pProps.ts +1 -1
- package/src/promise/pTimeout.ts +1 -1
- package/src/semver.ts +1 -0
- package/src/types.ts +4 -4
- package/src/zod/zod.shared.schemas.ts +1 -1
- package/dist/json-schema/from-data/generateJsonSchemaFromData.d.ts +0 -8
- package/dist/json-schema/from-data/generateJsonSchemaFromData.js +0 -87
- package/dist/json-schema/index.d.ts +0 -1
- package/dist/json-schema/index.js +0 -1
- package/dist/json-schema/jsonSchema.cnst.d.ts +0 -2
- package/dist/json-schema/jsonSchema.cnst.js +0 -38
- package/dist/json-schema/jsonSchema.model.d.ts +0 -121
- package/dist/json-schema/jsonSchema.model.js +0 -1
- package/dist/json-schema/jsonSchema.util.d.ts +0 -8
- package/dist/json-schema/jsonSchema.util.js +0 -27
- package/dist/json-schema/jsonSchemaBuilder.d.ts +0 -159
- package/dist/json-schema/jsonSchemaBuilder.js +0 -412
- package/dist/json-schema/jsonSchemas.d.ts +0 -2
- package/dist/json-schema/jsonSchemas.js +0 -6
- package/src/json-schema/from-data/generateJsonSchemaFromData.ts +0 -121
- package/src/json-schema/index.ts +0 -1
- package/src/json-schema/jsonSchema.cnst.ts +0 -52
- package/src/json-schema/jsonSchema.model.ts +0 -172
- package/src/json-schema/jsonSchema.util.ts +0 -36
- package/src/json-schema/jsonSchemaBuilder.ts +0 -590
- package/src/json-schema/jsonSchemas.ts +0 -8
package/dist/promise/pProps.js
CHANGED
|
@@ -15,6 +15,6 @@
|
|
|
15
15
|
export async function pProps(input) {
|
|
16
16
|
const keys = Object.keys(input);
|
|
17
17
|
// `as any` here is added to make it compile when `noUncheckedIndexedAccess` is false
|
|
18
|
-
//
|
|
18
|
+
// oxlint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
19
19
|
return Object.fromEntries((await Promise.all(Object.values(input))).map((v, i) => [keys[i], v]));
|
|
20
20
|
}
|
package/dist/promise/pTimeout.js
CHANGED
|
@@ -44,7 +44,7 @@ export async function pTimeout(fn, opt) {
|
|
|
44
44
|
_typeCast(err);
|
|
45
45
|
// keep original stack
|
|
46
46
|
err.stack = fakeError.stack.replace('Error: TimeoutError', err.name + ': ' + err.message);
|
|
47
|
-
//
|
|
47
|
+
// oxlint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
|
|
48
48
|
reject(_errorDataAppend(err, opt.errorData));
|
|
49
49
|
}
|
|
50
50
|
return;
|
package/dist/semver.js
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naturalcycles/js-lib",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "15.
|
|
4
|
+
"version": "15.47.0",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"tslib": "^2",
|
|
7
7
|
"undici": "^7",
|
|
@@ -29,7 +29,6 @@
|
|
|
29
29
|
"./error": "./dist/error/index.js",
|
|
30
30
|
"./error/*.js": "./dist/error/*.js",
|
|
31
31
|
"./http": "./dist/http/index.js",
|
|
32
|
-
"./json-schema": "./dist/json-schema/index.js",
|
|
33
32
|
"./log": "./dist/log/commonLogger.js",
|
|
34
33
|
"./math": "./dist/math/index.js",
|
|
35
34
|
"./math/*.js": "./dist/math/*.js",
|
package/src/promise/pProps.ts
CHANGED
|
@@ -17,7 +17,7 @@ export async function pProps<T>(input: { [K in keyof T]: T[K] | Promise<T[K]> })
|
|
|
17
17
|
}> {
|
|
18
18
|
const keys = Object.keys(input)
|
|
19
19
|
// `as any` here is added to make it compile when `noUncheckedIndexedAccess` is false
|
|
20
|
-
//
|
|
20
|
+
// oxlint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
21
21
|
return Object.fromEntries(
|
|
22
22
|
(await Promise.all(Object.values(input))).map((v, i) => [keys[i], v]),
|
|
23
23
|
) as any
|
package/src/promise/pTimeout.ts
CHANGED
|
@@ -88,7 +88,7 @@ export async function pTimeout<T>(fn: AnyAsyncFunction<T>, opt: PTimeoutOptions)
|
|
|
88
88
|
_typeCast<Error>(err)
|
|
89
89
|
// keep original stack
|
|
90
90
|
err.stack = fakeError.stack!.replace('Error: TimeoutError', err.name + ': ' + err.message)
|
|
91
|
-
//
|
|
91
|
+
// oxlint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
|
|
92
92
|
reject(_errorDataAppend(err, opt.errorData))
|
|
93
93
|
}
|
|
94
94
|
return
|
package/src/semver.ts
CHANGED
package/src/types.ts
CHANGED
|
@@ -34,7 +34,7 @@ export type AnyEnum = NumberEnum
|
|
|
34
34
|
export type NumberEnum = Record<string, number | string>
|
|
35
35
|
export type StringEnum = Record<string, string>
|
|
36
36
|
|
|
37
|
-
//
|
|
37
|
+
// oxlint-disable-next-line @typescript-eslint/consistent-type-definitions
|
|
38
38
|
export type CreatedUpdated = {
|
|
39
39
|
created: UnixTimestamp
|
|
40
40
|
updated: UnixTimestamp
|
|
@@ -44,12 +44,12 @@ export interface CreatedUpdatedId extends CreatedUpdated {
|
|
|
44
44
|
id: string
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
//
|
|
47
|
+
// oxlint-disable-next-line @typescript-eslint/consistent-type-definitions
|
|
48
48
|
export type ObjectWithId = {
|
|
49
49
|
id: string
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
//
|
|
52
|
+
// oxlint-disable-next-line @typescript-eslint/consistent-type-definitions
|
|
53
53
|
export type PartialObjectWithId = {
|
|
54
54
|
id?: string
|
|
55
55
|
}
|
|
@@ -61,7 +61,7 @@ export interface AnyObjectWithId extends AnyObject, ObjectWithId {}
|
|
|
61
61
|
/**
|
|
62
62
|
* Base interface for any Entity that was saved to DB.
|
|
63
63
|
*/
|
|
64
|
-
//
|
|
64
|
+
// oxlint-disable-next-line @typescript-eslint/consistent-type-definitions
|
|
65
65
|
export type BaseDBEntity = {
|
|
66
66
|
id: string
|
|
67
67
|
|
|
@@ -108,7 +108,7 @@ const baseDBEntitySchema = z.object({
|
|
|
108
108
|
updated: unixTimestamp2000(),
|
|
109
109
|
})
|
|
110
110
|
|
|
111
|
-
//
|
|
111
|
+
// oxlint-disable-next-line @typescript-eslint/consistent-type-definitions
|
|
112
112
|
type BaseDBEntityZodShape = {
|
|
113
113
|
id: ZodString
|
|
114
114
|
created: ZodBrandedInt<UnixTimestamp>
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { type AnyObject } from '../../types.js';
|
|
2
|
-
import type { JsonSchemaObject } from '../jsonSchema.model.js';
|
|
3
|
-
/**
|
|
4
|
-
* Each row must be an object (current limitation).
|
|
5
|
-
*
|
|
6
|
-
* `additionalProperties` is set to `true`, cause it's safer.
|
|
7
|
-
*/
|
|
8
|
-
export declare function generateJsonSchemaFromData<T extends AnyObject = AnyObject>(rows: AnyObject[]): JsonSchemaObject<T>;
|
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
import { _uniq } from '../../array/index.js';
|
|
2
|
-
import { _stringMapEntries } from '../../types.js';
|
|
3
|
-
/**
|
|
4
|
-
* Each row must be an object (current limitation).
|
|
5
|
-
*
|
|
6
|
-
* `additionalProperties` is set to `true`, cause it's safer.
|
|
7
|
-
*/
|
|
8
|
-
export function generateJsonSchemaFromData(rows) {
|
|
9
|
-
return objectToJsonSchema(rows);
|
|
10
|
-
}
|
|
11
|
-
function objectToJsonSchema(rows) {
|
|
12
|
-
const typesByKey = {};
|
|
13
|
-
rows.forEach(r => {
|
|
14
|
-
Object.keys(r).forEach(key => {
|
|
15
|
-
typesByKey[key] ||= new Set();
|
|
16
|
-
typesByKey[key].add(getTypeOfValue(r[key]));
|
|
17
|
-
});
|
|
18
|
-
});
|
|
19
|
-
const s = {
|
|
20
|
-
type: 'object',
|
|
21
|
-
properties: {},
|
|
22
|
-
required: [],
|
|
23
|
-
additionalProperties: true,
|
|
24
|
-
};
|
|
25
|
-
_stringMapEntries(typesByKey).forEach(([key, types]) => {
|
|
26
|
-
const schema = mergeTypes([...types], rows.map(r => r[key]));
|
|
27
|
-
if (!schema)
|
|
28
|
-
return;
|
|
29
|
-
s.properties[key] = schema;
|
|
30
|
-
});
|
|
31
|
-
// console.log(typesByKey)
|
|
32
|
-
return s;
|
|
33
|
-
}
|
|
34
|
-
function mergeTypes(types, samples) {
|
|
35
|
-
// skip "undefined" types
|
|
36
|
-
types = types.filter(t => t !== 'undefined');
|
|
37
|
-
if (!types.length)
|
|
38
|
-
return undefined;
|
|
39
|
-
if (types.length > 1) {
|
|
40
|
-
// oneOf
|
|
41
|
-
const s = {
|
|
42
|
-
oneOf: types.map(type => mergeTypes([type], samples)),
|
|
43
|
-
};
|
|
44
|
-
return s;
|
|
45
|
-
}
|
|
46
|
-
const type = types[0];
|
|
47
|
-
if (type === 'null') {
|
|
48
|
-
return {
|
|
49
|
-
type: 'null',
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
if (type === 'boolean') {
|
|
53
|
-
return {
|
|
54
|
-
type: 'boolean',
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
if (type === 'string') {
|
|
58
|
-
return {
|
|
59
|
-
type: 'string',
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
if (type === 'number') {
|
|
63
|
-
return {
|
|
64
|
-
type: 'number',
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
if (type === 'object') {
|
|
68
|
-
return objectToJsonSchema(samples.filter((r) => r && typeof r === 'object'));
|
|
69
|
-
}
|
|
70
|
-
if (type === 'array') {
|
|
71
|
-
// possible feature: detect if it's a tuple
|
|
72
|
-
// currently assume no-tuple
|
|
73
|
-
const items = samples.filter(r => Array.isArray(r)).flat();
|
|
74
|
-
const itemTypes = _uniq(items.map(i => getTypeOfValue(i)));
|
|
75
|
-
return {
|
|
76
|
-
type: 'array',
|
|
77
|
-
items: mergeTypes(itemTypes, items),
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
function getTypeOfValue(v) {
|
|
82
|
-
if (v === null)
|
|
83
|
-
return 'null';
|
|
84
|
-
if (Array.isArray(v))
|
|
85
|
-
return 'array';
|
|
86
|
-
return typeof v;
|
|
87
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './from-data/generateJsonSchemaFromData.js';
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './from-data/generateJsonSchemaFromData.js';
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import type { JsonSchema, JsonSchemaAny, JsonSchemaNumber, JsonSchemaObject, JsonSchemaString } from './jsonSchema.model.js';
|
|
2
|
-
export declare const JSON_SCHEMA_ORDER: (keyof JsonSchema | keyof JsonSchemaAny | keyof JsonSchemaObject | keyof JsonSchemaString | keyof JsonSchemaNumber)[];
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
export const JSON_SCHEMA_ORDER = [
|
|
2
|
-
'$schema',
|
|
3
|
-
'$id',
|
|
4
|
-
'title',
|
|
5
|
-
'description',
|
|
6
|
-
'deprecated',
|
|
7
|
-
'readOnly',
|
|
8
|
-
'writeOnly',
|
|
9
|
-
'type',
|
|
10
|
-
'default',
|
|
11
|
-
// Object,
|
|
12
|
-
'properties',
|
|
13
|
-
'required',
|
|
14
|
-
'minProperties',
|
|
15
|
-
'maxProperties',
|
|
16
|
-
'patternProperties',
|
|
17
|
-
'propertyNames',
|
|
18
|
-
// Array
|
|
19
|
-
'properties',
|
|
20
|
-
'required',
|
|
21
|
-
'minProperties',
|
|
22
|
-
'maxProperties',
|
|
23
|
-
'patternProperties',
|
|
24
|
-
'propertyNames',
|
|
25
|
-
// String
|
|
26
|
-
'pattern',
|
|
27
|
-
'minLength',
|
|
28
|
-
'maxLength',
|
|
29
|
-
'format',
|
|
30
|
-
'transform',
|
|
31
|
-
// Number
|
|
32
|
-
'format',
|
|
33
|
-
'multipleOf',
|
|
34
|
-
'minimum',
|
|
35
|
-
'exclusiveMinimum',
|
|
36
|
-
'maximum',
|
|
37
|
-
'exclusiveMaximum',
|
|
38
|
-
];
|
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
import type { AnyObject, StringMap } from '../types.js';
|
|
2
|
-
export type JsonSchema<T = unknown> = JsonSchemaAny<T> | JsonSchemaOneOf<T> | JsonSchemaAllOf<T> | JsonSchemaAnyOf<T> | JsonSchemaNot<T> | JsonSchemaRef<T> | JsonSchemaConst<T> | JsonSchemaEnum<T> | JsonSchemaString | JsonSchemaNumber | JsonSchemaBoolean | JsonSchemaNull | JsonSchemaObject<T extends AnyObject ? T : AnyObject> | JsonSchemaArray<T> | JsonSchemaTuple<T>;
|
|
3
|
-
export interface JsonSchemaAny<T = unknown> {
|
|
4
|
-
$schema?: string;
|
|
5
|
-
$id?: string;
|
|
6
|
-
title?: string;
|
|
7
|
-
description?: string;
|
|
8
|
-
deprecated?: boolean;
|
|
9
|
-
readOnly?: boolean;
|
|
10
|
-
writeOnly?: boolean;
|
|
11
|
-
type?: string | string[];
|
|
12
|
-
default?: T;
|
|
13
|
-
if?: JsonSchema;
|
|
14
|
-
then?: JsonSchema;
|
|
15
|
-
else?: JsonSchema;
|
|
16
|
-
/**
|
|
17
|
-
* https://ajv.js.org/packages/ajv-keywords.html#instanceof
|
|
18
|
-
*
|
|
19
|
-
* Useful for Node.js Buffer, you can use it like:
|
|
20
|
-
* `instanceof: 'Buffer'`
|
|
21
|
-
*/
|
|
22
|
-
instanceof?: string | string[];
|
|
23
|
-
/**
|
|
24
|
-
* This is a temporary "intermediate AST" field that is used inside the parser.
|
|
25
|
-
* In the final schema this field will NOT be present.
|
|
26
|
-
*/
|
|
27
|
-
optionalField?: true;
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Union type
|
|
31
|
-
*/
|
|
32
|
-
export interface JsonSchemaOneOf<T = unknown> extends JsonSchemaAny<T> {
|
|
33
|
-
oneOf: JsonSchema[];
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Intersection type
|
|
37
|
-
*/
|
|
38
|
-
export interface JsonSchemaAllOf<T = unknown> extends JsonSchemaAny<T> {
|
|
39
|
-
allOf: JsonSchema[];
|
|
40
|
-
}
|
|
41
|
-
export interface JsonSchemaAnyOf<T = unknown> extends JsonSchemaAny<T> {
|
|
42
|
-
anyOf: JsonSchema[];
|
|
43
|
-
}
|
|
44
|
-
export interface JsonSchemaNot<T = unknown> extends JsonSchemaAny<T> {
|
|
45
|
-
not: JsonSchema;
|
|
46
|
-
}
|
|
47
|
-
export interface JsonSchemaConst<T = unknown> extends JsonSchemaAny<T> {
|
|
48
|
-
const: T;
|
|
49
|
-
}
|
|
50
|
-
export interface JsonSchemaString<T extends string = string> extends JsonSchemaAny<T> {
|
|
51
|
-
type: 'string';
|
|
52
|
-
pattern?: string;
|
|
53
|
-
minLength?: number;
|
|
54
|
-
maxLength?: number;
|
|
55
|
-
format?: string;
|
|
56
|
-
contentMediaType?: string;
|
|
57
|
-
contentEncoding?: string;
|
|
58
|
-
/**
|
|
59
|
-
* https://ajv.js.org/packages/ajv-keywords.html#transform
|
|
60
|
-
*/
|
|
61
|
-
transform?: ('trim' | 'toLowerCase' | 'toUpperCase')[];
|
|
62
|
-
}
|
|
63
|
-
export interface JsonSchemaNumber<T extends number = number> extends JsonSchemaAny<T> {
|
|
64
|
-
type: 'number' | 'integer';
|
|
65
|
-
format?: string;
|
|
66
|
-
multipleOf?: number;
|
|
67
|
-
minimum?: number;
|
|
68
|
-
exclusiveMinimum?: number;
|
|
69
|
-
maximum?: number;
|
|
70
|
-
exclusiveMaximum?: number;
|
|
71
|
-
}
|
|
72
|
-
export interface JsonSchemaBoolean extends JsonSchemaAny<boolean> {
|
|
73
|
-
type: 'boolean';
|
|
74
|
-
}
|
|
75
|
-
export interface JsonSchemaNull extends JsonSchemaAny<null> {
|
|
76
|
-
type: 'null';
|
|
77
|
-
}
|
|
78
|
-
export interface JsonSchemaEnum<T = unknown> extends JsonSchemaAny<T> {
|
|
79
|
-
enum: T[];
|
|
80
|
-
}
|
|
81
|
-
export interface JsonSchemaRef<T = unknown> extends JsonSchemaAny<T> {
|
|
82
|
-
$ref: string;
|
|
83
|
-
}
|
|
84
|
-
export interface JsonSchemaRootObject<T extends AnyObject = AnyObject> extends JsonSchemaObject<T> {
|
|
85
|
-
$id: string;
|
|
86
|
-
}
|
|
87
|
-
export interface JsonSchemaObject<T extends AnyObject = AnyObject> extends JsonSchemaAny<T> {
|
|
88
|
-
type: 'object';
|
|
89
|
-
properties: {
|
|
90
|
-
[k in keyof T]: JsonSchema;
|
|
91
|
-
};
|
|
92
|
-
required: (keyof T)[];
|
|
93
|
-
additionalProperties: boolean;
|
|
94
|
-
minProperties?: number;
|
|
95
|
-
maxProperties?: number;
|
|
96
|
-
patternProperties?: StringMap<JsonSchema>;
|
|
97
|
-
propertyNames?: JsonSchema;
|
|
98
|
-
/**
|
|
99
|
-
* @example
|
|
100
|
-
*
|
|
101
|
-
* dependentRequired: {
|
|
102
|
-
* credit_card: ['billing_address']
|
|
103
|
-
* }
|
|
104
|
-
*/
|
|
105
|
-
dependentRequired?: StringMap<string[]>;
|
|
106
|
-
dependentSchemas?: StringMap<JsonSchema>;
|
|
107
|
-
dependencies?: StringMap<string[]>;
|
|
108
|
-
}
|
|
109
|
-
export interface JsonSchemaArray<ITEM = unknown> extends JsonSchemaAny<ITEM[]> {
|
|
110
|
-
type: 'array';
|
|
111
|
-
items: JsonSchema<ITEM>;
|
|
112
|
-
minItems?: number;
|
|
113
|
-
maxItems?: number;
|
|
114
|
-
uniqueItems?: boolean;
|
|
115
|
-
}
|
|
116
|
-
export interface JsonSchemaTuple<T = unknown> extends JsonSchemaAny<T> {
|
|
117
|
-
type: 'array';
|
|
118
|
-
items: JsonSchema[];
|
|
119
|
-
minItems: number;
|
|
120
|
-
maxItems: number;
|
|
121
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { AnyObject } from '../types.js';
|
|
2
|
-
import type { JsonSchemaObject } from './jsonSchema.model.js';
|
|
3
|
-
/**
|
|
4
|
-
* Merges s2 into s1 (mutates s1) and returns s1.
|
|
5
|
-
* Does not mutate s2.
|
|
6
|
-
* API similar to Object.assign(s1, s2)
|
|
7
|
-
*/
|
|
8
|
-
export declare function mergeJsonSchemaObjects<T1 extends AnyObject, T2 extends AnyObject>(s1: JsonSchemaObject<T1>, s2: JsonSchemaObject<T2>): JsonSchemaObject<T1 & T2>;
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { _uniq } from '../array/array.util.js';
|
|
2
|
-
import { _filterNullishValues } from '../object/object.util.js';
|
|
3
|
-
/**
|
|
4
|
-
* Merges s2 into s1 (mutates s1) and returns s1.
|
|
5
|
-
* Does not mutate s2.
|
|
6
|
-
* API similar to Object.assign(s1, s2)
|
|
7
|
-
*/
|
|
8
|
-
export function mergeJsonSchemaObjects(s1, s2) {
|
|
9
|
-
// Merge `properties`
|
|
10
|
-
Object.entries(s2.properties).forEach(([k, v]) => {
|
|
11
|
-
;
|
|
12
|
-
s1.properties[k] = v;
|
|
13
|
-
});
|
|
14
|
-
// Merge `patternProperties`
|
|
15
|
-
Object.entries(s2.patternProperties || {}).forEach(([k, v]) => {
|
|
16
|
-
;
|
|
17
|
-
s1.patternProperties[k] = v;
|
|
18
|
-
});
|
|
19
|
-
s1.propertyNames = s2.propertyNames || s1.propertyNames;
|
|
20
|
-
s1.minProperties = s2.minProperties ?? s1.minProperties;
|
|
21
|
-
s1.maxProperties = s2.maxProperties ?? s1.maxProperties;
|
|
22
|
-
// Merge `required`
|
|
23
|
-
s1.required.push(...s2.required);
|
|
24
|
-
s1.required = _uniq(s1.required).sort();
|
|
25
|
-
// `additionalProperties` remains the same
|
|
26
|
-
return _filterNullishValues(s1, { mutate: true });
|
|
27
|
-
}
|
|
@@ -1,159 +0,0 @@
|
|
|
1
|
-
import { type AnyObject, type BaseDBEntity, type IsoDate, type IsoDateTime, type NumberEnum, type StringEnum, type UnixTimestamp, type UnixTimestampMillis } from '../types.js';
|
|
2
|
-
import type { JsonSchema, JsonSchemaAllOf, JsonSchemaAny, JsonSchemaArray, JsonSchemaBoolean, JsonSchemaConst, JsonSchemaEnum, JsonSchemaNull, JsonSchemaNumber, JsonSchemaObject, JsonSchemaOneOf, JsonSchemaRef, JsonSchemaString, JsonSchemaTuple } from './jsonSchema.model.js';
|
|
3
|
-
export interface JsonSchemaBuilder<T = unknown> {
|
|
4
|
-
build: () => JsonSchema<T>;
|
|
5
|
-
}
|
|
6
|
-
/**
|
|
7
|
-
* Fluent (chainable) API to manually create Json Schemas.
|
|
8
|
-
* Inspired by Joi and Zod.
|
|
9
|
-
*/
|
|
10
|
-
export declare const j: {
|
|
11
|
-
any<T = unknown>(): JsonSchemaAnyBuilder<T, JsonSchemaAny<T>, false>;
|
|
12
|
-
const<T extends string | number | boolean | null>(value: T): JsonSchemaAnyBuilder<T, JsonSchemaConst<T>, false>;
|
|
13
|
-
null(): JsonSchemaAnyBuilder<null, JsonSchemaNull, false>;
|
|
14
|
-
ref<T = unknown>($ref: string): JsonSchemaAnyBuilder<T, JsonSchemaRef<T>, false>;
|
|
15
|
-
enum<const T extends readonly (string | number | boolean | null)[] | StringEnum | NumberEnum>(input: T): JsonSchemaAnyBuilder<T extends readonly (infer U)[] ? U : T extends StringEnum ? T[keyof T] : T extends NumberEnum ? T[keyof T] : never, JsonSchemaEnum<any>, false>;
|
|
16
|
-
boolean(): JsonSchemaAnyBuilder<boolean, JsonSchemaBoolean, false>;
|
|
17
|
-
buffer(): JsonSchemaAnyBuilder<Buffer<ArrayBufferLike>, JsonSchemaAny<Buffer<ArrayBufferLike>>, false>;
|
|
18
|
-
number<T extends number = number>(): JsonSchemaNumberBuilder<T, false>;
|
|
19
|
-
integer<T extends number = number>(): JsonSchemaNumberBuilder<T, false>;
|
|
20
|
-
string<T extends string = string>(): JsonSchemaStringBuilder<T, false>;
|
|
21
|
-
object: typeof object;
|
|
22
|
-
dbEntity<P extends Record<string, JsonSchemaAnyBuilder<any, any, any>>>(props: P): JsonSchemaObjectBuilder<BaseDBEntity & { [K in keyof P]: P[K] extends JsonSchemaAnyBuilder<infer U, any, any> ? U : never; }>;
|
|
23
|
-
rootObject<T extends AnyObject>(props: { [K in keyof T]: JsonSchemaAnyBuilder<T[K]>; }): JsonSchemaObjectBuilder<T, false>;
|
|
24
|
-
array<T extends JsonSchemaAnyBuilder<any>>(itemSchema: T): JsonSchemaArrayBuilder<T["infer"], false>;
|
|
25
|
-
tuple<T extends any[] = unknown[]>(items: JsonSchemaAnyBuilder[]): JsonSchemaTupleBuilder<T>;
|
|
26
|
-
oneOf<Builders extends JsonSchemaAnyBuilder<any, any, any>[]>(items: [...Builders]): JsonSchemaAnyBuilder<Builders[number] extends JsonSchemaAnyBuilder<infer U, any, any> ? U : never, JsonSchemaOneOf<Builders[number] extends JsonSchemaAnyBuilder<infer U, any, any> ? U : never>>;
|
|
27
|
-
allOf<T = unknown>(items: JsonSchemaAnyBuilder[]): JsonSchemaAnyBuilder<T, JsonSchemaAllOf<T>, false>;
|
|
28
|
-
};
|
|
29
|
-
export declare class JsonSchemaAnyBuilder<T = unknown, SCHEMA_TYPE extends JsonSchema<T> = JsonSchema<T>, Opt extends boolean = false> implements JsonSchemaBuilder<T> {
|
|
30
|
-
protected schema: SCHEMA_TYPE;
|
|
31
|
-
constructor(schema: SCHEMA_TYPE);
|
|
32
|
-
/**
|
|
33
|
-
* Used in ObjectBuilder to access schema.optionalProperty
|
|
34
|
-
*/
|
|
35
|
-
getSchema(): SCHEMA_TYPE;
|
|
36
|
-
$schema($schema: string): this;
|
|
37
|
-
$schemaDraft7(): this;
|
|
38
|
-
$id($id: string): this;
|
|
39
|
-
title(title: string): this;
|
|
40
|
-
description(description: string): this;
|
|
41
|
-
deprecated(deprecated?: boolean): this;
|
|
42
|
-
type(type: string): this;
|
|
43
|
-
default(v: any): this;
|
|
44
|
-
instanceof(of: string): this;
|
|
45
|
-
optional(): JsonSchemaAnyBuilder<T | undefined, JsonSchema<T | undefined>, true>;
|
|
46
|
-
optional(optional: true): JsonSchemaAnyBuilder<T | undefined, JsonSchema<T | undefined>, true>;
|
|
47
|
-
optional(optional: false): JsonSchemaAnyBuilder<Exclude<T, undefined>, JsonSchema<Exclude<T, undefined>>, false>;
|
|
48
|
-
nullable(): JsonSchemaAnyBuilder<T | null, JsonSchema<T | null>, Opt>;
|
|
49
|
-
/**
|
|
50
|
-
* Produces a "clean schema object" without methods.
|
|
51
|
-
* Same as if it would be JSON.stringified.
|
|
52
|
-
*/
|
|
53
|
-
build(): SCHEMA_TYPE;
|
|
54
|
-
clone(): JsonSchemaAnyBuilder<T, SCHEMA_TYPE, Opt>;
|
|
55
|
-
/**
|
|
56
|
-
* @experimental
|
|
57
|
-
*/
|
|
58
|
-
infer: T;
|
|
59
|
-
}
|
|
60
|
-
export declare class JsonSchemaNumberBuilder<T extends number = number, Opt extends boolean = false> extends JsonSchemaAnyBuilder<T, JsonSchemaNumber<T>, Opt> {
|
|
61
|
-
constructor();
|
|
62
|
-
integer(): this;
|
|
63
|
-
multipleOf(multipleOf: number): this;
|
|
64
|
-
min(minimum: number): this;
|
|
65
|
-
exclusiveMin(exclusiveMinimum: number): this;
|
|
66
|
-
max(maximum: number): this;
|
|
67
|
-
exclusiveMax(exclusiveMaximum: number): this;
|
|
68
|
-
/**
|
|
69
|
-
* Both ranges are inclusive.
|
|
70
|
-
*/
|
|
71
|
-
range(minimum: number, maximum: number): this;
|
|
72
|
-
format(format: string): this;
|
|
73
|
-
int32: () => this;
|
|
74
|
-
int64: () => this;
|
|
75
|
-
float: () => this;
|
|
76
|
-
double: () => this;
|
|
77
|
-
unixTimestamp: () => JsonSchemaNumberBuilder<UnixTimestamp>;
|
|
78
|
-
unixTimestamp2000: () => JsonSchemaNumberBuilder<UnixTimestamp>;
|
|
79
|
-
unixTimestampMillis: () => JsonSchemaNumberBuilder<UnixTimestampMillis>;
|
|
80
|
-
unixTimestampMillis2000: () => JsonSchemaNumberBuilder<UnixTimestampMillis>;
|
|
81
|
-
utcOffset: () => this;
|
|
82
|
-
utcOffsetHours: () => this;
|
|
83
|
-
branded<B extends number>(): JsonSchemaNumberBuilder<B>;
|
|
84
|
-
}
|
|
85
|
-
export declare class JsonSchemaStringBuilder<T extends string = string, Opt extends boolean = false> extends JsonSchemaAnyBuilder<T, JsonSchemaString<T>, Opt> {
|
|
86
|
-
constructor();
|
|
87
|
-
regex(pattern: RegExp): this;
|
|
88
|
-
pattern(pattern: string): this;
|
|
89
|
-
min(minLength: number): this;
|
|
90
|
-
max(maxLength: number): this;
|
|
91
|
-
length(minLength: number, maxLength: number): this;
|
|
92
|
-
format(format: string): this;
|
|
93
|
-
email: () => this;
|
|
94
|
-
url: () => this;
|
|
95
|
-
ipv4: () => this;
|
|
96
|
-
ipv6: () => this;
|
|
97
|
-
password: () => this;
|
|
98
|
-
id: () => this;
|
|
99
|
-
slug: () => this;
|
|
100
|
-
semVer: () => this;
|
|
101
|
-
languageTag: () => this;
|
|
102
|
-
countryCode: () => this;
|
|
103
|
-
currency: () => this;
|
|
104
|
-
trim(trim?: boolean): this;
|
|
105
|
-
toLowerCase(toLowerCase?: boolean): this;
|
|
106
|
-
toUpperCase(toUpperCase?: boolean): this;
|
|
107
|
-
truncate(toLength: number): this;
|
|
108
|
-
branded<B extends string>(): JsonSchemaStringBuilder<B>;
|
|
109
|
-
/**
|
|
110
|
-
* Accepts only the `YYYY-MM-DD` shape from all ISO 8601 variants.
|
|
111
|
-
*/
|
|
112
|
-
isoDate(): JsonSchemaStringBuilder<IsoDate>;
|
|
113
|
-
/**
|
|
114
|
-
* Accepts strings that start with the `YYYY-MM-DDTHH:MM:SS` shape
|
|
115
|
-
* and optionally end with either a `Z` or a `+/-hh:mm` timezone part.
|
|
116
|
-
*/
|
|
117
|
-
isoDateTime(): JsonSchemaStringBuilder<IsoDateTime>;
|
|
118
|
-
jwt(): this;
|
|
119
|
-
}
|
|
120
|
-
export declare class JsonSchemaObjectBuilder<T extends AnyObject, Opt extends boolean = false> extends JsonSchemaAnyBuilder<T, JsonSchemaObject<T>, Opt> {
|
|
121
|
-
constructor();
|
|
122
|
-
addProperties(props?: {
|
|
123
|
-
[k in keyof T]: JsonSchemaBuilder<T[k]>;
|
|
124
|
-
}): this;
|
|
125
|
-
/**
|
|
126
|
-
* Ensures `required` is always sorted and _uniq
|
|
127
|
-
*/
|
|
128
|
-
required(required: (keyof T)[]): this;
|
|
129
|
-
addRequired(required: (keyof T)[]): this;
|
|
130
|
-
minProps(minProperties: number): this;
|
|
131
|
-
maxProps(maxProperties: number): this;
|
|
132
|
-
additionalProps(additionalProperties: boolean): this;
|
|
133
|
-
baseDBEntity(): JsonSchemaObjectBuilder<T & BaseDBEntity>;
|
|
134
|
-
extend<T2 extends AnyObject>(s2: JsonSchemaObjectBuilder<T2>): JsonSchemaObjectBuilder<T & T2 extends infer O ? {
|
|
135
|
-
[K in keyof O]: O[K];
|
|
136
|
-
} : never>;
|
|
137
|
-
}
|
|
138
|
-
export declare class JsonSchemaArrayBuilder<ITEM, Opt extends boolean = false> extends JsonSchemaAnyBuilder<ITEM[], JsonSchemaArray<ITEM>, Opt> {
|
|
139
|
-
constructor(itemsSchema: JsonSchemaBuilder<ITEM>);
|
|
140
|
-
min(minItems: number): this;
|
|
141
|
-
max(maxItems: number): this;
|
|
142
|
-
unique(uniqueItems?: boolean): this;
|
|
143
|
-
}
|
|
144
|
-
export declare class JsonSchemaTupleBuilder<T extends any[]> extends JsonSchemaAnyBuilder<T, JsonSchemaTuple<T>> {
|
|
145
|
-
constructor(items: JsonSchemaBuilder[]);
|
|
146
|
-
}
|
|
147
|
-
declare function object<P extends Record<string, JsonSchemaAnyBuilder<any, any, any>>>(props?: {
|
|
148
|
-
[K in keyof P]: P[K] & JsonSchemaAnyBuilder<any, any, any>;
|
|
149
|
-
}): JsonSchemaObjectBuilder<{
|
|
150
|
-
[K in keyof P as P[K] extends JsonSchemaAnyBuilder<any, any, infer Opt> ? Opt extends true ? never : K : never]: P[K] extends JsonSchemaAnyBuilder<infer U, any, any> ? U : never;
|
|
151
|
-
} & {
|
|
152
|
-
[K in keyof P as P[K] extends JsonSchemaAnyBuilder<any, any, infer Opt> ? Opt extends true ? K : never : never]?: P[K] extends JsonSchemaAnyBuilder<infer U, any, any> ? U : never;
|
|
153
|
-
} extends infer O ? {
|
|
154
|
-
[K in keyof O]: O[K];
|
|
155
|
-
} : never>;
|
|
156
|
-
declare function object<T extends AnyObject>(props?: {
|
|
157
|
-
[K in keyof T]: JsonSchemaAnyBuilder<T[K]>;
|
|
158
|
-
}): JsonSchemaObjectBuilder<T>;
|
|
159
|
-
export {};
|