Skip to content

Commit

Permalink
feat(lanuage): added objectId regex for go and java
Browse files Browse the repository at this point in the history
  • Loading branch information
Talent Zeng committed Sep 19, 2024
1 parent d7bf0c3 commit bf193c4
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
8 changes: 7 additions & 1 deletion pkg/go/validation/validation-rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const (
RuleType Rule = "[^:#@\\*\\s]{1,254}"
RuleRelation Rule = "[^:#@\\*\\s]{1,50}"
RuleCondition Rule = "[^\\*\\s]{1,50}"
RuleID Rule = "[^#:\\*\\s]+"
RuleID Rule = "[^#:\\s*][a-zA-Z0-9_|*@.+]*"
RuleObject Rule = "[^\\s]{2,256}"
)

Expand All @@ -22,6 +22,12 @@ func ValidateObject(object string) bool {
return typeMatch && objectMatch
}

func ValidateObjectId(relation string) bool {
match, _ := regexp.MatchString(fmt.Sprintf("^%s$", RuleID), relation)

return match
}

func ValidateRelation(relation string) bool {
match, _ := regexp.MatchString(fmt.Sprintf("^%s$", RuleRelation), relation)

Expand Down
36 changes: 36 additions & 0 deletions pkg/go/validation/validation-rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,42 @@ 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
{"user@domain.com", 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|underscore_@domain.com", 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) {
assert.Equal(t, test.expected, ValidateObjectId(test.value))
})
}

}

func TestValidateObject(t *testing.T) {
t.Parallel()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ public class Rules {
public static final String TYPE = "[^:#@\\*\\s]{1,254}";
public static final String RELATION = "[^:#@\\*\\s]{1,50}";
public static final String CONDITION = "[^\\*\\s]{1,50}";
public static final String ID = "[^#:\\*\\s]+";
public static final String ID = "[^#:\\s*][a-zA-Z0-9_|*@.+]*";
public static final String OBJECT = "[^\\s]{2,256}";
}

public static class Regexes {
public static final ValidationRegex object =
ValidationRegex.build("object", String.format("^%s$", Rules.OBJECT));

public static final ValidationRegex objectId =
ValidationRegex.build("object", String.format("^%s$", Rules.ID));

public static final ValidationRegex typeId =
ValidationRegex.build("object", String.format("^%s:%s$", Rules.TYPE, Rules.ID));

Expand All @@ -40,6 +43,10 @@ public static boolean validateObject(String object) {
return Regexes.typeId.matches(object) && Regexes.object.matches(object);
}

public static boolean validateObjectId(String objectId) {
return Regexes.objectId.matches(objectId);
}

public static boolean validateRelation(String relation) {
return Regexes.relation.matches(relation);
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/js/validator/validate-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export const Rules = {
type: "[^:#@\\*\\s]{1,254}",
relation: "[^:#@\\*\\s]{1,50}",
condition: "[^\\*\\s]{1,50}",
id: "(?![#:\\s*])[a-zA-Z0-9_|*@.+]+",
id: "[^#:\\s*][a-zA-Z0-9_|*@.+]*",
object: "[^\\s]{2,256}",
};

Expand Down

0 comments on commit bf193c4

Please sign in to comment.