@bedrockio/yada 1.0.5 → 1.0.6

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
@@ -9,6 +9,7 @@ var _utils = require("./utils");
9
9
  var _password = require("./password");
10
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
11
  const SLUG_REG = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
12
+ const PHONE_REG = /^\+?[1-9]\d{1,14}$/;
12
13
  class StringSchema extends _TypeSchema.default {
13
14
  constructor() {
14
15
  super(String);
@@ -90,6 +91,13 @@ class StringSchema extends _TypeSchema.default {
90
91
  }
91
92
  });
92
93
  }
94
+ phone() {
95
+ return this.format('phone', str => {
96
+ if (!PHONE_REG.test(str)) {
97
+ throw new _errors.LocalizedError('Must be a valid phone number.');
98
+ }
99
+ });
100
+ }
93
101
  hex() {
94
102
  return this.format('hex', str => {
95
103
  if (!_validator.default.isHexadecimal(str)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/yada",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "description": "Validation library inspired by Joi.",
5
5
  "scripts": {
6
6
  "test": "NODE_OPTIONS='--experimental-vm-modules --no-warnings' jest",
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();