@orion-js/schema 3.2.21 → 3.2.33

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.
@@ -6,14 +6,18 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const fieldType_1 = __importDefault(require("../fieldType"));
7
7
  const isString_1 = __importDefault(require("lodash/isString"));
8
8
  const Errors_1 = __importDefault(require("../Errors"));
9
+ const isNil_1 = __importDefault(require("lodash/isNil"));
9
10
  exports.default = (0, fieldType_1.default)({
10
11
  name: 'email',
11
- validate(value) {
12
- if (!(0, isString_1.default)(value))
12
+ validate(value, { currentSchema }) {
13
+ if ((value === '' || (0, isNil_1.default)(value)) && !currentSchema.optional) {
14
+ return Errors_1.default.REQUIRED;
15
+ }
16
+ if (value && !(0, isString_1.default)(value))
13
17
  return Errors_1.default.NOT_A_STRING;
14
18
  // eslint-disable-next-line
15
- const regex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
16
- if (!regex.test(value))
19
+ const regex = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
20
+ if (value && !regex.test(value))
17
21
  return Errors_1.default.NOT_AN_EMAIL;
18
22
  },
19
23
  clean(value) {
@@ -6,13 +6,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const email_1 = __importDefault(require("./email"));
7
7
  const Errors_1 = __importDefault(require("../Errors"));
8
8
  test('return an error when the value is incorrect', async () => {
9
- //@ts-ignore
10
9
  expect(email_1.default.validate(['Hello'])).toBe(Errors_1.default.NOT_A_STRING);
11
- //@ts-ignore
12
10
  expect(email_1.default.validate({ name: 'Nicolás' })).toBe(Errors_1.default.NOT_A_STRING);
13
- //@ts-ignore
14
11
  expect(email_1.default.validate(new Date())).toBe(Errors_1.default.NOT_A_STRING);
15
12
  expect(email_1.default.validate('astring')).toBe(Errors_1.default.NOT_AN_EMAIL);
13
+ expect(email_1.default.validate(null)).toBe(Errors_1.default.REQUIRED);
14
+ expect(email_1.default.validate('')).toBe(Errors_1.default.REQUIRED);
15
+ expect(email_1.default.validate('', { currentSchema: { optional: true } })).toBeFalsy();
16
+ expect(email_1.default.validate(null, { currentSchema: { optional: true } })).toBeFalsy();
16
17
  });
17
18
  test('return no error when the value is correct', async () => {
18
19
  expect(email_1.default.validate('aemail@email.com')).toBeFalsy();
@@ -6,11 +6,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const string_1 = __importDefault(require("./string"));
7
7
  const Errors_1 = __importDefault(require("../Errors"));
8
8
  test('return an error when the value is incorrect', async () => {
9
- //@ts-ignore
10
9
  expect(string_1.default.validate(['Hello'])).toBe(Errors_1.default.NOT_A_STRING);
11
- //@ts-ignore
12
10
  expect(string_1.default.validate({ name: 'Nicolás' })).toBe(Errors_1.default.NOT_A_STRING);
13
- //@ts-ignore
14
11
  expect(string_1.default.validate(new Date())).toBe(Errors_1.default.NOT_A_STRING);
15
12
  });
16
13
  test('return no error when the value is correct', async () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orion-js/schema",
3
- "version": "3.2.21",
3
+ "version": "3.2.33",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "files": [
@@ -36,5 +36,5 @@
36
36
  "publishConfig": {
37
37
  "access": "public"
38
38
  },
39
- "gitHead": "160e776b7a8448c8e4f67cae79849d6520eaf0c1"
39
+ "gitHead": "3b0bf99ff75e304ce8fc0e8d9d766039cdbcc624"
40
40
  }