@bedrockio/yada 1.0.13 → 1.0.15

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.
Files changed (53) hide show
  1. package/README.md +3 -3
  2. package/dist/cjs/Schema.js +101 -24
  3. package/dist/cjs/TypeSchema.js +1 -2
  4. package/dist/cjs/array.js +21 -14
  5. package/dist/cjs/boolean.js +8 -5
  6. package/dist/cjs/date.js +32 -5
  7. package/dist/cjs/index.js +101 -7
  8. package/dist/cjs/localization.js +31 -15
  9. package/dist/cjs/number.js +22 -11
  10. package/dist/cjs/object.js +35 -10
  11. package/dist/cjs/string.js +76 -8
  12. package/dist/cjs/utils.js +2 -22
  13. package/package.json +11 -14
  14. package/src/Schema.js +83 -20
  15. package/src/array.js +19 -13
  16. package/src/boolean.js +6 -2
  17. package/src/date.js +25 -2
  18. package/src/index.js +50 -7
  19. package/src/localization.js +28 -14
  20. package/src/number.js +18 -8
  21. package/src/object.js +30 -8
  22. package/src/string.js +61 -3
  23. package/src/utils.js +0 -20
  24. package/types/Schema.d.ts +93 -0
  25. package/types/Schema.d.ts.map +1 -0
  26. package/types/TypeSchema.d.ts +7 -0
  27. package/types/TypeSchema.d.ts.map +1 -0
  28. package/types/array.d.ts +23 -0
  29. package/types/array.d.ts.map +1 -0
  30. package/types/boolean.d.ts +10 -0
  31. package/types/boolean.d.ts.map +1 -0
  32. package/types/date.d.ts +34 -0
  33. package/types/date.d.ts.map +1 -0
  34. package/types/errors.d.ts +54 -0
  35. package/types/errors.d.ts.map +1 -0
  36. package/types/index.d.ts +49 -0
  37. package/types/index.d.ts.map +1 -0
  38. package/types/localization.d.ts +19 -0
  39. package/types/localization.d.ts.map +1 -0
  40. package/types/messages.d.ts +2 -0
  41. package/types/messages.d.ts.map +1 -0
  42. package/types/number.d.ts +24 -0
  43. package/types/number.d.ts.map +1 -0
  44. package/types/object.d.ts +33 -0
  45. package/types/object.d.ts.map +1 -0
  46. package/types/password.d.ts +13 -0
  47. package/types/password.d.ts.map +1 -0
  48. package/types/string.d.ts +118 -0
  49. package/types/string.d.ts.map +1 -0
  50. package/types/utils.d.ts +3 -0
  51. package/types/utils.d.ts.map +1 -0
  52. package/.eslintignore +0 -1
  53. package/test/all.test.js +0 -1989
