joi 14.2.0 → 17.2.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.
Files changed (59) hide show
  1. package/LICENSE.md +10 -0
  2. package/README.md +9 -118
  3. package/dist/joi-browser.min.js +1 -0
  4. package/lib/annotate.js +175 -0
  5. package/lib/base.js +1068 -0
  6. package/lib/cache.js +143 -0
  7. package/lib/common.js +216 -0
  8. package/lib/compile.js +283 -0
  9. package/lib/errors.js +160 -269
  10. package/lib/extend.js +312 -0
  11. package/lib/index.d.ts +2200 -0
  12. package/lib/index.js +179 -347
  13. package/lib/manifest.js +476 -0
  14. package/lib/messages.js +178 -0
  15. package/lib/modify.js +267 -0
  16. package/lib/ref.js +386 -25
  17. package/lib/schemas.js +291 -15
  18. package/lib/state.js +152 -0
  19. package/lib/template.js +427 -0
  20. package/lib/trace.js +346 -0
  21. package/lib/types/alternatives.js +329 -0
  22. package/lib/types/any.js +174 -0
  23. package/lib/types/array.js +775 -0
  24. package/lib/types/binary.js +98 -0
  25. package/lib/types/boolean.js +150 -0
  26. package/lib/types/date.js +233 -0
  27. package/lib/types/function.js +93 -0
  28. package/lib/types/keys.js +1043 -0
  29. package/lib/types/link.js +168 -0
  30. package/lib/types/number.js +335 -0
  31. package/lib/types/object.js +22 -0
  32. package/lib/types/string.js +820 -0
  33. package/lib/types/symbol.js +102 -0
  34. package/lib/validator.js +650 -0
  35. package/lib/values.js +263 -0
  36. package/package.json +34 -29
  37. package/CHANGELOG.md +0 -3
  38. package/LICENSE +0 -25
  39. package/lib/cast.js +0 -64
  40. package/lib/language.js +0 -166
  41. package/lib/set.js +0 -191
  42. package/lib/types/alternatives/index.js +0 -218
  43. package/lib/types/any/index.js +0 -978
  44. package/lib/types/any/settings.js +0 -36
  45. package/lib/types/array/index.js +0 -707
  46. package/lib/types/binary/index.js +0 -100
  47. package/lib/types/boolean/index.js +0 -100
  48. package/lib/types/date/index.js +0 -182
  49. package/lib/types/func/index.js +0 -90
  50. package/lib/types/lazy/index.js +0 -82
  51. package/lib/types/number/index.js +0 -248
  52. package/lib/types/object/index.js +0 -957
  53. package/lib/types/state.js +0 -11
  54. package/lib/types/string/index.js +0 -703
  55. package/lib/types/string/ip.js +0 -54
  56. package/lib/types/string/rfc3986.js +0 -219
  57. package/lib/types/string/uri.js +0 -46
  58. package/lib/types/symbol/index.js +0 -93
  59. package/lib/types/symbols.js +0 -5
