opticedge-cloud-utils 1.1.26 → 1.1.27

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.
@@ -1,3 +1,4 @@
1
1
  /** Basic email validation — good enough for most production use cases */
2
2
  declare const isValidEmail: (email: string) => boolean;
3
- export { isValidEmail };
3
+ declare const isValidObjectId: (id: string) => boolean;
4
+ export { isValidEmail, isValidObjectId };
package/dist/validator.js CHANGED
@@ -1,6 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isValidEmail = void 0;
3
+ exports.isValidObjectId = exports.isValidEmail = void 0;
4
+ const mongodb_1 = require("mongodb");
4
5
  /** Basic email validation — good enough for most production use cases */
5
6
  const isValidEmail = (email) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email.trim());
6
7
  exports.isValidEmail = isValidEmail;
8
+ const isValidObjectId = (id) => {
9
+ return mongodb_1.ObjectId.isValid(id) && new mongodb_1.ObjectId(id).toHexString() === id;
10
+ };
11
+ exports.isValidObjectId = isValidObjectId;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opticedge-cloud-utils",
3
- "version": "1.1.26",
3
+ "version": "1.1.27",
4
4
  "description": "Common utilities for cloud functions",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/validator.ts CHANGED
@@ -1,4 +1,10 @@
1
+ import { ObjectId } from 'mongodb'
2
+
1
3
  /** Basic email validation — good enough for most production use cases */
2
4
  const isValidEmail = (email: string) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email.trim())
3
5
 
4
- export { isValidEmail }
6
+ const isValidObjectId = (id: string): boolean => {
7
+ return ObjectId.isValid(id) && new ObjectId(id).toHexString() === id
8
+ }
9
+
10
+ export { isValidEmail, isValidObjectId }
@@ -0,0 +1,69 @@
1
+ import { describe, it, expect } from '@jest/globals'
2
+ import { ObjectId } from 'mongodb'
3
+ import { isValidEmail, isValidObjectId } from '../src'
4
+
5
+ describe('isValidEmail', () => {
6
+ it('returns true for simple valid emails', () => {
7
+ expect(isValidEmail('test@example.com')).toBe(true)
8
+ expect(isValidEmail('user.name@domain.co')).toBe(true)
9
+ expect(isValidEmail('user_name+tag@sub.domain.org')).toBe(true)
10
+ })
11
+
12
+ it('returns false for missing parts', () => {
13
+ expect(isValidEmail('')).toBe(false)
14
+ expect(isValidEmail('plainaddress')).toBe(false)
15
+ expect(isValidEmail('@no-local-part.com')).toBe(false)
16
+ expect(isValidEmail('username@')).toBe(false)
17
+ expect(isValidEmail('user@domain')).toBe(false)
18
+ expect(isValidEmail('username@.com')).toBe(false)
19
+ })
20
+
21
+ it('handles whitespace correctly', () => {
22
+ expect(isValidEmail(' test@example.com ')).toBe(true)
23
+ expect(isValidEmail('\nuser@domain.com\t')).toBe(true)
24
+ })
25
+
26
+ it('rejects clearly invalid characters or formats', () => {
27
+ expect(isValidEmail('user@@domain.com')).toBe(false)
28
+ expect(isValidEmail('user@domain,com')).toBe(false)
29
+ expect(isValidEmail('user@domain com')).toBe(false)
30
+ })
31
+
32
+ it('accepts minimal valid domain structures', () => {
33
+ expect(isValidEmail('x@y.z')).toBe(true)
34
+ })
35
+ })
36
+
37
+ describe('isValidObjectId', () => {
38
+ it('returns true for valid ObjectId strings', () => {
39
+ const id = new ObjectId().toHexString()
40
+ expect(isValidObjectId(id)).toBe(true)
41
+ })
42
+
43
+ it('returns false for invalid length', () => {
44
+ expect(isValidObjectId('123')).toBe(false)
45
+ expect(isValidObjectId('12345678901234567890123')).toBe(false)
46
+ expect(isValidObjectId('1234567890123456789012345')).toBe(false)
47
+ })
48
+
49
+ it('returns false for non-hex characters', () => {
50
+ expect(isValidObjectId('zzzzzzzzzzzzzzzzzzzzzzzz')).toBe(false)
51
+ expect(isValidObjectId('12345678901234567890123g')).toBe(false)
52
+ })
53
+
54
+ it('rejects 12-byte strings that ObjectId.isValid would accept', () => {
55
+ expect(isValidObjectId('123456789012')).toBe(false)
56
+ })
57
+
58
+ it('is case-sensitive to exact hex string match', () => {
59
+ const id = new ObjectId().toHexString()
60
+ expect(isValidObjectId(id.toUpperCase())).toBe(false)
61
+ })
62
+
63
+ it('returns false for empty or malformed input', () => {
64
+ expect(isValidObjectId('')).toBe(false)
65
+ expect(isValidObjectId(' ')).toBe(false)
66
+ expect(isValidObjectId('null')).toBe(false)
67
+ expect(isValidObjectId('undefined')).toBe(false)
68
+ })
69
+ })
@@ -1,37 +0,0 @@
1
- // isValidEmail.test.ts
2
- import { describe, it, expect } from '@jest/globals'
3
- import { isValidEmail } from '../src'
4
-
5
- describe('isValidEmail', () => {
6
- it('returns true for simple valid emails', () => {
7
- expect(isValidEmail('test@example.com')).toBe(true)
8
- expect(isValidEmail('user.name@domain.co')).toBe(true)
9
- expect(isValidEmail('user_name+tag@sub.domain.org')).toBe(true)
10
- })
11
-
12
- it('returns false for missing parts', () => {
13
- expect(isValidEmail('')).toBe(false)
14
- expect(isValidEmail('plainaddress')).toBe(false)
15
- expect(isValidEmail('@no-local-part.com')).toBe(false)
16
- expect(isValidEmail('username@')).toBe(false)
17
- expect(isValidEmail('user@domain')).toBe(false)
18
- expect(isValidEmail('username@.com')).toBe(false)
19
- })
20
-
21
- it('handles whitespace correctly', () => {
22
- expect(isValidEmail(' test@example.com ')).toBe(true) // trims input
23
- expect(isValidEmail('\nuser@domain.com\t')).toBe(true)
24
- })
25
-
26
- it('rejects invalid characters or formats', () => {
27
- expect(isValidEmail('user@@domain.com')).toBe(false)
28
- expect(isValidEmail('user@domain,com')).toBe(false)
29
- expect(isValidEmail('user@domain..com')).toBe(false)
30
- expect(isValidEmail('user@.domain.com')).toBe(false)
31
- expect(isValidEmail('user@domain com')).toBe(false)
32
- })
33
-
34
- it('accepts minimal valid domain structures', () => {
35
- expect(isValidEmail('x@y.z')).toBe(true) // still valid
36
- })
37
- })