@bedrockio/yada 1.0.1 → 1.0.3
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/cjs/Schema.js +265 -0
- package/dist/cjs/TypeSchema.js +30 -0
- package/dist/cjs/array.js +128 -0
- package/dist/cjs/boolean.js +29 -0
- package/dist/cjs/date.js +130 -0
- package/dist/cjs/errors.js +87 -0
- package/dist/cjs/index.js +36 -0
- package/dist/cjs/localization.js +39 -0
- package/dist/cjs/messages.js +75 -0
- package/dist/cjs/number.js +91 -0
- package/dist/cjs/object.js +104 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/password.js +52 -0
- package/dist/cjs/string.js +280 -0
- package/dist/cjs/utils.js +28 -0
- package/package.json +5 -2
- package/src/index.js +0 -15
- package/.eslintrc +0 -13
- package/babel.config.json +0 -17
- package/jest.config.json +0 -5
- package/scripts/build +0 -4
- package/src/__tests__/index.js +0 -1855
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.getLocalized = getLocalized;
|
|
5
|
+
exports.getLocalizerTemplates = getLocalizerTemplates;
|
|
6
|
+
exports.localize = localize;
|
|
7
|
+
exports.useLocalizer = useLocalizer;
|
|
8
|
+
const TOKEN_REG = /{(.+?)}/g;
|
|
9
|
+
let localizer;
|
|
10
|
+
let templates = {};
|
|
11
|
+
function useLocalizer(arg) {
|
|
12
|
+
const fn = typeof arg === 'function' ? arg : template => arg[template];
|
|
13
|
+
localizer = fn;
|
|
14
|
+
templates = {};
|
|
15
|
+
}
|
|
16
|
+
function getLocalized(template) {
|
|
17
|
+
if (localizer) {
|
|
18
|
+
return localizer(template);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function localize(template, values = {}) {
|
|
22
|
+
let message = template;
|
|
23
|
+
if (localizer) {
|
|
24
|
+
let localized = getLocalized(template);
|
|
25
|
+
if (typeof localized === 'function') {
|
|
26
|
+
localized = localized(values);
|
|
27
|
+
}
|
|
28
|
+
if (localized) {
|
|
29
|
+
message = localized;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
templates[template] = message;
|
|
33
|
+
return message.replace(TOKEN_REG, (match, token) => {
|
|
34
|
+
return values[token];
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
function getLocalizerTemplates() {
|
|
38
|
+
return templates;
|
|
39
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.getFullMessage = getFullMessage;
|
|
5
|
+
var _errors = require("./errors");
|
|
6
|
+
var _localization = require("./localization");
|
|
7
|
+
function getFullMessage(error, options) {
|
|
8
|
+
const {
|
|
9
|
+
delimiter
|
|
10
|
+
} = options;
|
|
11
|
+
if (error.details) {
|
|
12
|
+
return error.details.map(error => {
|
|
13
|
+
if ((0, _errors.isSchemaError)(error)) {
|
|
14
|
+
return getFullMessage(error, {
|
|
15
|
+
field: error.field,
|
|
16
|
+
index: error.index,
|
|
17
|
+
...options
|
|
18
|
+
});
|
|
19
|
+
} else {
|
|
20
|
+
return error.message;
|
|
21
|
+
}
|
|
22
|
+
}).join(delimiter);
|
|
23
|
+
} else {
|
|
24
|
+
return getLabeledMessage(error, options);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function getLabeledMessage(error, options) {
|
|
28
|
+
const {
|
|
29
|
+
field,
|
|
30
|
+
index
|
|
31
|
+
} = options;
|
|
32
|
+
const base = getBase(error.message);
|
|
33
|
+
if (field) {
|
|
34
|
+
const msg = `{field} ${base}`;
|
|
35
|
+
return (0, _localization.localize)(msg, {
|
|
36
|
+
field: getFieldLabel(field, options)
|
|
37
|
+
});
|
|
38
|
+
} else if (index != null) {
|
|
39
|
+
const msg = `Element at index "{index}" ${base}`;
|
|
40
|
+
return (0, _localization.localize)(msg, {
|
|
41
|
+
index
|
|
42
|
+
});
|
|
43
|
+
} else {
|
|
44
|
+
return (0, _localization.localize)(base);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function getFieldLabel(field, options) {
|
|
48
|
+
const {
|
|
49
|
+
natural
|
|
50
|
+
} = options;
|
|
51
|
+
if (natural) {
|
|
52
|
+
return naturalize(field);
|
|
53
|
+
} else {
|
|
54
|
+
return `"${field}"`;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function getBase(str) {
|
|
58
|
+
if (str === 'Value is required.') {
|
|
59
|
+
return 'is required.';
|
|
60
|
+
} else {
|
|
61
|
+
return downcase(str);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function naturalize(str) {
|
|
65
|
+
const first = str.slice(0, 1).toUpperCase();
|
|
66
|
+
let rest = str.slice(1);
|
|
67
|
+
rest = rest.replace(/[A-Z]+/, caps => {
|
|
68
|
+
return ' ' + caps.toLowerCase();
|
|
69
|
+
});
|
|
70
|
+
rest = rest.replace(/[-_]/g, ' ');
|
|
71
|
+
return first + rest;
|
|
72
|
+
}
|
|
73
|
+
function downcase(str) {
|
|
74
|
+
return str.slice(0, 1).toLowerCase() + str.slice(1);
|
|
75
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.default = void 0;
|
|
5
|
+
var _TypeSchema = _interopRequireDefault(require("./TypeSchema"));
|
|
6
|
+
var _errors = require("./errors");
|
|
7
|
+
var _utils = require("./utils");
|
|
8
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
9
|
+
class NumberSchema extends _TypeSchema.default {
|
|
10
|
+
constructor() {
|
|
11
|
+
super(Number);
|
|
12
|
+
this.assert('type', (val, options) => {
|
|
13
|
+
if (typeof val === 'string' && options.cast) {
|
|
14
|
+
val = Number(val);
|
|
15
|
+
}
|
|
16
|
+
if (typeof val !== 'number' || isNaN(val)) {
|
|
17
|
+
throw new _errors.LocalizedError('Must be a number.');
|
|
18
|
+
}
|
|
19
|
+
return val;
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
min(min, msg) {
|
|
23
|
+
msg ||= 'Must be greater than {min}.';
|
|
24
|
+
return this.clone({
|
|
25
|
+
min
|
|
26
|
+
}).assert('min', num => {
|
|
27
|
+
if (num < min) {
|
|
28
|
+
throw new _errors.LocalizedError(msg, {
|
|
29
|
+
min
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
max(max, msg) {
|
|
35
|
+
msg ||= 'Must be less than {max}.';
|
|
36
|
+
return this.clone({
|
|
37
|
+
max
|
|
38
|
+
}).assert('max', num => {
|
|
39
|
+
if (num > max) {
|
|
40
|
+
throw new _errors.LocalizedError(msg, {
|
|
41
|
+
max
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
negative() {
|
|
47
|
+
return this.max(0, 'Must be negative.');
|
|
48
|
+
}
|
|
49
|
+
positive() {
|
|
50
|
+
return this.min(0, 'Must be positive.');
|
|
51
|
+
}
|
|
52
|
+
integer() {
|
|
53
|
+
return this.clone().assert('integer', num => {
|
|
54
|
+
if (!Number.isInteger(num)) {
|
|
55
|
+
throw new _errors.LocalizedError('Must be an integer.');
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
multiple(multiple) {
|
|
60
|
+
return this.clone({
|
|
61
|
+
multiple
|
|
62
|
+
}).assert('multiple', num => {
|
|
63
|
+
if (num % multiple !== 0) {
|
|
64
|
+
throw new _errors.LocalizedError('Must be a multiple of {multiple}.', {
|
|
65
|
+
multiple
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
toOpenApi(extra) {
|
|
71
|
+
const {
|
|
72
|
+
min,
|
|
73
|
+
max,
|
|
74
|
+
multiple
|
|
75
|
+
} = this.meta;
|
|
76
|
+
return {
|
|
77
|
+
...super.toOpenApi(extra),
|
|
78
|
+
...(min != null && {
|
|
79
|
+
minimum: min
|
|
80
|
+
}),
|
|
81
|
+
...(max != null && {
|
|
82
|
+
maximum: max
|
|
83
|
+
}),
|
|
84
|
+
...(multiple != null && {
|
|
85
|
+
multipleOf: multiple
|
|
86
|
+
})
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
var _default = (0, _utils.wrapSchema)(NumberSchema);
|
|
91
|
+
exports.default = _default;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.default = void 0;
|
|
5
|
+
var _TypeSchema = _interopRequireDefault(require("./TypeSchema"));
|
|
6
|
+
var _errors = require("./errors");
|
|
7
|
+
var _utils = require("./utils");
|
|
8
|
+
var _Schema = require("./Schema");
|
|
9
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
10
|
+
const BASE_ASSERTIONS = ['type', 'transform', 'field'];
|
|
11
|
+
class ObjectSchema extends _TypeSchema.default {
|
|
12
|
+
constructor(fields = {}) {
|
|
13
|
+
super(Object, {
|
|
14
|
+
message: 'Object failed validation.',
|
|
15
|
+
fields
|
|
16
|
+
});
|
|
17
|
+
this.setup();
|
|
18
|
+
}
|
|
19
|
+
setup() {
|
|
20
|
+
this.assert('type', val => {
|
|
21
|
+
if (val === null || typeof val !== 'object') {
|
|
22
|
+
throw new _errors.LocalizedError('Must be an object.');
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
this.transform((obj, options) => {
|
|
26
|
+
const {
|
|
27
|
+
fields,
|
|
28
|
+
stripUnknown
|
|
29
|
+
} = options;
|
|
30
|
+
const allowUnknown = Object.keys(fields).length === 0;
|
|
31
|
+
if (obj) {
|
|
32
|
+
const result = {};
|
|
33
|
+
for (let key of Object.keys(obj)) {
|
|
34
|
+
if (key in fields || allowUnknown) {
|
|
35
|
+
result[key] = obj[key];
|
|
36
|
+
} else if (!stripUnknown) {
|
|
37
|
+
throw new _errors.LocalizedError('Unknown field "{key}".', {
|
|
38
|
+
key
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return result;
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
for (let [key, schema] of Object.entries(this.meta.fields)) {
|
|
46
|
+
this.assert('field', async (obj, options) => {
|
|
47
|
+
if (obj) {
|
|
48
|
+
let val;
|
|
49
|
+
try {
|
|
50
|
+
val = await schema.validate(obj[key], options);
|
|
51
|
+
} catch (error) {
|
|
52
|
+
if (error.details?.length === 1) {
|
|
53
|
+
const {
|
|
54
|
+
message,
|
|
55
|
+
original
|
|
56
|
+
} = error.details[0];
|
|
57
|
+
throw new _errors.FieldError(message, key, original);
|
|
58
|
+
} else {
|
|
59
|
+
throw new _errors.FieldError('Field failed validation.', key, error.original, error.details);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
...obj,
|
|
64
|
+
...(val !== undefined && {
|
|
65
|
+
[key]: val
|
|
66
|
+
})
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
append(arg) {
|
|
73
|
+
const schema = (0, _Schema.isSchema)(arg) ? arg : new ObjectSchema(arg);
|
|
74
|
+
const fields = {
|
|
75
|
+
...this.meta.fields,
|
|
76
|
+
...schema.meta.fields
|
|
77
|
+
};
|
|
78
|
+
const merged = new ObjectSchema(fields);
|
|
79
|
+
const assertions = [...this.assertions, ...schema.assertions];
|
|
80
|
+
for (let assertion of assertions) {
|
|
81
|
+
const {
|
|
82
|
+
type
|
|
83
|
+
} = assertion;
|
|
84
|
+
if (!BASE_ASSERTIONS.includes(type)) {
|
|
85
|
+
merged.pushAssertion(assertion);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return merged;
|
|
89
|
+
}
|
|
90
|
+
toOpenApi(extra) {
|
|
91
|
+
const properties = {};
|
|
92
|
+
for (let [key, schema] of Object.entries(this.meta.fields)) {
|
|
93
|
+
properties[key] = schema.toOpenApi();
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
...super.toOpenApi(extra),
|
|
97
|
+
...(Object.keys(properties).length > 0 && {
|
|
98
|
+
properties
|
|
99
|
+
})
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
var _default = (0, _utils.wrapSchema)(ObjectSchema);
|
|
104
|
+
exports.default = _default;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{ "type": "commonjs" }
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.PASSWORD_DEFAULTS = void 0;
|
|
5
|
+
exports.validateLength = validateLength;
|
|
6
|
+
exports.validateUppercase = exports.validateSymbols = exports.validateNumbers = exports.validateLowercase = void 0;
|
|
7
|
+
var _errors = require("./errors");
|
|
8
|
+
const LOWER_REG = /[a-z]/g;
|
|
9
|
+
const UPPER_REG = /[A-Z]/g;
|
|
10
|
+
const NUMBER_REG = /[0-9]/g;
|
|
11
|
+
const SYMBOL_REG = /[!@#$%^&*]/g;
|
|
12
|
+
const PASSWORD_DEFAULTS = {
|
|
13
|
+
minLength: 12,
|
|
14
|
+
minLowercase: 0,
|
|
15
|
+
minUppercase: 0,
|
|
16
|
+
minNumbers: 0,
|
|
17
|
+
minSymbols: 0
|
|
18
|
+
};
|
|
19
|
+
exports.PASSWORD_DEFAULTS = PASSWORD_DEFAULTS;
|
|
20
|
+
function validateLength(expected) {
|
|
21
|
+
return (str = '') => {
|
|
22
|
+
if (str.length < expected) {
|
|
23
|
+
const s = expected === 1 ? '' : 's';
|
|
24
|
+
throw new _errors.LocalizedError('Must be at least {length} character{s}.', {
|
|
25
|
+
length: expected,
|
|
26
|
+
s
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
const validateLowercase = validateRegex(LOWER_REG, 'Must contain at least {length} lowercase character{s}.');
|
|
32
|
+
exports.validateLowercase = validateLowercase;
|
|
33
|
+
const validateUppercase = validateRegex(UPPER_REG, 'Must contain at least {length} uppercase character{s}.');
|
|
34
|
+
exports.validateUppercase = validateUppercase;
|
|
35
|
+
const validateNumbers = validateRegex(NUMBER_REG, 'Must contain at least {length} number{s}.');
|
|
36
|
+
exports.validateNumbers = validateNumbers;
|
|
37
|
+
const validateSymbols = validateRegex(SYMBOL_REG, 'Must contain at least {length} symbol{s}.');
|
|
38
|
+
exports.validateSymbols = validateSymbols;
|
|
39
|
+
function validateRegex(reg, message) {
|
|
40
|
+
return expected => {
|
|
41
|
+
return (str = '') => {
|
|
42
|
+
const length = str.match(reg)?.length || 0;
|
|
43
|
+
if (length < expected) {
|
|
44
|
+
const s = expected === 1 ? '' : 's';
|
|
45
|
+
throw new _errors.LocalizedError(message, {
|
|
46
|
+
length: expected,
|
|
47
|
+
s
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
}
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.default = void 0;
|
|
5
|
+
var _validator = _interopRequireDefault(require("validator"));
|
|
6
|
+
var _TypeSchema = _interopRequireDefault(require("./TypeSchema"));
|
|
7
|
+
var _errors = require("./errors");
|
|
8
|
+
var _utils = require("./utils");
|
|
9
|
+
var _password = require("./password");
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
+
const SLUG_REG = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
12
|
+
class StringSchema extends _TypeSchema.default {
|
|
13
|
+
constructor() {
|
|
14
|
+
super(String);
|
|
15
|
+
this.assert('type', (val, options) => {
|
|
16
|
+
if (typeof val !== 'string' && options.cast) {
|
|
17
|
+
val = String(val);
|
|
18
|
+
}
|
|
19
|
+
if (typeof val !== 'string') {
|
|
20
|
+
throw new _errors.LocalizedError('Must be a string.');
|
|
21
|
+
}
|
|
22
|
+
return val;
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
min(length) {
|
|
26
|
+
return this.clone({
|
|
27
|
+
min: length
|
|
28
|
+
}).assert('length', str => {
|
|
29
|
+
if (str && str.length < length) {
|
|
30
|
+
throw new _errors.LocalizedError('Must be {length} characters or more.', {
|
|
31
|
+
length
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
max(length) {
|
|
37
|
+
return this.clone({
|
|
38
|
+
max: length
|
|
39
|
+
}).assert('length', str => {
|
|
40
|
+
if (str && str.length > length) {
|
|
41
|
+
throw new _errors.LocalizedError('Must be {length} characters or less.', {
|
|
42
|
+
length
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
trim() {
|
|
48
|
+
return this.clone().transform(str => {
|
|
49
|
+
return str.trim();
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
lowercase(assert = false) {
|
|
53
|
+
return this.clone().transform(str => {
|
|
54
|
+
const lower = str.toLowerCase();
|
|
55
|
+
if (lower !== str) {
|
|
56
|
+
if (assert) {
|
|
57
|
+
throw new _errors.LocalizedError('Must be in lower case.');
|
|
58
|
+
}
|
|
59
|
+
return lower;
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
uppercase(assert = false) {
|
|
64
|
+
return this.clone().transform(str => {
|
|
65
|
+
const upper = str.toUpperCase();
|
|
66
|
+
if (upper !== str) {
|
|
67
|
+
if (assert) {
|
|
68
|
+
throw new _errors.LocalizedError('Must be in upper case.');
|
|
69
|
+
}
|
|
70
|
+
return upper;
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
match(reg) {
|
|
75
|
+
if (!(reg instanceof RegExp)) {
|
|
76
|
+
throw new _errors.LocalizedError('Argument must be a regular expression');
|
|
77
|
+
}
|
|
78
|
+
return this.clone().assert('regex', str => {
|
|
79
|
+
if (str && !reg.test(str)) {
|
|
80
|
+
throw new _errors.LocalizedError('Must match pattern {reg}.', {
|
|
81
|
+
reg
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
email() {
|
|
87
|
+
return this.format('email', str => {
|
|
88
|
+
if (!_validator.default.isEmail(str)) {
|
|
89
|
+
throw new _errors.LocalizedError('Must be an email address.');
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
hex() {
|
|
94
|
+
return this.format('hex', str => {
|
|
95
|
+
if (!_validator.default.isHexadecimal(str)) {
|
|
96
|
+
throw new _errors.LocalizedError('Must be hexadecimal.');
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
md5() {
|
|
101
|
+
return this.format('md5', str => {
|
|
102
|
+
if (!_validator.default.isHash(str, 'md5')) {
|
|
103
|
+
throw new _errors.LocalizedError('Must be a hash in md5 format.');
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
sha1() {
|
|
108
|
+
return this.format('sha1', str => {
|
|
109
|
+
if (!_validator.default.isHash(str, 'sha1')) {
|
|
110
|
+
throw new _errors.LocalizedError('Must be a hash in sha1 format.');
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
ascii() {
|
|
115
|
+
return this.format('ascii', str => {
|
|
116
|
+
if (!_validator.default.isAscii(str)) {
|
|
117
|
+
throw new _errors.LocalizedError('Must be ASCII.');
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
base64(options) {
|
|
122
|
+
return this.format('base64', str => {
|
|
123
|
+
if (!_validator.default.isBase64(str, options)) {
|
|
124
|
+
throw new _errors.LocalizedError('Must be base64.');
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
creditCard() {
|
|
129
|
+
return this.format('credit-card', str => {
|
|
130
|
+
if (!_validator.default.isCreditCard(str)) {
|
|
131
|
+
throw new _errors.LocalizedError('Must be a valid credit card number.');
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
ip() {
|
|
136
|
+
return this.format('ip', str => {
|
|
137
|
+
if (!_validator.default.isIP(str)) {
|
|
138
|
+
throw new _errors.LocalizedError('Must be a valid IP address.');
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
country() {
|
|
143
|
+
return this.format('country', str => {
|
|
144
|
+
if (!_validator.default.isISO31661Alpha2(str)) {
|
|
145
|
+
throw new _errors.LocalizedError('Must be a valid country code.');
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
locale() {
|
|
150
|
+
return this.format('locale', str => {
|
|
151
|
+
if (!_validator.default.isLocale(str)) {
|
|
152
|
+
throw new _errors.LocalizedError('Must be a valid locale code.');
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
jwt() {
|
|
157
|
+
return this.format('jwt', str => {
|
|
158
|
+
if (!_validator.default.isJWT(str)) {
|
|
159
|
+
throw new _errors.LocalizedError('Must be a valid JWT token.');
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
slug() {
|
|
164
|
+
return this.format('slug', str => {
|
|
165
|
+
// Validator shows some issues here so use a custom regex.
|
|
166
|
+
if (!SLUG_REG.test(str)) {
|
|
167
|
+
throw new _errors.LocalizedError('Must be a valid slug.');
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
latlng() {
|
|
172
|
+
return this.format('latlng', str => {
|
|
173
|
+
if (!_validator.default.isLatLong(str)) {
|
|
174
|
+
throw new _errors.LocalizedError('Must be a valid lat,lng coordinate.');
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
postalCode(locale = 'any') {
|
|
179
|
+
return this.format('postal-code', str => {
|
|
180
|
+
if (!_validator.default.isPostalCode(str, locale)) {
|
|
181
|
+
throw new _errors.LocalizedError('Must be a valid postal code.');
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
password(options = {}) {
|
|
186
|
+
const {
|
|
187
|
+
minLength,
|
|
188
|
+
minLowercase,
|
|
189
|
+
minUppercase,
|
|
190
|
+
minNumbers,
|
|
191
|
+
minSymbols
|
|
192
|
+
} = {
|
|
193
|
+
..._password.PASSWORD_DEFAULTS,
|
|
194
|
+
...options
|
|
195
|
+
};
|
|
196
|
+
const schema = this.clone();
|
|
197
|
+
if (minLength) {
|
|
198
|
+
schema.assert('password', (0, _password.validateLength)(minLength));
|
|
199
|
+
}
|
|
200
|
+
if (minLowercase) {
|
|
201
|
+
schema.assert('password', (0, _password.validateLowercase)(minLowercase));
|
|
202
|
+
}
|
|
203
|
+
if (minUppercase) {
|
|
204
|
+
schema.assert('password', (0, _password.validateUppercase)(minUppercase));
|
|
205
|
+
}
|
|
206
|
+
if (minNumbers) {
|
|
207
|
+
schema.assert('password', (0, _password.validateNumbers)(minNumbers));
|
|
208
|
+
}
|
|
209
|
+
if (minSymbols) {
|
|
210
|
+
schema.assert('password', (0, _password.validateSymbols)(minSymbols));
|
|
211
|
+
}
|
|
212
|
+
return schema;
|
|
213
|
+
}
|
|
214
|
+
url(options) {
|
|
215
|
+
return this.format('url', str => {
|
|
216
|
+
if (!_validator.default.isURL(str, options)) {
|
|
217
|
+
throw new _errors.LocalizedError('Must be a valid URL.');
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
domain(options) {
|
|
222
|
+
return this.format('domain', str => {
|
|
223
|
+
if (!_validator.default.isFQDN(str, options)) {
|
|
224
|
+
throw new _errors.LocalizedError('Must be a valid domain.');
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
uuid(version) {
|
|
229
|
+
return this.format('uuid', str => {
|
|
230
|
+
if (!_validator.default.isUUID(str, version)) {
|
|
231
|
+
throw new _errors.LocalizedError('Must be a valid unique id.');
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
btc() {
|
|
236
|
+
return this.format('bitcoin-address', str => {
|
|
237
|
+
if (!_validator.default.isBtcAddress(str)) {
|
|
238
|
+
throw new _errors.LocalizedError('Must be a valid Bitcoin address.');
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
eth() {
|
|
243
|
+
return this.format('etherium-address', str => {
|
|
244
|
+
if (!_validator.default.isEthereumAddress(str)) {
|
|
245
|
+
throw new _errors.LocalizedError('Must be a valid Ethereum address.');
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
swift() {
|
|
250
|
+
return this.format('swift-code', str => {
|
|
251
|
+
if (!_validator.default.isBIC(str)) {
|
|
252
|
+
throw new _errors.LocalizedError('Must be a valid SWIFT code.');
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
mongo() {
|
|
257
|
+
return this.format('mongo-object-id', str => {
|
|
258
|
+
if (!_validator.default.isMongoId(str)) {
|
|
259
|
+
throw new _errors.LocalizedError('Must be a valid ObjectId.');
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
toOpenApi(extra) {
|
|
264
|
+
const {
|
|
265
|
+
min,
|
|
266
|
+
max
|
|
267
|
+
} = this.meta;
|
|
268
|
+
return {
|
|
269
|
+
...super.toOpenApi(extra),
|
|
270
|
+
...(min != null && {
|
|
271
|
+
minLength: min
|
|
272
|
+
}),
|
|
273
|
+
...(max != null && {
|
|
274
|
+
maxLength: max
|
|
275
|
+
})
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
var _default = (0, _utils.wrapSchema)(StringSchema);
|
|
280
|
+
exports.default = _default;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.isSchemaError = exports.isSchema = void 0;
|
|
5
|
+
exports.wrapAny = wrapAny;
|
|
6
|
+
exports.wrapArgs = wrapArgs;
|
|
7
|
+
exports.wrapSchema = wrapSchema;
|
|
8
|
+
var _Schema = _interopRequireWildcard(require("./Schema"));
|
|
9
|
+
exports.isSchema = _Schema.isSchema;
|
|
10
|
+
var _errors = require("./errors");
|
|
11
|
+
exports.isSchemaError = _errors.isSchemaError;
|
|
12
|
+
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); }
|
|
13
|
+
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; }
|
|
14
|
+
function wrapSchema(Class) {
|
|
15
|
+
return (...args) => {
|
|
16
|
+
return new Class(...args);
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function wrapArgs(name) {
|
|
20
|
+
return (...args) => {
|
|
21
|
+
return new _Schema.default()[name](...args);
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
function wrapAny() {
|
|
25
|
+
return () => {
|
|
26
|
+
return new _Schema.default();
|
|
27
|
+
};
|
|
28
|
+
}
|
package/package.json
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bedrockio/yada",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "Validation library inspired by Joi.",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "NODE_OPTIONS=--experimental-vm-modules jest",
|
|
7
|
+
"lint": "eslint",
|
|
7
8
|
"build": "scripts/build",
|
|
8
|
-
"
|
|
9
|
+
"prepublish": "scripts/build"
|
|
9
10
|
},
|
|
10
11
|
"type": "module",
|
|
12
|
+
"main": "dist/cjs/index.js",
|
|
13
|
+
"module": "src/index.js",
|
|
11
14
|
"repository": "https://github.com/bedrockio/yada",
|
|
12
15
|
"author": "Andrew Plummer <plummer.andrew@gmail.com>",
|
|
13
16
|
"license": "MIT",
|