@@ -0,0 +1,174 @@
1
+ 'use strict';
2
+
3
+ const Assert = require('@hapi/hoek/lib/assert');
4
+
5
+ const Base = require('../base');
6
+ const Common = require('../common');
7
+ const Messages = require('../messages');
8
+
9
+
10
+ const internals = {};
11
+
12
+
13
+ module.exports = Base.extend({
14
+
15
+ type: 'any',
16
+
17
+ flags: {
18
+
19
+ only: { default: false }
20
+ },
21
+
22
+ terms: {
23
+
24
+ alterations: { init: null },
25
+ examples: { init: null },
26
+ externals: { init: null },
27
+ metas: { init: [] },
28
+ notes: { init: [] },
29
+ shared: { init: null },
30
+ tags: { init: [] },
31
+ whens: { init: null }
32
+ },
33
+
34
+ rules: {
35
+
36
+ custom: {
37
+ method(method, description) {
38
+
39
+ Assert(typeof method === 'function', 'Method must be a function');
40
+ Assert(description === undefined || description && typeof description === 'string', 'Description must be a non-empty string');
41
+
42
+ return this.$_addRule({ name: 'custom', args: { method, description } });
43
+ },
44
+ validate(value, helpers, { method }) {
45
+
46
+ try {
47
+ return method(value, helpers);
48
+ }
49
+ catch (err) {
50
+ return helpers.error('any.custom', { error: err });
51
+ }
52
+ },
53
+ args: ['method', 'description'],
54
+ multi: true
55
+ },
56
+
57
+ messages: {
58
+ method(messages) {
59
+
60
+ return this.prefs({ messages });
61
+ }
62
+ },
63
+
64
+ shared: {
65
+ method(schema) {
66
+
67
+ Assert(Common.isSchema(schema) && schema._flags.id, 'Schema must be a schema with an id');
68
+
69
+ const obj = this.clone();
70
+ obj.$_terms.shared = obj.$_terms.shared || [];
71
+ obj.$_terms.shared.push(schema);
72
+ obj.$_mutateRegister(schema);
73
+ return obj;
74
+ }
75
+ },
76
+
77
+ warning: {
78
+ method(code, local) {
79
+
80
+ Assert(code && typeof code === 'string', 'Invalid warning code');
81
+
82
+ return this.$_addRule({ name: 'warning', args: { code, local }, warn: true });
83
+ },
84
+ validate(value, helpers, { code, local }) {
85
+
86
+ return helpers.error(code, local);
87
+ },
88
+ args: ['code', 'local'],
89
+ multi: true
90
+ }
91
+ },
92
+
93
+ modifiers: {
94
+
95
+ keep(rule, enabled = true) {
96
+
97
+ rule.keep = enabled;
98
+ },
99
+
100
+ message(rule, message) {
101
+
102
+ rule.message = Messages.compile(message);
103
+ },
104
+
105
+ warn(rule, enabled = true) {
106
+
107
+ rule.warn = enabled;
108
+ }
109
+ },
110
+
111
+ manifest: {
112
+
113
+ build(obj, desc) {
114
+
115
+ for (const key in desc) {
116
+ const values = desc[key];
117
+
118
+ if (['examples', 'externals', 'metas', 'notes', 'tags'].includes(key)) {
119
+ for (const value of values) {
120
+ obj = obj[key.slice(0, -1)](value);
121
+ }
122
+
123
+ continue;
124
+ }
125
+
126
+ if (key === 'alterations') {
127
+ const alter = {};
128
+ for (const { target, adjuster } of values) {
129
+ alter[target] = adjuster;
130
+ }
131
+
132
+ obj = obj.alter(alter);
133
+ continue;
134
+ }
135
+
136
+ if (key === 'whens') {
137
+ for (const value of values) {
138
+ const { ref, is, not, then, otherwise, concat } = value;
139
+ if (concat) {
140
+ obj = obj.concat(concat);
141
+ }
142
+ else if (ref) {
143
+ obj = obj.when(ref, { is, not, then, otherwise, switch: value.switch, break: value.break });
144
+ }
145
+ else {
146
+ obj = obj.when(is, { then, otherwise, break: value.break });
147
+ }
148
+ }
149
+
150
+ continue;
151
+ }
152
+
153
+ if (key === 'shared') {
154
+ for (const value of values) {
155
+ obj = obj.shared(value);
156
+ }
157
+ }
158
+ }
159
+
160
+ return obj;
161
+ }
162
+ },
163
+
164
+ messages: {
165
+ 'any.custom': '{{#label}} failed custom validation because {{#error.message}}',
166
+ 'any.default': '{{#label}} threw an error when running default method',
167
+ 'any.failover': '{{#label}} threw an error when running failover method',
168
+ 'any.invalid': '{{#label}} contains an invalid value',
169
+ 'any.only': '{{#label}} must be {if(#valids.length == 1, "", "one of ")}{{#valids}}',
170
+ 'any.ref': '{{#label}} {{#arg}} references {{:#ref}} which {{#reason}}',
171
+ 'any.required': '{{#label}} is required',
172
+ 'any.unknown': '{{#label}} is not allowed'
173
+ }
174
+ });