Skip to content

Commit

Permalink
fix: json schema (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpoehnelt authored Aug 14, 2020
1 parent 7985284 commit b5d8f63
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Share this app with one of the following badges.

### Configuration

The level of rules can be modified using `.github/in-solidarity.yml`. Chec the[sample configuration](https://github.com/jpoehnelt/in-solidarity-bot/blob/main/fixtures/in-solidarity.yml).
The level of rules can be modified using `.github/in-solidarity.yml`. Check the [sample configuration](https://github.com/jpoehnelt/in-solidarity-bot/blob/main/fixtures/in-solidarity.yml).

```yaml
rules:
Expand Down
21 changes: 17 additions & 4 deletions src/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import fs from "fs";
import yaml from "js-yaml";

const fakeContext = ({
config: () =>
config: async () =>
yaml.safeLoad(fs.readFileSync("./fixtures/in-solidarity.yml", "utf8")),
} as unknown) as Context;

Expand All @@ -35,7 +35,7 @@ test("should override default rules", async () => {

test("should throw for invalid config", async () => {
const context = ({
config: () => {
config: async () => {
return {
rules: {
master: {
Expand All @@ -48,9 +48,22 @@ test("should throw for invalid config", async () => {
await expect(getConfig(context)).rejects.toBeInstanceOf(InvalidConfigError);
});

test("should handle case wehre no repo config", async () => {
test("should throw for invalid config having level at top", async () => {
const context = ({
config: () => {
config: async () => {
return {
rules: {
master: "failure",
},
};
},
} as unknown) as Context;
await expect(getConfig(context)).rejects.toBeInstanceOf(InvalidConfigError);
});

test("should handle case where no repo config", async () => {
const context = ({
config: async () => {
return;
},
} as unknown) as Context;
Expand Down
4 changes: 3 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const ajv = new Ajv({ allErrors: true });

const rulesPropertiesSchema = Object.keys(DEFAULT_RULES).reduce((obj, k) => {
obj[k] = {
type: "object",
additionalProperties: false,
properties: {
level: { type: "string", enum: Object.values(Level) },
Expand All @@ -45,9 +46,11 @@ const rulesPropertiesSchema = Object.keys(DEFAULT_RULES).reduce((obj, k) => {
}, {});

const schema = {
type: "object",
additionalProperties: false,
properties: {
rules: {
type: "object",
additionalProperties: false,
properties: rulesPropertiesSchema,
},
Expand All @@ -58,7 +61,6 @@ export const getConfig = async (context: Context): Promise<Configuration> => {
const validate = ajv.compile(schema);

const repoConfig = await context.config(CONFIG_FILE);

if (repoConfig) {
if (!validate(repoConfig)) {
throw new InvalidConfigError(
Expand Down

0 comments on commit b5d8f63

Please sign in to comment.