Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: moved store validation to language #286

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

public class ValidationOptions {

private static final String DEFAULT_TYPE_PATTERN = "^[^:#@\\s]{1,254}$";
private static final String DEFAULT_RELATION_PATTERN = "^[^:#@\\s]{1,50}$";
private static final String DEFAULT_TYPE_PATTERN = "^[^:#@\\*\\s]{1,254}$";
private static final String DEFAULT_RELATION_PATTERN = "^[^:#@\\*\\s]{1,50}$";

private String typePattern = DEFAULT_TYPE_PATTERN;
private String relationPattern = DEFAULT_RELATION_PATTERN;
Expand Down
80 changes: 64 additions & 16 deletions pkg/js/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
],
"author": "OpenFGA",
"dependencies": {
"ajv": "^8.16.0",
"antlr4": "^4.13.1",
"yaml": "^2.4.5"
},
Expand Down
37 changes: 37 additions & 0 deletions pkg/js/tests/_testcases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ModelValidationSingleError,
} from "../errors";
import { ModuleFile } from "../transformer";
import { ErrorObject } from "ajv";

interface ValidTestCase {
name: string;
Expand Down Expand Up @@ -60,6 +61,13 @@ interface ModuleTestCase extends ValidTestCase {
expected_errors?: BaseError[];
}

interface MultipleInvalidStoreTestCase {
name: string;
store: string;
skip?: boolean;
expected_errors: ErrorObject[];
}

export function loadValidTransformerTestCases(): ValidTestCase[] {
const testDataPath = path.join(__dirname, "../../../tests", "data", "transformer");
const entries = fs.readdirSync(testDataPath, { withFileTypes: true });
Expand Down Expand Up @@ -201,3 +209,32 @@ export function loadModuleTestCases(): ModuleTestCase[] {

return testCases;
}

export function loadStoreTestCases(): MultipleInvalidStoreTestCase[] {
const testDataPath = path.join(__dirname, "../../../tests", "data", "stores");
const entries = fs.readdirSync(testDataPath, { withFileTypes: true });

const errors: MultipleInvalidStoreTestCase[] = [];

for (const entry of entries) {
if (!entry.isDirectory()) {
continue;
}

const testCase: Partial<MultipleInvalidStoreTestCase> = {
name: entry.name,
};

testCase.store = path.join(testDataPath, testCase.name!, "store.fga.yaml");

const errorsPath = path.join(testDataPath, testCase.name!, "expected_errors.json");
if (fs.existsSync(errorsPath)) {
const expectedErrors = fs.readFileSync(errorsPath);
testCase.expected_errors = JSON.parse(expectedErrors.toString("utf8"));
}

errors.push(testCase as MultipleInvalidStoreTestCase);
}

return errors;
}
39 changes: 39 additions & 0 deletions pkg/js/tests/valdiate-store.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { describe, expect, it } from "@jest/globals";
import { YamlStoreValidator, isStringValue } from "../validator/validate-store";
import { transformDSLToJSONObject } from "../transformer";
import { ValidateFunction } from "ajv";
import * as fs from "fs";
import * as YAML from "yaml";
import { loadStoreTestCases } from "./_testcases";
import path from "path";

describe("validate valid store file", () => {
const testCases = loadStoreTestCases();

testCases.forEach((testCase) => {
const testFn = testCase.skip ? it.skip : it;

testFn(`should valdiate ${testCase.name} `, () => {
const schemaValidator: ValidateFunction = YamlStoreValidator();

const yaml = YAML.parseDocument(fs.readFileSync(testCase.store).toString());

let jsonModel;
if (yaml.has("model") && isStringValue(yaml.get("model"))) {
jsonModel = transformDSLToJSONObject(String(yaml.get("model", false)));
}

if (yaml.has("model_file") && isStringValue(yaml.get("model_file"))) {
jsonModel = transformDSLToJSONObject(
fs.readFileSync(path.join(testCase.store, "..", String(yaml.get("model_file", false))), "utf-8"),
);
}

if (!schemaValidator.call({ jsonModel }, yaml.toJSON())) {
if (schemaValidator.errors) {
expect(schemaValidator.errors).toEqual(testCase.expected_errors);
}
}
});
});
});
Loading
Loading