@bedrockio/yada 1.0.13 → 1.0.14
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/README.md +3 -3
- package/dist/cjs/Schema.js +47 -24
- package/dist/cjs/TypeSchema.js +1 -2
- package/dist/cjs/array.js +12 -2
- package/dist/cjs/boolean.js +5 -2
- package/dist/cjs/date.js +29 -2
- package/dist/cjs/index.js +89 -7
- package/dist/cjs/localization.js +31 -15
- package/dist/cjs/number.js +19 -8
- package/dist/cjs/object.js +14 -2
- package/dist/cjs/string.js +73 -5
- package/dist/cjs/utils.js +1 -15
- package/package.json +8 -5
- package/src/Schema.js +39 -20
- package/src/array.js +9 -0
- package/src/boolean.js +3 -0
- package/src/date.js +22 -0
- package/src/index.js +40 -7
- package/src/localization.js +28 -14
- package/src/number.js +15 -6
- package/src/object.js +10 -0
- package/src/string.js +58 -1
- package/src/utils.js +0 -14
- package/types/Schema.d.ts +49 -0
- package/types/Schema.d.ts.map +1 -0
- package/types/TypeSchema.d.ts +7 -0
- package/types/TypeSchema.d.ts.map +1 -0
- package/types/array.d.ts +18 -0
- package/types/array.d.ts.map +1 -0
- package/types/boolean.d.ts +7 -0
- package/types/boolean.d.ts.map +1 -0
- package/types/date.d.ts +31 -0
- package/types/date.d.ts.map +1 -0
- package/types/errors.d.ts +54 -0
- package/types/errors.d.ts.map +1 -0
- package/types/index.d.ts +39 -0
- package/types/index.d.ts.map +1 -0
- package/types/localization.d.ts +19 -0
- package/types/localization.d.ts.map +1 -0
- package/types/messages.d.ts +2 -0
- package/types/messages.d.ts.map +1 -0
- package/types/number.d.ts +21 -0
- package/types/number.d.ts.map +1 -0
- package/types/object.d.ts +17 -0
- package/types/object.d.ts.map +1 -0
- package/types/password.d.ts +13 -0
- package/types/password.d.ts.map +1 -0
- package/types/string.d.ts +115 -0
- package/types/string.d.ts.map +1 -0
- package/types/utils.d.ts +4 -0
- package/types/utils.d.ts.map +1 -0
- package/.eslintignore +0 -1
- package/test/all.test.js +0 -1989
package/dist/cjs/utils.js
CHANGED
|
@@ -15,25 +15,11 @@ Object.defineProperty(exports, "isSchemaError", {
|
|
|
15
15
|
return _errors.isSchemaError;
|
|
16
16
|
}
|
|
17
17
|
});
|
|
18
|
-
exports.wrapAny = wrapAny;
|
|
19
|
-
exports.wrapArgs = wrapArgs;
|
|
20
18
|
exports.wrapSchema = wrapSchema;
|
|
21
|
-
var _Schema =
|
|
19
|
+
var _Schema = require("./Schema");
|
|
22
20
|
var _errors = require("./errors");
|
|
23
|
-
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
24
|
-
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
25
21
|
function wrapSchema(Class) {
|
|
26
22
|
return (...args) => {
|
|
27
23
|
return new Class(...args);
|
|
28
24
|
};
|
|
29
|
-
}
|
|
30
|
-
function wrapArgs(name) {
|
|
31
|
-
return (...args) => {
|
|
32
|
-
return new _Schema.default()[name](...args);
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
function wrapAny() {
|
|
36
|
-
return () => {
|
|
37
|
-
return new _Schema.default();
|
|
38
|
-
};
|
|
39
25
|
}
|
package/package.json
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bedrockio/yada",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.14",
|
|
4
4
|
"description": "Validation library inspired by Joi.",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "jest",
|
|
7
7
|
"lint": "eslint",
|
|
8
8
|
"build": "scripts/build",
|
|
9
|
-
"
|
|
9
|
+
"types": "tsc",
|
|
10
|
+
"prepublishOnly": "yarn build && yarn types"
|
|
10
11
|
},
|
|
11
12
|
"type": "module",
|
|
12
|
-
"main": "dist/cjs/index.js",
|
|
13
|
-
"module": "src/index.js",
|
|
13
|
+
"main": "./dist/cjs/index.js",
|
|
14
|
+
"module": "./src/index.js",
|
|
15
|
+
"types": "./types/index.d.ts",
|
|
14
16
|
"repository": "https://github.com/bedrockio/yada",
|
|
15
17
|
"author": "Andrew Plummer <plummer.andrew@gmail.com>",
|
|
16
18
|
"license": "MIT",
|
|
@@ -26,7 +28,8 @@
|
|
|
26
28
|
"eslint": "^8.26.0",
|
|
27
29
|
"eslint-plugin-bedrock": "^1.0.17",
|
|
28
30
|
"jest": "^29.2.2",
|
|
29
|
-
"prettier": "^2.7.1"
|
|
31
|
+
"prettier": "^2.7.1",
|
|
32
|
+
"typescript": "^4.9.5"
|
|
30
33
|
},
|
|
31
34
|
"exports": {
|
|
32
35
|
".": {
|
package/src/Schema.js
CHANGED
|
@@ -9,6 +9,10 @@ import {
|
|
|
9
9
|
const INITIAL_TYPES = ['default', 'required', 'type', 'transform'];
|
|
10
10
|
const REQUIRED_TYPES = ['default', 'required'];
|
|
11
11
|
|
|
12
|
+
/**
|
|
13
|
+
* @typedef {[fn: Function] | [type: string, fn: Function]} CustomSignature
|
|
14
|
+
*/
|
|
15
|
+
|
|
12
16
|
export default class Schema {
|
|
13
17
|
constructor(meta = {}) {
|
|
14
18
|
this.assertions = [];
|
|
@@ -33,9 +37,18 @@ export default class Schema {
|
|
|
33
37
|
});
|
|
34
38
|
}
|
|
35
39
|
|
|
40
|
+
/**
|
|
41
|
+
* @param {CustomSignature} args
|
|
42
|
+
*/
|
|
36
43
|
custom(...args) {
|
|
37
|
-
|
|
38
|
-
|
|
44
|
+
let type, fn;
|
|
45
|
+
if (typeof args[0] === 'function') {
|
|
46
|
+
type = 'custom';
|
|
47
|
+
fn = args[0];
|
|
48
|
+
} else {
|
|
49
|
+
type = args[0];
|
|
50
|
+
fn = args[1];
|
|
51
|
+
}
|
|
39
52
|
if (!type) {
|
|
40
53
|
throw new Error('Assertion type required.');
|
|
41
54
|
} else if (!fn) {
|
|
@@ -120,6 +133,9 @@ export default class Schema {
|
|
|
120
133
|
return value;
|
|
121
134
|
}
|
|
122
135
|
|
|
136
|
+
/**
|
|
137
|
+
* @returns {this}
|
|
138
|
+
*/
|
|
123
139
|
clone(meta) {
|
|
124
140
|
const clone = Object.create(this.constructor.prototype);
|
|
125
141
|
clone.assertions = [...this.assertions];
|
|
@@ -127,12 +143,33 @@ export default class Schema {
|
|
|
127
143
|
return clone;
|
|
128
144
|
}
|
|
129
145
|
|
|
146
|
+
/**
|
|
147
|
+
* @returns {Schema}
|
|
148
|
+
*/
|
|
130
149
|
append(schema) {
|
|
131
150
|
const merged = this.clone(schema.meta);
|
|
132
151
|
merged.assertions = [...this.assertions, ...schema.assertions];
|
|
133
152
|
return merged;
|
|
134
153
|
}
|
|
135
154
|
|
|
155
|
+
toOpenApi(extra) {
|
|
156
|
+
const { required, format, tags, default: defaultValue } = this.meta;
|
|
157
|
+
return {
|
|
158
|
+
...(required && {
|
|
159
|
+
required: true,
|
|
160
|
+
}),
|
|
161
|
+
...(defaultValue && {
|
|
162
|
+
default: defaultValue,
|
|
163
|
+
}),
|
|
164
|
+
...(format && {
|
|
165
|
+
format,
|
|
166
|
+
}),
|
|
167
|
+
...this.enumToOpenApi(),
|
|
168
|
+
...tags,
|
|
169
|
+
...extra,
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
136
173
|
// Private
|
|
137
174
|
|
|
138
175
|
assertEnum(set, allow) {
|
|
@@ -210,24 +247,6 @@ export default class Schema {
|
|
|
210
247
|
}
|
|
211
248
|
}
|
|
212
249
|
|
|
213
|
-
toOpenApi(extra) {
|
|
214
|
-
const { required, format, tags, default: defaultValue } = this.meta;
|
|
215
|
-
return {
|
|
216
|
-
...(required && {
|
|
217
|
-
required: true,
|
|
218
|
-
}),
|
|
219
|
-
...(defaultValue && {
|
|
220
|
-
default: defaultValue,
|
|
221
|
-
}),
|
|
222
|
-
...(format && {
|
|
223
|
-
format,
|
|
224
|
-
}),
|
|
225
|
-
...this.enumToOpenApi(),
|
|
226
|
-
...tags,
|
|
227
|
-
...extra,
|
|
228
|
-
};
|
|
229
|
-
}
|
|
230
|
-
|
|
231
250
|
enumToOpenApi() {
|
|
232
251
|
const { enum: allowed } = this.meta;
|
|
233
252
|
if (allowed?.length) {
|
package/src/array.js
CHANGED
|
@@ -17,6 +17,9 @@ class ArraySchema extends Schema {
|
|
|
17
17
|
this.setup();
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
/**
|
|
21
|
+
* @private
|
|
22
|
+
*/
|
|
20
23
|
setup() {
|
|
21
24
|
const { schemas } = this.meta;
|
|
22
25
|
const schema =
|
|
@@ -140,4 +143,10 @@ class ArraySchema extends Schema {
|
|
|
140
143
|
}
|
|
141
144
|
}
|
|
142
145
|
|
|
146
|
+
/**
|
|
147
|
+
* @type {{
|
|
148
|
+
* (...schemas: Schema[]) : ArraySchema;
|
|
149
|
+
* (schemas: Schema[]) : ArraySchema;
|
|
150
|
+
* }}
|
|
151
|
+
*/
|
|
143
152
|
export default wrapSchema(ArraySchema);
|
package/src/boolean.js
CHANGED
package/src/date.js
CHANGED
|
@@ -17,44 +17,60 @@ class DateSchema extends Schema {
|
|
|
17
17
|
});
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
/**
|
|
21
|
+
* @param {string|number|Date} min
|
|
22
|
+
*/
|
|
20
23
|
min(min) {
|
|
21
24
|
min = new Date(min);
|
|
22
25
|
return this.clone().assert('min', (date) => {
|
|
23
26
|
if (date < min) {
|
|
24
27
|
throw new LocalizedError('Must be after {date}.', {
|
|
28
|
+
// @ts-ignore
|
|
25
29
|
date: min.toISOString(),
|
|
26
30
|
});
|
|
27
31
|
}
|
|
28
32
|
});
|
|
29
33
|
}
|
|
30
34
|
|
|
35
|
+
/**
|
|
36
|
+
* @param {string|number|Date} max
|
|
37
|
+
*/
|
|
31
38
|
max(max) {
|
|
32
39
|
max = new Date(max);
|
|
33
40
|
return this.clone().assert('max', (date) => {
|
|
34
41
|
if (date > max) {
|
|
35
42
|
throw new LocalizedError('Must be before {date}.', {
|
|
43
|
+
// @ts-ignore
|
|
36
44
|
date: max.toISOString(),
|
|
37
45
|
});
|
|
38
46
|
}
|
|
39
47
|
});
|
|
40
48
|
}
|
|
41
49
|
|
|
50
|
+
/**
|
|
51
|
+
* @param {string|number|Date} max
|
|
52
|
+
*/
|
|
42
53
|
before(max) {
|
|
43
54
|
max = new Date(max);
|
|
44
55
|
return this.clone().assert('before', (date) => {
|
|
45
56
|
if (date >= max) {
|
|
46
57
|
throw new LocalizedError('Must be before {date}.', {
|
|
58
|
+
// @ts-ignore
|
|
47
59
|
date: max.toISOString(),
|
|
48
60
|
});
|
|
49
61
|
}
|
|
50
62
|
});
|
|
51
63
|
}
|
|
52
64
|
|
|
65
|
+
/**
|
|
66
|
+
* @param {string|number|Date} min
|
|
67
|
+
*/
|
|
53
68
|
after(min) {
|
|
54
69
|
min = new Date(min);
|
|
55
70
|
return this.clone().assert('after', (date) => {
|
|
56
71
|
if (date <= min) {
|
|
57
72
|
throw new LocalizedError('Must be after {date}.', {
|
|
73
|
+
// @ts-ignore
|
|
58
74
|
date: min.toISOString(),
|
|
59
75
|
});
|
|
60
76
|
}
|
|
@@ -79,6 +95,9 @@ class DateSchema extends Schema {
|
|
|
79
95
|
});
|
|
80
96
|
}
|
|
81
97
|
|
|
98
|
+
/**
|
|
99
|
+
* @param {"date" | "date-time"} format
|
|
100
|
+
*/
|
|
82
101
|
iso(format = 'date-time') {
|
|
83
102
|
return this.clone({ format }).assert('format', (val, options) => {
|
|
84
103
|
const { original } = options;
|
|
@@ -127,4 +146,7 @@ class DateSchema extends Schema {
|
|
|
127
146
|
}
|
|
128
147
|
}
|
|
129
148
|
|
|
149
|
+
/**
|
|
150
|
+
* @type {() => DateSchema}
|
|
151
|
+
*/
|
|
130
152
|
export default wrapSchema(DateSchema);
|
package/src/index.js
CHANGED
|
@@ -5,14 +5,47 @@ import number from './number';
|
|
|
5
5
|
import object from './object';
|
|
6
6
|
import string from './string';
|
|
7
7
|
|
|
8
|
-
import
|
|
9
|
-
import {
|
|
8
|
+
import Schema from './Schema';
|
|
9
|
+
import { isSchema, isSchemaError } from './utils';
|
|
10
|
+
import { useLocalizer, getLocalizedMessages } from './localization';
|
|
10
11
|
import { LocalizedError } from './errors';
|
|
11
12
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
function any() {
|
|
14
|
+
return new Schema();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function allow(...args) {
|
|
18
|
+
return new Schema().allow(...args);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function reject(...args) {
|
|
22
|
+
return new Schema().reject(...args);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @param {import("./Schema").CustomSignature} args
|
|
27
|
+
*/
|
|
28
|
+
function custom(...args) {
|
|
29
|
+
return new Schema().custom(...args);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export {
|
|
33
|
+
array,
|
|
34
|
+
boolean,
|
|
35
|
+
date,
|
|
36
|
+
number,
|
|
37
|
+
object,
|
|
38
|
+
string,
|
|
39
|
+
any,
|
|
40
|
+
allow,
|
|
41
|
+
reject,
|
|
42
|
+
custom,
|
|
43
|
+
isSchema,
|
|
44
|
+
isSchemaError,
|
|
45
|
+
useLocalizer,
|
|
46
|
+
getLocalizedMessages,
|
|
47
|
+
LocalizedError,
|
|
48
|
+
};
|
|
16
49
|
|
|
17
50
|
export default {
|
|
18
51
|
array,
|
|
@@ -28,6 +61,6 @@ export default {
|
|
|
28
61
|
isSchema,
|
|
29
62
|
isSchemaError,
|
|
30
63
|
useLocalizer,
|
|
31
|
-
|
|
64
|
+
getLocalizedMessages,
|
|
32
65
|
LocalizedError,
|
|
33
66
|
};
|
package/src/localization.js
CHANGED
|
@@ -1,38 +1,52 @@
|
|
|
1
1
|
const TOKEN_REG = /{(.+?)}/g;
|
|
2
2
|
|
|
3
3
|
let localizer;
|
|
4
|
-
let templates = {};
|
|
5
4
|
|
|
5
|
+
/**
|
|
6
|
+
* @type {{ [key: string]: string }}
|
|
7
|
+
*/
|
|
8
|
+
let messages = {};
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @param {{ [key: string]: string } | ((key: string) => string)} arg
|
|
12
|
+
* An object that maps messages to localized strings or a function that
|
|
13
|
+
* accepts a message and returns a localized string. Use "getLocalizedMessages"
|
|
14
|
+
* to see the messages that exist.
|
|
15
|
+
*/
|
|
6
16
|
export function useLocalizer(arg) {
|
|
7
|
-
const fn = typeof arg === 'function' ? arg : (
|
|
17
|
+
const fn = typeof arg === 'function' ? arg : (message) => arg[message];
|
|
8
18
|
localizer = fn;
|
|
9
|
-
|
|
19
|
+
messages = {};
|
|
10
20
|
}
|
|
11
21
|
|
|
12
|
-
export function getLocalized(
|
|
22
|
+
export function getLocalized(message) {
|
|
13
23
|
if (localizer) {
|
|
14
|
-
return localizer(
|
|
24
|
+
return localizer(message);
|
|
15
25
|
}
|
|
16
26
|
}
|
|
17
27
|
|
|
18
|
-
export function localize(
|
|
19
|
-
let
|
|
20
|
-
if (
|
|
21
|
-
let localized = getLocalized(
|
|
28
|
+
export function localize(message, values = {}) {
|
|
29
|
+
let str = message;
|
|
30
|
+
if (str) {
|
|
31
|
+
let localized = getLocalized(message);
|
|
22
32
|
if (typeof localized === 'function') {
|
|
23
33
|
localized = localized(values);
|
|
24
34
|
}
|
|
25
35
|
if (localized) {
|
|
26
|
-
|
|
36
|
+
str = localized;
|
|
27
37
|
}
|
|
28
38
|
}
|
|
29
|
-
|
|
39
|
+
messages[message] = str;
|
|
30
40
|
|
|
31
|
-
return
|
|
41
|
+
return str.replace(TOKEN_REG, (match, token) => {
|
|
32
42
|
return token in values ? values[token] : match;
|
|
33
43
|
});
|
|
34
44
|
}
|
|
35
45
|
|
|
36
|
-
|
|
37
|
-
|
|
46
|
+
/**
|
|
47
|
+
* Returns an object containing all encountered messages
|
|
48
|
+
* mapped to their localizations.
|
|
49
|
+
*/
|
|
50
|
+
export function getLocalizedMessages() {
|
|
51
|
+
return messages;
|
|
38
52
|
}
|
package/src/number.js
CHANGED
|
@@ -16,22 +16,28 @@ class NumberSchema extends TypeSchema {
|
|
|
16
16
|
});
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
/**
|
|
20
|
+
* @param {number} min
|
|
21
|
+
* @param {string} message
|
|
22
|
+
*/
|
|
23
|
+
min(min, message = 'Must be greater than {min}.') {
|
|
21
24
|
return this.clone({ min }).assert('min', (num) => {
|
|
22
25
|
if (num < min) {
|
|
23
|
-
throw new LocalizedError(
|
|
26
|
+
throw new LocalizedError(message, {
|
|
24
27
|
min,
|
|
25
28
|
});
|
|
26
29
|
}
|
|
27
30
|
});
|
|
28
31
|
}
|
|
29
32
|
|
|
30
|
-
|
|
31
|
-
|
|
33
|
+
/**
|
|
34
|
+
* @param {number} max
|
|
35
|
+
* @param {string} message
|
|
36
|
+
*/
|
|
37
|
+
max(max, message = 'Must be less than {max}.') {
|
|
32
38
|
return this.clone({ max }).assert('max', (num) => {
|
|
33
39
|
if (num > max) {
|
|
34
|
-
throw new LocalizedError(
|
|
40
|
+
throw new LocalizedError(message, {
|
|
35
41
|
max,
|
|
36
42
|
});
|
|
37
43
|
}
|
|
@@ -81,4 +87,7 @@ class NumberSchema extends TypeSchema {
|
|
|
81
87
|
}
|
|
82
88
|
}
|
|
83
89
|
|
|
90
|
+
/**
|
|
91
|
+
* @type {() => NumberSchema}
|
|
92
|
+
*/
|
|
84
93
|
export default wrapSchema(NumberSchema);
|
package/src/object.js
CHANGED
|
@@ -11,6 +11,9 @@ class ObjectSchema extends TypeSchema {
|
|
|
11
11
|
this.setup();
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
/**
|
|
15
|
+
* @private
|
|
16
|
+
*/
|
|
14
17
|
setup() {
|
|
15
18
|
this.assert('type', (val) => {
|
|
16
19
|
if (val === null || typeof val !== 'object') {
|
|
@@ -76,6 +79,10 @@ class ObjectSchema extends TypeSchema {
|
|
|
76
79
|
}
|
|
77
80
|
}
|
|
78
81
|
|
|
82
|
+
/**
|
|
83
|
+
* @param {object|ObjectSchema} arg
|
|
84
|
+
* @returns Schema
|
|
85
|
+
*/
|
|
79
86
|
append(arg) {
|
|
80
87
|
let schema;
|
|
81
88
|
if (arg instanceof ObjectSchema) {
|
|
@@ -124,4 +131,7 @@ class ObjectSchema extends TypeSchema {
|
|
|
124
131
|
}
|
|
125
132
|
}
|
|
126
133
|
|
|
134
|
+
/**
|
|
135
|
+
* @type {(arg: object) => ObjectSchema}
|
|
136
|
+
*/
|
|
127
137
|
export default wrapSchema(ObjectSchema);
|
package/src/string.js
CHANGED
|
@@ -28,6 +28,9 @@ class StringSchema extends TypeSchema {
|
|
|
28
28
|
});
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
+
/**
|
|
32
|
+
* @param {number} length
|
|
33
|
+
*/
|
|
31
34
|
min(length) {
|
|
32
35
|
return this.clone({ min: length }).assert('length', (str) => {
|
|
33
36
|
if (str && str.length < length) {
|
|
@@ -38,6 +41,9 @@ class StringSchema extends TypeSchema {
|
|
|
38
41
|
});
|
|
39
42
|
}
|
|
40
43
|
|
|
44
|
+
/**
|
|
45
|
+
* @param {number} length
|
|
46
|
+
*/
|
|
41
47
|
max(length) {
|
|
42
48
|
return this.clone({ max: length }).assert('length', (str) => {
|
|
43
49
|
if (str && str.length > length) {
|
|
@@ -54,6 +60,9 @@ class StringSchema extends TypeSchema {
|
|
|
54
60
|
});
|
|
55
61
|
}
|
|
56
62
|
|
|
63
|
+
/**
|
|
64
|
+
* @param {boolean} [assert] Throws an error if not lowercase. Default: `false`.
|
|
65
|
+
*/
|
|
57
66
|
lowercase(assert = false) {
|
|
58
67
|
return this.clone().transform((str) => {
|
|
59
68
|
const lower = str.toLowerCase();
|
|
@@ -66,6 +75,9 @@ class StringSchema extends TypeSchema {
|
|
|
66
75
|
});
|
|
67
76
|
}
|
|
68
77
|
|
|
78
|
+
/**
|
|
79
|
+
* @param {boolean} [assert] Throws an error if not uppercase. Default: `false`.
|
|
80
|
+
*/
|
|
69
81
|
uppercase(assert = false) {
|
|
70
82
|
return this.clone().transform((str) => {
|
|
71
83
|
const upper = str.toUpperCase();
|
|
@@ -78,6 +90,9 @@ class StringSchema extends TypeSchema {
|
|
|
78
90
|
});
|
|
79
91
|
}
|
|
80
92
|
|
|
93
|
+
/**
|
|
94
|
+
* @param {RegExp} reg
|
|
95
|
+
*/
|
|
81
96
|
match(reg) {
|
|
82
97
|
if (!(reg instanceof RegExp)) {
|
|
83
98
|
throw new LocalizedError('Argument must be a regular expression');
|
|
@@ -139,6 +154,10 @@ class StringSchema extends TypeSchema {
|
|
|
139
154
|
});
|
|
140
155
|
}
|
|
141
156
|
|
|
157
|
+
/**
|
|
158
|
+
* @param {Object} [options]
|
|
159
|
+
* @param {boolean} [options.urlSafe]
|
|
160
|
+
*/
|
|
142
161
|
base64(options) {
|
|
143
162
|
return this.format('base64', (str) => {
|
|
144
163
|
if (!validator.isBase64(str, options)) {
|
|
@@ -204,6 +223,9 @@ class StringSchema extends TypeSchema {
|
|
|
204
223
|
});
|
|
205
224
|
}
|
|
206
225
|
|
|
226
|
+
/**
|
|
227
|
+
* @param {string} locale
|
|
228
|
+
*/
|
|
207
229
|
postalCode(locale = 'any') {
|
|
208
230
|
return this.format('postal-code', (str) => {
|
|
209
231
|
if (!validator.isPostalCode(str, locale)) {
|
|
@@ -212,8 +234,16 @@ class StringSchema extends TypeSchema {
|
|
|
212
234
|
});
|
|
213
235
|
}
|
|
214
236
|
|
|
237
|
+
/**
|
|
238
|
+
* @param {Object} [options]
|
|
239
|
+
* @param {number} [options.minLength]
|
|
240
|
+
* @param {number} [options.minNumbers]
|
|
241
|
+
* @param {number} [options.minSymbols]
|
|
242
|
+
* @param {number} [options.minLowercase]
|
|
243
|
+
* @param {number} [options.minUppercase]
|
|
244
|
+
*/
|
|
215
245
|
password(options = {}) {
|
|
216
|
-
const { minLength,
|
|
246
|
+
const { minLength, minNumbers, minSymbols, minLowercase, minUppercase } = {
|
|
217
247
|
...PASSWORD_DEFAULTS,
|
|
218
248
|
...options,
|
|
219
249
|
};
|
|
@@ -239,6 +269,18 @@ class StringSchema extends TypeSchema {
|
|
|
239
269
|
return schema;
|
|
240
270
|
}
|
|
241
271
|
|
|
272
|
+
/**
|
|
273
|
+
* @param {Object} [options]
|
|
274
|
+
* @param {boolean} [options.require_protocol]
|
|
275
|
+
* @param {boolean} [options.require_valid_protocol]
|
|
276
|
+
* @param {boolean} [options.require_host]
|
|
277
|
+
* @param {boolean} [options.require_port]
|
|
278
|
+
* @param {boolean} [options.allow_protocol_relative_urls]
|
|
279
|
+
* @param {boolean} [options.allow_fragments]
|
|
280
|
+
* @param {boolean} [options.allow_query_components]
|
|
281
|
+
* @param {boolean} [options.validate_length]
|
|
282
|
+
* @param {string[]} [options.protocols]
|
|
283
|
+
*/
|
|
242
284
|
url(options) {
|
|
243
285
|
return this.format('url', (str) => {
|
|
244
286
|
if (!validator.isURL(str, options)) {
|
|
@@ -247,6 +289,15 @@ class StringSchema extends TypeSchema {
|
|
|
247
289
|
});
|
|
248
290
|
}
|
|
249
291
|
|
|
292
|
+
/**
|
|
293
|
+
* @param {Object} [options]
|
|
294
|
+
* @param {boolean} [options.require_tld=true]
|
|
295
|
+
* @param {boolean} [options.allow_underscores=false]
|
|
296
|
+
* @param {boolean} [options.allow_trailing_dot=false]
|
|
297
|
+
* @param {boolean} [options.allow_numeric_tld=false]
|
|
298
|
+
* @param {boolean} [options.allow_wildcard=false]
|
|
299
|
+
* @param {boolean} [options.ignore_max_length=false]
|
|
300
|
+
*/
|
|
250
301
|
domain(options) {
|
|
251
302
|
return this.format('domain', (str) => {
|
|
252
303
|
if (!validator.isFQDN(str, options)) {
|
|
@@ -255,6 +306,9 @@ class StringSchema extends TypeSchema {
|
|
|
255
306
|
});
|
|
256
307
|
}
|
|
257
308
|
|
|
309
|
+
/**
|
|
310
|
+
* @param {1 | 2 | 3 | 4 | 5} [version] Version of UUID to check.
|
|
311
|
+
*/
|
|
258
312
|
uuid(version) {
|
|
259
313
|
return this.format('uuid', (str) => {
|
|
260
314
|
if (!validator.isUUID(str, version)) {
|
|
@@ -309,4 +363,7 @@ class StringSchema extends TypeSchema {
|
|
|
309
363
|
}
|
|
310
364
|
}
|
|
311
365
|
|
|
366
|
+
/**
|
|
367
|
+
* @type {() => StringSchema}
|
|
368
|
+
*/
|
|
312
369
|
export default wrapSchema(StringSchema);
|
package/src/utils.js
CHANGED
|
@@ -1,22 +1,8 @@
|
|
|
1
|
-
import Schema from './Schema';
|
|
2
|
-
|
|
3
1
|
export function wrapSchema(Class) {
|
|
4
2
|
return (...args) => {
|
|
5
3
|
return new Class(...args);
|
|
6
4
|
};
|
|
7
5
|
}
|
|
8
6
|
|
|
9
|
-
export function wrapArgs(name) {
|
|
10
|
-
return (...args) => {
|
|
11
|
-
return new Schema()[name](...args);
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export function wrapAny() {
|
|
16
|
-
return () => {
|
|
17
|
-
return new Schema();
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
|
|
21
7
|
export { isSchema } from './Schema';
|
|
22
8
|
export { isSchemaError } from './errors';
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
export function isSchema(arg: any): boolean;
|
|
2
|
+
/**
|
|
3
|
+
* @typedef {[fn: Function] | [type: string, fn: Function]} CustomSignature
|
|
4
|
+
*/
|
|
5
|
+
export default class Schema {
|
|
6
|
+
constructor(meta?: {});
|
|
7
|
+
assertions: any[];
|
|
8
|
+
meta: {};
|
|
9
|
+
required(): Schema;
|
|
10
|
+
default(value: any): Schema;
|
|
11
|
+
/**
|
|
12
|
+
* @param {CustomSignature} args
|
|
13
|
+
*/
|
|
14
|
+
custom(...args: CustomSignature): Schema;
|
|
15
|
+
strip(strip: any): Schema;
|
|
16
|
+
allow(...set: any[]): Schema;
|
|
17
|
+
reject(...set: any[]): Schema;
|
|
18
|
+
message(message: any): Schema;
|
|
19
|
+
tag(tags: any): Schema;
|
|
20
|
+
description(description: any): Schema;
|
|
21
|
+
options(options: any): Schema;
|
|
22
|
+
validate(value: any, options?: {}): Promise<any>;
|
|
23
|
+
/**
|
|
24
|
+
* @returns {this}
|
|
25
|
+
*/
|
|
26
|
+
clone(meta: any): this;
|
|
27
|
+
/**
|
|
28
|
+
* @returns {Schema}
|
|
29
|
+
*/
|
|
30
|
+
append(schema: any): Schema;
|
|
31
|
+
toOpenApi(extra: any): any;
|
|
32
|
+
assertEnum(set: any, allow: any): Schema;
|
|
33
|
+
assert(type: any, fn: any): Schema;
|
|
34
|
+
pushAssertion(assertion: any): void;
|
|
35
|
+
transform(fn: any): Schema;
|
|
36
|
+
getSortIndex(type: any): number;
|
|
37
|
+
runAssertion(assertion: any, value: any, options?: {}): Promise<any>;
|
|
38
|
+
enumToOpenApi(): {
|
|
39
|
+
type: "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function";
|
|
40
|
+
enum: any;
|
|
41
|
+
oneOf?: undefined;
|
|
42
|
+
} | {
|
|
43
|
+
oneOf: any[];
|
|
44
|
+
type?: undefined;
|
|
45
|
+
enum?: undefined;
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
export type CustomSignature = [fn: Function] | [type: string, fn: Function];
|
|
49
|
+
//# sourceMappingURL=Schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Schema.d.ts","sourceRoot":"","sources":["../src/Schema.js"],"names":[],"mappings":"AAgSA,4CAEC;AAvRD;;GAEG;AAEH;IACE,uBAGC;IAFC,kBAAoB;IACpB,SAAgB;IAKlB,mBAMC;IAED,4BAMC;IAED;;OAEG;IACH,gBAFW,eAAe,UAmBzB;IAED,0BAEC;IAED,6BAEC;IAED,8BAEC;IAED,8BAEC;IAED,uBAOC;IAED,sCAIC;IAED,8BAEC;IAED,iDAqCC;IAED;;OAEG;IACH,kBAFa,IAAI,CAOhB;IAED;;OAEG;IACH,qBAFa,MAAM,CAMlB;IAED,2BAgBC;IAID,yCA8BC;IAED,mCAQC;IAED,oCAKC;IAED,2BAOC;IAED,gCAGC;IAED,qEAUC;IAED;;;;;;;;MAoCC;CACF;8BAlRY,CAAC,EAAE,UAAU,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,UAAU,CAAC"}
|