Skip to content

Commit

Permalink
fix(openfga): added unit tests for new regex for Rules.id
Browse files Browse the repository at this point in the history
  • Loading branch information
Talent Zeng committed Sep 16, 2024
1 parent 523854d commit d7bf0c3
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
74 changes: 74 additions & 0 deletions pkg/js/tests/validate-rules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 'user@domain.com'", () => {
expect(Validator.objectId("user@domain.com")).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|underscore_@domain.com'", () => {
expect(Validator.objectId("pipe|underscore_@domain.com")).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();
});
});
});
4 changes: 4 additions & 0 deletions pkg/js/validator/validate-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export const Validator = {
type: (type: string): boolean => {
return validateFieldValue(`^${Rules.type}$`, type);
},
// ObjectId name
objectId: (id: string): boolean => {
return validateFieldValue(`^${Rules.id}$`, id);
},
};

const validateFieldValue = (rule: string, value: string): boolean => {
Expand Down

0 comments on commit d7bf0c3

Please sign in to comment.