@bedrockio/yada 1.0.5 → 1.0.7

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/.eslintignore ADDED
@@ -0,0 +1 @@
1
+ dist
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
 
3
- exports.__esModule = true;
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
4
6
  exports.default = void 0;
5
7
  exports.isSchema = isSchema;
6
8
  var _errors = require("./errors");
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
 
3
- exports.__esModule = true;
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
4
6
  exports.default = void 0;
5
7
  var _Schema = _interopRequireDefault(require("./Schema"));
6
8
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
package/dist/cjs/array.js CHANGED
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
 
3
- exports.__esModule = true;
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
4
6
  exports.default = void 0;
5
7
  var _Schema = _interopRequireDefault(require("./Schema"));
6
8
  var _errors = require("./errors");
@@ -60,6 +62,17 @@ class ArraySchema extends _Schema.default {
60
62
  });
61
63
  }
62
64
  }
65
+ length(length) {
66
+ return this.clone().assert('length', arr => {
67
+ if (arr.length !== length) {
68
+ const s = length === 1 ? '' : 's';
69
+ throw new _errors.LocalizedError('Must contain exactly {length} element{s}.', {
70
+ length,
71
+ s
72
+ });
73
+ }
74
+ });
75
+ }
63
76
  min(length) {
64
77
  return this.clone().assert('length', arr => {
65
78
  if (arr.length < length) {
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
 
3
- exports.__esModule = true;
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
4
6
  exports.default = void 0;
5
7
  var _TypeSchema = _interopRequireDefault(require("./TypeSchema"));
6
8
  var _errors = require("./errors");
package/dist/cjs/date.js CHANGED
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
 
3
- exports.__esModule = true;
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
4
6
  exports.default = void 0;
5
7
  var _validator = _interopRequireDefault(require("validator"));
6
8
  var _utils = require("./utils");
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
 
3
- exports.__esModule = true;
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
4
6
  exports.ValidationError = exports.LocalizedError = exports.FieldError = exports.ElementError = exports.AssertionError = exports.ArrayError = void 0;
5
7
  exports.isSchemaError = isSchemaError;
6
8
  var _messages = require("./messages");
package/dist/cjs/index.js CHANGED
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
 
3
- exports.__esModule = true;
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
4
6
  exports.default = void 0;
5
7
  var _array = _interopRequireDefault(require("./array"));
6
8
  var _boolean = _interopRequireDefault(require("./boolean"));
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
 
3
- exports.__esModule = true;
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
4
6
  exports.getLocalized = getLocalized;
5
7
  exports.getLocalizerTemplates = getLocalizerTemplates;
6
8
  exports.localize = localize;
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
 
3
- exports.__esModule = true;
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
4
6
  exports.getFullMessage = getFullMessage;
5
7
  var _errors = require("./errors");
6
8
  var _localization = require("./localization");
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
 
3
- exports.__esModule = true;
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
4
6
  exports.default = void 0;
5
7
  var _TypeSchema = _interopRequireDefault(require("./TypeSchema"));
6
8
  var _errors = require("./errors");
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
 
3
- exports.__esModule = true;
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
4
6
  exports.default = void 0;
5
7
  var _TypeSchema = _interopRequireDefault(require("./TypeSchema"));
6
8
  var _errors = require("./errors");
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
 
3
- exports.__esModule = true;
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
4
6
  exports.PASSWORD_DEFAULTS = void 0;
5
7
  exports.validateLength = validateLength;
6
8
  exports.validateUppercase = exports.validateSymbols = exports.validateNumbers = exports.validateLowercase = void 0;
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
 
3
- exports.__esModule = true;
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
4
6
  exports.default = void 0;
5
7
  var _validator = _interopRequireDefault(require("validator"));
6
8
  var _TypeSchema = _interopRequireDefault(require("./TypeSchema"));
@@ -9,6 +11,7 @@ var _utils = require("./utils");
9
11
  var _password = require("./password");
10
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
13
  const SLUG_REG = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
14
+ const PHONE_REG = /^\+?[1-9]\d{1,14}$/;
12
15
  class StringSchema extends _TypeSchema.default {
13
16
  constructor() {
14
17
  super(String);
@@ -90,6 +93,13 @@ class StringSchema extends _TypeSchema.default {
90
93
  }
91
94
  });
92
95
  }
96
+ phone() {
97
+ return this.format('phone', str => {
98
+ if (!PHONE_REG.test(str)) {
99
+ throw new _errors.LocalizedError('Must be a valid phone number.');
100
+ }
101
+ });
102
+ }
93
103
  hex() {
94
104
  return this.format('hex', str => {
95
105
  if (!_validator.default.isHexadecimal(str)) {
package/dist/cjs/utils.js CHANGED
@@ -1,14 +1,25 @@
1
1
  "use strict";
2
2
 
3
- exports.__esModule = true;
4
- exports.isSchemaError = exports.isSchema = void 0;
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "isSchema", {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _Schema.isSchema;
10
+ }
11
+ });
12
+ Object.defineProperty(exports, "isSchemaError", {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _errors.isSchemaError;
16
+ }
17
+ });
5
18
  exports.wrapAny = wrapAny;
6
19
  exports.wrapArgs = wrapArgs;
7
20
  exports.wrapSchema = wrapSchema;
8
21
  var _Schema = _interopRequireWildcard(require("./Schema"));
9
- exports.isSchema = _Schema.isSchema;
10
22
  var _errors = require("./errors");
11
- exports.isSchemaError = _errors.isSchemaError;
12
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); }
13
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; }
14
25
  function wrapSchema(Class) {
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@bedrockio/yada",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "Validation library inspired by Joi.",
5
5
  "scripts": {
6
- "test": "NODE_OPTIONS='--experimental-vm-modules --no-warnings' jest",
6
+ "test": "jest",
7
7
  "lint": "eslint",
8
8
  "build": "scripts/build",
9
9
  "prepublishOnly": "npm run build"
package/src/array.js CHANGED
@@ -59,6 +59,18 @@ class ArraySchema extends Schema {
59
59
  }
60
60
  }
61
61
 
62
+ length(length) {
63
+ return this.clone().assert('length', (arr) => {
64
+ if (arr.length !== length) {
65
+ const s = length === 1 ? '' : 's';
66
+ throw new LocalizedError('Must contain exactly {length} element{s}.', {
67
+ length,
68
+ s,
69
+ });
70
+ }
71
+ });
72
+ }
73
+
62
74
  min(length) {
63
75
  return this.clone().assert('length', (arr) => {
64
76
  if (arr.length < length) {
package/src/string.js CHANGED
@@ -12,6 +12,7 @@ import {
12
12
  } from './password';
13
13
 
14
14
  const SLUG_REG = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
15
+ const PHONE_REG = /^\+?[1-9]\d{1,14}$/;
15
16
 
16
17
  class StringSchema extends TypeSchema {
17
18
  constructor() {
@@ -98,6 +99,14 @@ class StringSchema extends TypeSchema {
98
99
  });
99
100
  }
100
101
 
102
+ phone() {
103
+ return this.format('phone', (str) => {
104
+ if (!PHONE_REG.test(str)) {
105
+ throw new LocalizedError('Must be a valid phone number.');
106
+ }
107
+ });
108
+ }
109
+
101
110
  hex() {
102
111
  return this.format('hex', (str) => {
103
112
  if (!validator.isHexadecimal(str)) {
package/test/all.test.js CHANGED
@@ -83,6 +83,14 @@ describe('string', () => {
83
83
  await assertFail(schema, 'foo@bar', ['Must be an email address.']);
84
84
  });
85
85
 
86
+ it('should validate an E.164 phone number', async () => {
87
+ const schema = yd.string().phone();
88
+ await assertPass(schema, undefined);
89
+ await assertPass(schema, '+16175551212');
90
+ await assertFail(schema, '+1', ['Must be a valid phone number.']);
91
+ await assertFail(schema, 'foo', ['Must be a valid phone number.']);
92
+ });
93
+
86
94
  it('should validate a regex pattern', async () => {
87
95
  expect(() => {
88
96
  yd.string().match();
@@ -762,6 +770,13 @@ describe('array', () => {
762
770
  await assertFail(schema, [{ bar: 'hi' }], ['Unknown field "bar".']);
763
771
  });
764
772
 
773
+ it('should validate a fixed length', async () => {
774
+ const schema = yd.array().length(2);
775
+ await assertFail(schema, [1], ['Must contain exactly 2 elements.']);
776
+ await assertPass(schema, [1, 2]);
777
+ await assertFail(schema, [1, 2, 3], ['Must contain exactly 2 elements.']);
778
+ });
779
+
765
780
  it('should validate a minimum length', async () => {
766
781
  const schema = yd.array().min(1);
767
782
  await assertPass(schema, ['one']);