Skip to content

Commit

Permalink
Merge pull request #34 from GrantBirki/support-more-ajv-versions
Browse files Browse the repository at this point in the history
Support More JSON Schema Versions
  • Loading branch information
GrantBirki committed Aug 14, 2023
2 parents 8ed3604 + 3f06bcc commit 1652fde
Show file tree
Hide file tree
Showing 6 changed files with 857 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Here is a quick example of how to install this action in any workflow:
| `files` | `false` | `""` | List of file paths to validate. Each file path must be on a newline. |
| `use_dot_match` | `false` | `"true"` | Whether or not to use dot-matching when searching for files - `"true"` or `"false"` - If this is true, directories like `.github`, etc will be searched |
| `json_schema` | `false` | `""` | The full path to the JSON schema file (e.g. ./schemas/schema.json) - Default is `""` which doesn't enforce a strict schema |
| `json_schema_version` | `false` | `"draft-07"` | The version of the JSON schema to use - `"draft-07"`, `"draft-2019-09"`, `"draft-2020-12"` |
| `json_extension` | `false` | `".json"` | The file extension for JSON files (e.g. .json) |
| `json_exclude_regex` | `false` | `""` | A regex to exclude files from validation (e.g. `".*\.schema\.json$"` to exclude all files ending with `.schema.json`) - Default is `""` which doesn't exclude any files |
| `use_ajv_formats` | `false` | `"true"` | Whether or not to use the [AJV formats](https://github.com/ajv-validator/ajv-formats) with the JSON processor |
Expand Down
105 changes: 105 additions & 0 deletions __tests__/functions/json-validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as core from '@actions/core'
const debugMock = jest.spyOn(core, 'debug').mockImplementation(() => {})
const infoMock = jest.spyOn(core, 'info').mockImplementation(() => {})
const errorMock = jest.spyOn(core, 'error').mockImplementation(() => {})
const warningMock = jest.spyOn(core, 'warning').mockImplementation(() => {})

class Exclude {
isExcluded() {
Expand All @@ -25,6 +26,7 @@ beforeEach(() => {
process.env.INPUT_YAML_EXTENSION = '.yaml'
process.env.INPUT_YAML_EXTENSION_SHORT = '.yml'
process.env.INPUT_FILES = ''
process.env.INPUT_JSON_SCHEMA_VERSION = 'draft-07'
})

test('successfully validates a json file with a schema', async () => {
Expand All @@ -41,6 +43,25 @@ test('successfully validates a json file with a schema', async () => {
)
})

test('successfully validates a json file with a schema and defaults to the draft-07 schema version when none is set', async () => {
const badJsonSchemaVersion = 'evil-draft-999'
process.env.INPUT_JSON_SCHEMA_VERSION = badJsonSchemaVersion
expect(await jsonValidator(excludeMock)).toStrictEqual({
failed: 0,
passed: 1,
skipped: 0,
success: true,
violations: []
})

expect(debugMock).toHaveBeenCalledWith(
'using ajv-formats with json-validator'
)
expect(warningMock).toHaveBeenCalledWith(
`json_schema_version '${badJsonSchemaVersion}' is not supported. Defaulting to 'draft-07'`
)
})

test('successfully validates a json file without using a schema', async () => {
process.env.INPUT_JSON_SCHEMA = ''
expect(await jsonValidator(excludeMock)).toStrictEqual({
Expand Down Expand Up @@ -299,6 +320,90 @@ test('processes a real world example when yaml_as_json is true and the single fi
)
})

test('processes a real world example when yaml_as_json is true and the single file contains multiple schema errors and uses the 2020 ajv schema', async () => {
process.env.INPUT_YAML_AS_JSON = 'true'
process.env.INPUT_JSON_SCHEMA = '__tests__/fixtures/schemas/challenge.json'
process.env.INPUT_BASE_DIR = '__tests__/fixtures/real_world/challenges'
process.env.INPUT_JSON_SCHEMA_VERSION = 'draft-2020-12'

expect(await jsonValidator(excludeMock)).toStrictEqual({
failed: 1,
passed: 0,
skipped: 0,
success: false,
violations: [
{
file: '__tests__/fixtures/real_world/challenges/challenge.yml',
errors: [
{
path: null,
message: `must have required property 'inputFormat'`
},
{
path: null,
message: `must have required property 'publicTests'`
}
]
}
]
})

expect(debugMock).toHaveBeenCalledWith(
'using ajv-formats with json-validator'
)
expect(debugMock).toHaveBeenCalledWith(
'json - using baseDir: __tests__/fixtures/real_world/challenges'
)
expect(debugMock).toHaveBeenCalledWith(
'json - using glob: **/*{.json,yaml,yml}'
)
expect(debugMock).toHaveBeenCalledWith(
`attempting to process yaml file: '__tests__/fixtures/real_world/challenges/challenge.yml' as json`
)
})

test('processes a real world example when yaml_as_json is true and the single file contains multiple schema errors and uses the 2019 ajv schema', async () => {
process.env.INPUT_YAML_AS_JSON = 'true'
process.env.INPUT_JSON_SCHEMA = '__tests__/fixtures/schemas/challenge.json'
process.env.INPUT_BASE_DIR = '__tests__/fixtures/real_world/challenges'
process.env.INPUT_JSON_SCHEMA_VERSION = 'draft-2019-09'

expect(await jsonValidator(excludeMock)).toStrictEqual({
failed: 1,
passed: 0,
skipped: 0,
success: false,
violations: [
{
file: '__tests__/fixtures/real_world/challenges/challenge.yml',
errors: [
{
path: null,
message: `must have required property 'inputFormat'`
},
{
path: null,
message: `must have required property 'publicTests'`
}
]
}
]
})

expect(debugMock).toHaveBeenCalledWith(
'using ajv-formats with json-validator'
)
expect(debugMock).toHaveBeenCalledWith(
'json - using baseDir: __tests__/fixtures/real_world/challenges'
)
expect(debugMock).toHaveBeenCalledWith(
'json - using glob: **/*{.json,yaml,yml}'
)
expect(debugMock).toHaveBeenCalledWith(
`attempting to process yaml file: '__tests__/fixtures/real_world/challenges/challenge.yml' as json`
)
})

test('successfully validates json files with a schema when files is defined', async () => {
const files = [
'__tests__/fixtures/json/valid/json1.json',
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ inputs:
description: The full path to the JSON schema file (e.g. ./schemas/schema.json) - Default is "" which doesn't enforce a strict schema
required: false
default: ""
json_schema_version:
description: The version of the JSON schema to use - "draft-07", "draft-2019-09", "draft-2020-12"
default: "draft-07"
required: false
json_extension:
description: The file extension for JSON files (e.g. .json) - Default is ".json"
required: false
Expand Down
Loading

0 comments on commit 1652fde

Please sign in to comment.