-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix validation of object IDs (#348)
- Loading branch information
Showing
7 changed files
with
155 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,43 @@ func validateBadStructure(t *testing.T, validator func(string) bool) { | |
assert.False(t, validator("item:**")) | ||
} | ||
|
||
func TestValidateObjectID(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
value string | ||
expected bool | ||
}{ | ||
{"document1", true}, // Should pass valid ID | ||
{"doc_123", true}, // Should pass valid ID with underscore | ||
{"[email protected]", true}, // Should pass valid email-like ID | ||
{"file.name", true}, // Should pass valid ID with dot | ||
{"data+set", true}, // Should pass valid ID with plus | ||
{"pipe|char", true}, // Should pass valid ID with pipe | ||
{"star*char", true}, // Should pass valid ID with star | ||
{"underscore_", true}, // Should pass valid ID with underscore | ||
{"pipe|[email protected]", true}, // Should pass valid complex ID | ||
{"#document1", false}, // Should fail if starts with # | ||
{":doc123", false}, // Should fail if starts with : | ||
{" doc123", false}, // Should fail if starts with space | ||
{"doc*123", true}, // Should pass valid ID with star | ||
{"doc:123", false}, // Should fail if contains : | ||
{"doc#123", false}, // Should fail if contains # | ||
{"doc 123", false}, // Should fail if contains space | ||
{"doc*", true}, // Should pass valid ID with star | ||
{"doc:", false}, // Should fail if ends with : | ||
{" doc", false}, // Should fail if starts with space | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.value, func(t *testing.T) { | ||
t.Parallel() | ||
assert.Equal(t, test.expected, ValidateObjectID(test.value)) | ||
}) | ||
} | ||
|
||
} | ||
|
||
func TestValidateObject(t *testing.T) { | ||
t.Parallel() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,30 @@ public void ruleObjectTest() { | |
validatedBadStructure(Validator::validateObject); | ||
} | ||
|
||
@Test | ||
public void testValidateObjectId() { | ||
// Valid cases | ||
assertTrue(Validator.Regexes.objectId.matches("document1")); | ||
assertTrue(Validator.Regexes.objectId.matches("doc_123")); | ||
assertTrue(Validator.Regexes.objectId.matches("[email protected]")); | ||
assertTrue(Validator.Regexes.objectId.matches("file.name")); | ||
assertTrue(Validator.Regexes.objectId.matches("data+set")); | ||
assertTrue(Validator.Regexes.objectId.matches("pipe|char")); | ||
assertTrue(Validator.Regexes.objectId.matches("star*char")); | ||
assertTrue(Validator.Regexes.objectId.matches("underscore_")); | ||
assertTrue(Validator.Regexes.objectId.matches("pipe|[email protected]")); | ||
|
||
// Invalid cases | ||
assertFalse(Validator.Regexes.objectId.matches("#document1")); | ||
assertFalse(Validator.Regexes.objectId.matches(":doc123")); | ||
assertFalse(Validator.Regexes.objectId.matches(" doc123")); | ||
assertFalse(Validator.Regexes.objectId.matches("doc:123")); | ||
assertFalse(Validator.Regexes.objectId.matches("doc#123")); | ||
assertFalse(Validator.Regexes.objectId.matches("doc 123")); | ||
assertFalse(Validator.Regexes.objectId.matches("doc:")); | ||
assertFalse(Validator.Regexes.objectId.matches(" doc")); | ||
} | ||
|
||
@Test | ||
public void ruleUserTest() { | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -172,4 +172,78 @@ describe("Validation Rules", () => { | |
|
||
validatedBadStructure(Validator.type); | ||
}); | ||
|
||
describe("Rule 'id'", () => { | ||
it("should pass 'document1'", () => { | ||
expect(Validator.objectId("document1")).toBeTruthy(); | ||
}); | ||
|
||
it("should pass 'doc_123'", () => { | ||
expect(Validator.objectId("doc_123")).toBeTruthy(); | ||
}); | ||
|
||
it("should pass '[email protected]'", () => { | ||
expect(Validator.objectId("[email protected]")).toBeTruthy(); | ||
}); | ||
|
||
it("should pass 'file.name'", () => { | ||
expect(Validator.objectId("file.name")).toBeTruthy(); | ||
}); | ||
|
||
it("should pass 'data+set'", () => { | ||
expect(Validator.objectId("data+set")).toBeTruthy(); | ||
}); | ||
|
||
it("should pass 'pipe|char'", () => { | ||
expect(Validator.objectId("pipe|char")).toBeTruthy(); | ||
}); | ||
|
||
it("should pass 'star*char'", () => { | ||
expect(Validator.objectId("star*char")).toBeTruthy(); | ||
}); | ||
|
||
it("should pass 'underscore_'", () => { | ||
expect(Validator.objectId("underscore_")).toBeTruthy(); | ||
}); | ||
|
||
it("should pass 'pipe|[email protected]'", () => { | ||
expect(Validator.objectId("pipe|[email protected]")).toBeTruthy(); | ||
}); | ||
|
||
it("should fail '#document1'", () => { | ||
expect(Validator.objectId("#document1")).toBeFalsy(); | ||
}); | ||
|
||
it("should fail ':doc123'", () => { | ||
expect(Validator.objectId(":doc123")).toBeFalsy(); | ||
}); | ||
|
||
it("should fail ' doc123'", () => { | ||
expect(Validator.objectId(" doc123")).toBeFalsy(); | ||
}); | ||
|
||
it("should fail 'doc*123'", () => { | ||
expect(Validator.objectId("doc*123")).toBeTruthy(); | ||
}); | ||
|
||
it("should fail 'doc:123'", () => { | ||
expect(Validator.objectId("doc:123")).toBeFalsy(); | ||
}); | ||
|
||
it("should fail 'doc#123'", () => { | ||
expect(Validator.objectId("doc#123")).toBeFalsy(); | ||
}); | ||
|
||
it("should fail 'doc 123'", () => { | ||
expect(Validator.objectId("doc 123")).toBeFalsy(); | ||
}); | ||
|
||
it("should fail 'doc*'", () => { | ||
expect(Validator.objectId("doc*")).toBeTruthy(); | ||
}); | ||
|
||
it("should fail 'doc:'", () => { | ||
expect(Validator.objectId("doc:")).toBeFalsy(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters