joi 13.6.0 → 13.7.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/README.md CHANGED
@@ -16,7 +16,7 @@ Imagine you run facebook and you want visitors to sign up on the website with re
16
16
  This is joi, joi allows you to create *blueprints* or *schemas* for JavaScript objects (an object that stores information) to ensure *validation* of key information.
17
17
 
18
18
  # API
19
- See the detailed [API Reference](https://github.com/hapijs/joi/blob/v13.6.0/API.md).
19
+ See the detailed [API Reference](https://github.com/hapijs/joi/blob/v13.7.0/API.md).
20
20
 
21
21
  # Example
22
22
 
package/lib/index.js CHANGED
@@ -22,7 +22,8 @@ const internals = {
22
22
  func: require('./types/func'),
23
23
  number: require('./types/number'),
24
24
  object: require('./types/object'),
25
- string: require('./types/string')
25
+ string: require('./types/string'),
26
+ symbol: require('./types/symbol')
26
27
  };
27
28
 
28
29
  internals.callWithDefaults = function (schema, args) {
@@ -112,6 +113,13 @@ internals.root = function () {
112
113
  return internals.callWithDefaults.call(this, internals.string, args);
113
114
  };
114
115
 
116
+ root.symbol = function (...args) {
117
+
118
+ Hoek.assert(args.length === 0, 'Joi.symbol() does not allow arguments.');
119
+
120
+ return internals.callWithDefaults.call(this, internals.symbol, args);
121
+ };
122
+
115
123
  root.ref = function (...args) {
116
124
 
117
125
  return Ref.create(...args);
package/lib/language.js CHANGED
@@ -157,5 +157,9 @@ exports.errors = {
157
157
  ref: 'references "{{ref}}" which is not a number',
158
158
  ip: 'must be a valid ip address with a {{cidr}} CIDR',
159
159
  ipVersion: 'must be a valid ip address of one of the following versions {{version}} with a {{cidr}} CIDR'
160
+ },
161
+ symbol: {
162
+ base: 'must be a symbol',
163
+ map: 'must be one of {{map}}'
160
164
  }
161
165
  };
@@ -0,0 +1,93 @@
1
+ 'use strict';
2
+
3
+ // Load modules
4
+
5
+ const Util = require('util');
6
+
7
+ const Any = require('../any');
8
+ const Hoek = require('hoek');
9
+
10
+
11
+ // Declare internals
12
+
13
+ const internals = {};
14
+
15
+
16
+ internals.Map = class extends Map {
17
+
18
+ slice() {
19
+
20
+ return new internals.Map(this);
21
+ }
22
+
23
+ toString() {
24
+
25
+ return Util.inspect(this);
26
+ }
27
+ };
28
+
29
+
30
+ internals.Symbol = class extends Any {
31
+
32
+ constructor() {
33
+
34
+ super();
35
+ this._type = 'symbol';
36
+ this._inner.map = new internals.Map();
37
+ }
38
+
39
+ _base(value, state, options) {
40
+
41
+ if (options.convert) {
42
+ const lookup = this._inner.map.get(value);
43
+ if (lookup) {
44
+ value = lookup;
45
+ }
46
+
47
+ if (this._flags.allowOnly) {
48
+ return {
49
+ value,
50
+ errors: (typeof value === 'symbol') ? null : this.createError('symbol.map', { map: this._inner.map }, state, options)
51
+ };
52
+ }
53
+ }
54
+
55
+ return {
56
+ value,
57
+ errors: (typeof value === 'symbol') ? null : this.createError('symbol.base', null, state, options)
58
+ };
59
+ }
60
+
61
+ map(iterable) {
62
+
63
+ if (iterable && !iterable[Symbol.iterator] && typeof iterable === 'object') {
64
+ iterable = Object.entries(iterable);
65
+ }
66
+
67
+ Hoek.assert(iterable && iterable[Symbol.iterator], 'Iterable must be an iterable or object');
68
+ const obj = this.clone();
69
+
70
+ const symbols = [];
71
+ for (const entry of iterable) {
72
+ Hoek.assert(entry && entry[Symbol.iterator], 'Entry must be an iterable');
73
+ const [key, value] = entry;
74
+
75
+ Hoek.assert(typeof key !== 'object' && typeof key !== 'function' && typeof key !== 'symbol', 'Key must not be an object, function, or Symbol');
76
+ Hoek.assert(typeof value === 'symbol', 'Value must be a Symbol');
77
+ obj._inner.map.set(key, value);
78
+ symbols.push(value);
79
+ }
80
+
81
+ return obj.valid(...symbols);
82
+ }
83
+
84
+ describe() {
85
+
86
+ const description = super.describe();
87
+ description.map = new Map(this._inner.map);
88
+ return description;
89
+ }
90
+ };
91
+
92
+
93
+ module.exports = new internals.Symbol();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "joi",
3
3
  "description": "Object schema validation",
4
- "version": "13.6.0",
4
+ "version": "13.7.0",
5
5
  "homepage": "https://github.com/hapijs/joi",
6
6
  "repository": "git://github.com/hapijs/joi",
7
7
  "main": "lib/index.js",