Skip to content

Commit

Permalink
Merge pull request #49 from GrantBirki/support-ajv-non-strict-mode
Browse files Browse the repository at this point in the history
support ajv strict option via inputs
GrantBirki authored Jan 4, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents 0be3fe3 + 51a3ef9 commit 9caf79a
Showing 6 changed files with 23 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -53,6 +53,7 @@ Here is a quick example of how to install this action in any workflow:
| `use_gitignore` | `true` | `"true"` | Whether or not to use the .gitignore file in the root of the repository to exclude files from validation - `"true"` or `"false"` - Default is `"true"` |
| `git_ignore_path` | `false` | `".gitignore"` | The full path to the .gitignore file to use if use_gitignore is set to "true" (e.g. ./src/.gitignore) - Default is ".gitignore" which uses the .gitignore file in the root of the repository |
| `allow_multiple_documents` | `false` | `"false"` | Whether or not to allow multiple documents in a single YAML file - `"true"` or `"false"` - Default is `"false"`. Useful for k8s documents. |
| `ajv_strict_mode` | `false` | `"true"` | Whether or not to use strict mode for AJV - "true" or "false" - Default is "true" |
| `github_token` | `false` | `${{ github.token }}` | The GitHub token used to create an authenticated client - Provided for you by default! |

## Outputs 📤
1 change: 1 addition & 0 deletions __tests__/functions/json-validator.test.js
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@ beforeEach(() => {
process.env.INPUT_YAML_EXTENSION_SHORT = '.yml'
process.env.INPUT_FILES = ''
process.env.INPUT_JSON_SCHEMA_VERSION = 'draft-07'
process.env.INPUT_AJV_STRICT_MODE = 'true'
})

test('successfully validates a json file with a schema', async () => {
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -85,6 +85,10 @@ inputs:
description: Whether or not to allow multiple documents in a single YAML file - "true" or "false" - Default is "false"
required: false
default: "false"
ajv_strict_mode:
description: Whether or not to use strict mode for AJV - "true" or "false" - Default is "true"
required: false
default: "true"
outputs:
success:
description: Whether or not the validation was successful for all files - "true" or "false"
12 changes: 8 additions & 4 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

12 changes: 8 additions & 4 deletions src/functions/json-validator.js
Original file line number Diff line number Diff line change
@@ -13,19 +13,23 @@ import {globSync} from 'glob'
// :returns: the compiled schema
async function schema(jsonSchema) {
const jsonSchemaVersion = core.getInput('json_schema_version')
const strict = core.getBooleanInput('ajv_strict_mode')

core.debug(`json_schema_version: ${jsonSchemaVersion}`)
core.debug(`strict: ${strict}`)

var ajv
if (jsonSchemaVersion === 'draft-07') {
ajv = new Ajv({allErrors: true})
ajv = new Ajv({allErrors: true, strict: strict})
} else if (jsonSchemaVersion === 'draft-2019-09') {
ajv = new Ajv2019({allErrors: true})
ajv = new Ajv2019({allErrors: true, strict: strict})
} else if (jsonSchemaVersion === 'draft-2020-12') {
ajv = new Ajv2020({allErrors: true})
ajv = new Ajv2020({allErrors: true, strict: strict})
} else {
core.warning(
`json_schema_version '${jsonSchemaVersion}' is not supported. Defaulting to 'draft-07'`
)
ajv = new Ajv({allErrors: true})
ajv = new Ajv({allErrors: true, strict: strict})
}

// use ajv-formats if enabled

0 comments on commit 9caf79a

Please sign in to comment.