package/README.md CHANGED
@@ -607,7 +607,7 @@ yd.useLocalizer((template) => {
607
607
  Any validation message matching the keys passed to the map will result in the
608
608
  localized message instead. The curly braces allow for variable substitution.
609
609
 
610
- A special method `getLocalizerTemplates` will aggregte used error messages to
610
+ A special method `getLocalizedMessages` will aggregte used error messages to
611
611
  allow quick discovery of strings that have not yet been localized:
612
612
 
613
613
  ```js
@@ -616,7 +616,7 @@ yd.useLocalizer({
616
616
  'Object failed validation.': '不正な入力がありました。',
617
617
  });
618
618
  // Error validation occuring here
619
- yd.getLocalizedTemplates();
619
+ yd.getLocalizedMessages();
620
620
  // {
621
621
  // 'Must be at least {length} character{s}.': '{length}文字以上入力して下さい。',
622
622
  // 'Object failed validation.': '不正な入力がありました。',
@@ -685,7 +685,7 @@ Basic utility methods:
685
685
  - `isSchema`: returns `true` if the object passed is a schema.
686
686
  - `isSchemaError`: returns `true` if the error object is a schema error.
687
687
  - `useLocalizer`: Allows [localization](#localization) of error messages.
688
- - `getLocalizerTemplates`: Allows discovery of messages for
688
+ - `getLocalizedMessages`: Allows discovery of messages for
689
689
  [localization](#localization).
690
690
  - `LocalizedError`: An error object that can be thrown in custom validations to
691
691
  allow [localization](#localization).
@@ -8,6 +8,11 @@ exports.isSchema = isSchema;
8
8
  var _errors = require("./errors");
9
9
  const INITIAL_TYPES = ['default', 'required', 'type', 'transform'];
10
10
  const REQUIRED_TYPES = ['default', 'required'];
11
+
12
+ /**
13
+ * @typedef {[fn: Function] | [type: string, fn: Function]} CustomSignature
14
+ */
15
+
11
16
  class Schema {
12
17
  constructor(meta = {}) {
13
18
  this.assertions = [];
@@ -16,6 +21,9 @@ class Schema {
16
21
 
17
22
  // Public
18
23
 
24
+ /**
25
+ * @returns {this}
26
+ */
19
27
  required() {
20
28
  return this.clone({
21
29
  required: true
@@ -25,6 +33,11 @@ class Schema {
25
33
  }
26
34
  });
27
35
  }
36
+
37
+ /**
38
+ * Sets the schema default. [Link](https://github.com/bedrockio/yada#default)
39
+ * @returns {this}
40
+ */
28
41
  default(value) {
29
42
  return this.clone({
30
43
  default: value
@@ -34,9 +47,21 @@ class Schema {
34
47
  }
35
48
  });
36
49
  }
50
+
51
+ /**
52
+ * Validate by a custom function. [Link](https://github.com/bedrockio/yada#custom)
53
+ * @param {CustomSignature} args
54
+ * @returns {this}
55
+ */
37
56
  custom(...args) {
38
- const type = args.length > 1 ? args[0] : 'custom';
39
- const fn = args.length > 1 ? args[1] : args[0];
57
+ let type, fn;
58
+ if (typeof args[0] === 'function') {
59
+ type = 'custom';
60
+ fn = args[0];
61
+ } else {
62
+ type = args[0];
63
+ fn = args[1];
64
+ }
40
65
  if (!type) {
41
66
  throw new Error('Assertion type required.');
42
67
  } else if (!fn) {
@@ -46,22 +71,46 @@ class Schema {
46
71
  return await fn(val, options);
47
72
  });
48
73
  }
74
+
75
+ /**
76
+ * Conditionally exclude fields inside an object schema.
77
+ * [Link](https://github.com/bedrockio/yada#strip)
78
+ * @returns {this}
79
+ */
49
80
  strip(strip) {
50
81
  return this.clone({
51
82
  strip
52
83
  });
53
84
  }
85
+
86
+ /**
87
+ * Accept values or schemas. [Link](https://github.com/bedrockio/yada#allow)
88
+ * @returns {this}
89
+ */
54
90
  allow(...set) {
55
91
  return this.assertEnum(set, true);
56
92
  }
93
+
94
+ /**
95
+ * Reject values or schemas. [Link](https://github.com/bedrockio/yada#reject)
96
+ * @returns {this}
97
+ */
57
98
  reject(...set) {
58
99
  return this.assertEnum(set, false);
59
100
  }
101
+
102
+ /**
103
+ * @returns {this}
104
+ */
60
105
  message(message) {
61
106
  return this.clone({
62
107
  message
63
108
  });
64
109
  }
110
+
111
+ /**
112
+ * @returns {this}
113
+ */
65
114
  tag(tags) {
66
115
  return this.clone({
67
116
  tags: {
@@ -70,11 +119,19 @@ class Schema {
70
119
  }
71
120
  });
72
121
  }
122
+
123
+ /**
124
+ * @returns {this}
125
+ */
73
126
  description(description) {
74
127
  return this.tag({
75
128
  description
76
129
  });
77
130
  }
131
+
132
+ /**
133
+ * @returns {this}
134
+ */
78
135
  options(options) {
79
136
  return this.clone({
80
137
  ...options
@@ -116,6 +173,10 @@ class Schema {
116
173
  }
117
174
  return value;
118
175
  }
176
+
177
+ /**
178
+ * @returns {this}
179
+ */
119
180
  clone(meta) {
120
181
  const clone = Object.create(this.constructor.prototype);
121
182
  clone.assertions = [...this.assertions];
@@ -125,14 +186,44 @@ class Schema {
125
186
  };
126
187
  return clone;
127
188
  }
189
+
190
+ /**
191
+ * Appends another schema. [Link](https://github.com/bedrockio/yada#append)
192
+ * @returns {this}
193
+ */
128
194
  append(schema) {
129
195
  const merged = this.clone(schema.meta);
130
196
  merged.assertions = [...this.assertions, ...schema.assertions];
131
197
  return merged;
132
198
  }
199
+ toOpenApi(extra) {
200
+ const {
201
+ required,
202
+ format,
203
+ tags,
204
+ default: defaultValue
205
+ } = this.meta;
206
+ return {
207
+ ...(required && {
208
+ required: true
209
+ }),
210
+ ...(defaultValue && {
211
+ default: defaultValue
212
+ }),
213
+ ...(format && {
214
+ format
215
+ }),
216
+ ...this.enumToOpenApi(),
217
+ ...tags,
218
+ ...extra
219
+ };
220
+ }
133
221
 
134
222
  // Private
135
223
 
224
+ /**
225
+ * @returns {this}
226
+ */
136
227
  assertEnum(set, allow) {
137
228
  if (set.length === 1 && Array.isArray(set[0])) {
138
229
  set = set[0];
@@ -166,6 +257,10 @@ class Schema {
166
257
  }
167
258
  });
168
259
  }
260
+
261
+ /**
262
+ * @returns {this}
263
+ */
169
264
  assert(type, fn) {
170
265
  this.pushAssertion({
171
266
  halt: INITIAL_TYPES.includes(type),
@@ -181,6 +276,10 @@ class Schema {
181
276
  return this.getSortIndex(a.type) - this.getSortIndex(b.type);
182
277
  });
183
278
  }
279
+
280
+ /**
281
+ * @returns {this}
282
+ */
184
283
  transform(fn) {
185
284
  this.assert('transform', (val, options) => {
186
285
  if (val !== undefined) {
@@ -207,28 +306,6 @@ class Schema {
207
306
  throw new _errors.AssertionError(error.message, error.type || type, error);
208
307
  }
209
308
  }
210
- toOpenApi(extra) {
211
- const {
212
- required,
213
- format,
214
- tags,
215
- default: defaultValue
216
- } = this.meta;
217
- return {
218
- ...(required && {
219
- required: true
220
- }),
221
- ...(defaultValue && {
222
- default: defaultValue
223
- }),
224
- ...(format && {
225
- format
226
- }),
227
- ...this.enumToOpenApi(),
228
- ...tags,
229
- ...extra
230
- };
231
- }
232
309
  enumToOpenApi() {
233
310
  const {
234
311
  enum: allowed
@@ -29,5 +29,4 @@ class TypeSchema extends _Schema.default {
29
29
  };
30
30
  }
31
31
  }
32
- exports.default = TypeSchema;
33
- module.exports = exports.default;
32
+ exports.default = TypeSchema;
package/dist/cjs/array.js CHANGED
@@ -3,27 +3,22 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.default = void 0;
6
+ exports.default = _default;
7
7
  var _Schema = _interopRequireDefault(require("./Schema"));
8
8
  var _errors = require("./errors");
9
- var _utils = require("./utils");
10
9
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
10
  class ArraySchema extends _Schema.default {
12
- constructor(...args) {
13
- let schemas, meta;
14
- if (Array.isArray(args[0])) {
15
- schemas = args[0];
16
- meta = args[1];
17
- } else {
18
- schemas = args;
19
- }
11
+ constructor(schemas) {
20
12
  super({
21
13
  message: 'Array failed validation.',
22
- ...meta,
23
14
  schemas
24
15
  });
25
16
  this.setup();
26
17
  }
18
+
19
+ /**
20
+ * @private
21
+ */
27
22
  setup() {
28
23
  const {
29
24
  schemas
@@ -137,6 +132,18 @@ class ArraySchema extends _Schema.default {
137
132
  };
138
133
  }
139
134
  }
140
- var _default = (0, _utils.wrapSchema)(ArraySchema);
141
- exports.default = _default;
142
- module.exports = exports.default;
135
+
136
+ /**
137
+ * Creates an [array schema](https://github.com/bedrockio/yada#array).
138
+ *
139
+ * @param {...Schema} [schemas] Optional schemas to validate
140
+ * the different types of elements allowed in the array. If
141
+ * no arguments are passed elements may be of any type. Also
142
+ * accepts a single array argument.
143
+ */
144
+ function _default(...schemas) {
145
+ if (Array.isArray(schemas[0])) {
146
+ schemas = schemas[0];
147
+ }
148
+ return new ArraySchema(schemas);
149
+ }
@@ -3,10 +3,9 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.default = void 0;
6
+ exports.default = _default;
7
7
  var _TypeSchema = _interopRequireDefault(require("./TypeSchema"));
8
8
  var _errors = require("./errors");
9
- var _utils = require("./utils");
10
9
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
10
  class BooleanSchema extends _TypeSchema.default {
12
11
  constructor() {
@@ -27,6 +26,10 @@ class BooleanSchema extends _TypeSchema.default {
27
26
  });
28
27
  }
29
28
  }
30
- var _default = (0, _utils.wrapSchema)(BooleanSchema);
31
- exports.default = _default;
32
- module.exports = exports.default;
29
+
30
+ /**
31
+ * Creates a [boolean schema](https://github.com/bedrockio/yada#boolean).
32
+ */
33
+ function _default() {
34
+ return new BooleanSchema();
35
+ }
package/dist/cjs/date.js CHANGED
@@ -3,9 +3,8 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.default = void 0;
6
+ exports.default = _default;
7
7
  var _validator = _interopRequireDefault(require("validator"));
8
- var _utils = require("./utils");
9
8
  var _errors = require("./errors");
10
9
  var _Schema = _interopRequireDefault(require("./Schema"));
11
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
@@ -21,41 +20,61 @@ class DateSchema extends _Schema.default {
21
20
  }
22
21
  });
23
22
  }
23
+
24
+ /**
25
+ * @param {string|number|Date} min
26
+ */
24
27
  min(min) {
25
28
  min = new Date(min);
26
29
  return this.clone().assert('min', date => {
27
30
  if (date < min) {
28
31
  throw new _errors.LocalizedError('Must be after {date}.', {
32
+ // @ts-ignore
29
33
  date: min.toISOString()
30
34
  });
31
35
  }
32
36
  });
33
37
  }
38
+
39
+ /**
40
+ * @param {string|number|Date} max
41
+ */
34
42
  max(max) {
35
43
  max = new Date(max);
36
44
  return this.clone().assert('max', date => {
37
45
  if (date > max) {
38
46
  throw new _errors.LocalizedError('Must be before {date}.', {
47
+ // @ts-ignore
39
48
  date: max.toISOString()
40
49
  });
41
50
  }
42
51
  });
43
52
  }
53
+
54
+ /**
55
+ * @param {string|number|Date} max
56
+ */
44
57
  before(max) {
45
58
  max = new Date(max);
46
59
  return this.clone().assert('before', date => {
47
60
  if (date >= max) {
48
61
  throw new _errors.LocalizedError('Must be before {date}.', {
62
+ // @ts-ignore
49
63
  date: max.toISOString()
50
64
  });
51
65
  }
52
66
  });
53
67
  }
68
+
69
+ /**
70
+ * @param {string|number|Date} min
71
+ */
54
72
  after(min) {
55
73
  min = new Date(min);
56
74
  return this.clone().assert('after', date => {
57
75
  if (date <= min) {
58
76
  throw new _errors.LocalizedError('Must be after {date}.', {
77
+ // @ts-ignore
59
78
  date: min.toISOString()
60
79
  });
61
80
  }
@@ -77,6 +96,10 @@ class DateSchema extends _Schema.default {
77
96
  }
78
97
  });
79
98
  }
99
+
100
+ /**
101
+ * @param {"date" | "date-time"} format
102
+ */
80
103
  iso(format = 'date-time') {
81
104
  return this.clone({
82
105
  format
@@ -128,6 +151,10 @@ class DateSchema extends _Schema.default {
128
151
  };
129
152
  }
130
153
  }
131
- var _default = (0, _utils.wrapSchema)(DateSchema);
132
- exports.default = _default;
133
- module.exports = exports.default;
154
+
155
+ /**
156
+ * Creates a [date schema](https://github.com/bedrockio/yada#date).
157
+ */
158
+ function _default() {
159
+ return new DateSchema();
160
+ }
package/dist/cjs/index.js CHANGED
@@ -3,21 +3,116 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ Object.defineProperty(exports, "LocalizedError", {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _errors.LocalizedError;
10
+ }
11
+ });
12
+ exports.allow = allow;
13
+ exports.any = any;
14
+ Object.defineProperty(exports, "array", {
15
+ enumerable: true,
16
+ get: function () {
17
+ return _array.default;
18
+ }
19
+ });
20
+ Object.defineProperty(exports, "boolean", {
21
+ enumerable: true,
22
+ get: function () {
23
+ return _boolean.default;
24
+ }
25
+ });
26
+ exports.custom = custom;
27
+ Object.defineProperty(exports, "date", {
28
+ enumerable: true,
29
+ get: function () {
30
+ return _date.default;
31
+ }
32
+ });
6
33
  exports.default = void 0;
34
+ Object.defineProperty(exports, "getLocalizedMessages", {
35
+ enumerable: true,
36
+ get: function () {
37
+ return _localization.getLocalizedMessages;
38
+ }
39
+ });
40
+ Object.defineProperty(exports, "isSchema", {
41
+ enumerable: true,
42
+ get: function () {
43
+ return _utils.isSchema;
44
+ }
45
+ });
46
+ Object.defineProperty(exports, "isSchemaError", {
47
+ enumerable: true,
48
+ get: function () {
49
+ return _utils.isSchemaError;
50
+ }
51
+ });
52
+ Object.defineProperty(exports, "number", {
53
+ enumerable: true,
54
+ get: function () {
55
+ return _number.default;
56
+ }
57
+ });
58
+ Object.defineProperty(exports, "object", {
59
+ enumerable: true,
60
+ get: function () {
61
+ return _object.default;
62
+ }
63
+ });
64
+ exports.reject = reject;
65
+ Object.defineProperty(exports, "string", {
66
+ enumerable: true,
67
+ get: function () {
68
+ return _string.default;
69
+ }
70
+ });
71
+ Object.defineProperty(exports, "useLocalizer", {
72
+ enumerable: true,
73
+ get: function () {
74
+ return _localization.useLocalizer;
75
+ }
76
+ });
7
77
  var _array = _interopRequireDefault(require("./array"));
8
78
  var _boolean = _interopRequireDefault(require("./boolean"));
9
79
  var _date = _interopRequireDefault(require("./date"));
10
80
  var _number = _interopRequireDefault(require("./number"));
11
81
  var _object = _interopRequireDefault(require("./object"));
12
82
  var _string = _interopRequireDefault(require("./string"));
83
+ var _Schema = _interopRequireDefault(require("./Schema"));
13
84
  var _utils = require("./utils");
14
85
  var _localization = require("./localization");
15
86
  var _errors = require("./errors");
16
87
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
- const allow = (0, _utils.wrapArgs)('allow');
18
- const reject = (0, _utils.wrapArgs)('reject');
19
- const custom = (0, _utils.wrapArgs)('custom');
20
- const any = (0, _utils.wrapAny)();
88
+ /**
89
+ * Accepts anything.
90
+ */
91
+ function any() {
92
+ return new _Schema.default();
93
+ }
94
+
95
+ /**
96
+ * Accept values or schemas. [Link](https://github.com/bedrockio/yada#allow)
97
+ */
98
+ function allow(...args) {
99
+ return new _Schema.default().allow(...args);
100
+ }
101
+
102
+ /**
103
+ * Reject values or schemas. [Link](https://github.com/bedrockio/yada#reject)
104
+ */
105
+ function reject(...args) {
106
+ return new _Schema.default().reject(...args);
107
+ }
108
+
109
+ /**
110
+ * Validate by a custom function. [Link](https://github.com/bedrockio/yada#custom)
111
+ * @param {import("./Schema").CustomSignature} args
112
+ */
113
+ function custom(...args) {
114
+ return new _Schema.default().custom(...args);
115
+ }
21
116
  var _default = {
22
117
  array: _array.default,
23
118
  boolean: _boolean.default,
@@ -32,8 +127,7 @@ var _default = {
32
127
  isSchema: _utils.isSchema,
33
128
  isSchemaError: _utils.isSchemaError,
34
129
  useLocalizer: _localization.useLocalizer,
35
- getLocalizerTemplates: _localization.getLocalizerTemplates,
130
+ getLocalizedMessages: _localization.getLocalizedMessages,
36
131
  LocalizedError: _errors.LocalizedError
37
132
  };
38
- exports.default = _default;
39
- module.exports = exports.default;
133
+ exports.default = _default;
@@ -4,38 +4,54 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.getLocalized = getLocalized;
7
- exports.getLocalizerTemplates = getLocalizerTemplates;
7
+ exports.getLocalizedMessages = getLocalizedMessages;
8
8
  exports.localize = localize;
9
9
  exports.useLocalizer = useLocalizer;
10
10
  const TOKEN_REG = /{(.+?)}/g;
11
11
  let localizer;
12
- let templates = {};
12
+
13
+ /**
14
+ * @type {{ [key: string]: string }}
15
+ */
16
+ let messages = {};
17
+
18
+ /**
19
+ * @param {{ [key: string]: string } | ((key: string) => string)} arg
20
+ * An object that maps messages to localized strings or a function that
21
+ * accepts a message and returns a localized string. Use "getLocalizedMessages"
22
+ * to see the messages that exist.
23
+ */
13
24
  function useLocalizer(arg) {
14
- const fn = typeof arg === 'function' ? arg : template => arg[template];
25
+ const fn = typeof arg === 'function' ? arg : message => arg[message];
15
26
  localizer = fn;
16
- templates = {};
27
+ messages = {};
17
28
  }
18
- function getLocalized(template) {
29
+ function getLocalized(message) {
19
30
  if (localizer) {
20
- return localizer(template);
31
+ return localizer(message);
21
32
  }
22
33
  }
23
- function localize(template, values = {}) {
24
- let message = template;
25
- if (localizer) {
26
- let localized = getLocalized(template);
34
+ function localize(message, values = {}) {
35
+ let str = message;
36
+ if (str) {
37
+ let localized = getLocalized(message);
27
38
  if (typeof localized === 'function') {
28
39
  localized = localized(values);
29
40
  }
30
41
  if (localized) {
31
- message = localized;
42
+ str = localized;
32
43
  }
33
44
  }
34
- templates[template] = message;
35
- return message.replace(TOKEN_REG, (match, token) => {
45
+ messages[message] = str;
46
+ return str.replace(TOKEN_REG, (match, token) => {
36
47
  return token in values ? values[token] : match;
37
48
  });
38
49
  }
39
- function getLocalizerTemplates() {
40
- return templates;
50
+
51
+ /**
52
+ * Returns an object containing all encountered messages
53
+ * mapped to their localizations.
54
+ */
55
+ function getLocalizedMessages() {
56
+ return messages;
41
57
  }