Skip to content

Commit 9fe5266

Browse files
authored
Merge pull request #61 from cfuerst/feat/60/suport-custom-ajv-format
feat(ajv-custom-format): initial poc
2 parents d5bdaec + 1e8d0ce commit 9fe5266

File tree

10 files changed

+109
-1
lines changed

10 files changed

+109
-1
lines changed

.github/workflows/acceptance-test.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,15 @@ jobs:
4545
files: |
4646
__tests__/**/test1.yml
4747
__tests__/**/test*.json
48+
49+
- name: acceptance test - custom formats
50+
uses: ./
51+
id: json-yaml-validate-custom-formats-test
52+
with:
53+
comment: "true"
54+
json_schema: ./__tests__/fixtures/schemas/schema_with_custom_ajv_regexp_format.json
55+
ajv_custom_regexp_formats: |
56+
lowercase_char=^[a-z]*$
57+
lowercase_alphanumeric=^[a-z0-9]*$
58+
files: |
59+
__tests__/fixtures/json/custom_ajv_regexp_format/valid.json

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Here is a quick example of how to install this action in any workflow:
5454
| `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 |
5555
| `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. |
5656
| `ajv_strict_mode` | `false` | `"true"` | Whether or not to use strict mode for AJV - "true" or "false" - Default is "true" |
57+
| `ajv_custom_regexp_formats` | `false` | `""` | List of key value pairs of format_name=regexp. Each pair must be on a newline. (e.g. lowercase_chars=^[a-z]*$ ) |
5758
| `github_token` | `false` | `${{ github.token }}` | The GitHub token used to create an authenticated client - Provided for you by default! |
5859

5960
## Outputs 📤
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"lowercase_char_property": "INVALID",
3+
"lowercase_alphanumeric_property": "INVALID"
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"lowercase_char_property": "valid",
3+
"lowercase_alphanumeric_property": "valid1"
4+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"type": "object",
3+
"properties": {
4+
"lowercase_char_property": {
5+
"type": "string",
6+
"format": "lowercase_char"
7+
},
8+
"lowercase_alphanumeric_property": {
9+
"type": "string",
10+
"format": "lowercase_alphanumeric"
11+
}
12+
},
13+
"required": ["lowercase_char_property", "lowercase_alphanumeric_property"],
14+
"additionalProperties": false
15+
}

__tests__/functions/json-validator.test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ beforeEach(() => {
2828
process.env.INPUT_FILES = ''
2929
process.env.INPUT_JSON_SCHEMA_VERSION = 'draft-07'
3030
process.env.INPUT_AJV_STRICT_MODE = 'true'
31+
process.env.INPUT_AJV_CUSTOM_REGEXP_FORMATS = ''
3132
})
3233

3334
test('successfully validates a json file with a schema', async () => {
@@ -472,3 +473,53 @@ test('fails to validate a yaml file with an incorrect schema when yaml_as_json i
472473
)
473474
)
474475
})
476+
477+
test('successfully validates a json file with a schema containing a custom ajv format when custom format was added', async () => {
478+
process.env.INPUT_JSON_SCHEMA =
479+
'__tests__/fixtures/schemas/schema_with_custom_ajv_regexp_format.json'
480+
process.env.INPUT_FILES =
481+
'__tests__/fixtures/json/custom_ajv_regexp_format/valid.json'
482+
process.env.INPUT_AJV_CUSTOM_REGEXP_FORMATS =
483+
'lowercase_char=^[a-z]*$\nlowercase_alphanumeric=^[a-z0-9]*$'
484+
expect(await jsonValidator(excludeMock)).toStrictEqual({
485+
failed: 0,
486+
passed: 1,
487+
skipped: 0,
488+
success: true,
489+
violations: []
490+
})
491+
})
492+
493+
test('fails to validate a json file with a schema containing a custom ajv format when custom format added', async () => {
494+
process.env.INPUT_JSON_SCHEMA =
495+
'__tests__/fixtures/schemas/schema_with_custom_ajv_regexp_format.json'
496+
process.env.INPUT_FILES =
497+
'__tests__/fixtures/json/custom_ajv_regexp_format/invalid.json'
498+
process.env.INPUT_AJV_CUSTOM_REGEXP_FORMATS =
499+
'lowercase_char=^[a-z]*$\nlowercase_alphanumeric=^[a-z0-9]*$'
500+
expect(await jsonValidator(excludeMock)).toStrictEqual({
501+
failed: 1,
502+
passed: 0,
503+
skipped: 0,
504+
success: false,
505+
violations: [
506+
{
507+
file: '__tests__/fixtures/json/custom_ajv_regexp_format/invalid.json',
508+
errors: [
509+
{
510+
path: '/lowercase_char_property',
511+
message: 'must match format "lowercase_char"'
512+
},
513+
{
514+
path: '/lowercase_alphanumeric_property',
515+
message: 'must match format "lowercase_alphanumeric"'
516+
}
517+
]
518+
}
519+
]
520+
})
521+
})
522+
523+
test('todo - testcase needed for format referenced in schema but not added?', async () => {})
524+
525+
test('todo - testcase needed for invalid INPUT_AJV_CUSTOM_REGEXP_FORMATS input (structure, regex etc.)?', async () => {})

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ inputs:
8989
description: Whether or not to use strict mode for AJV - "true" or "false" - Default is "true"
9090
required: false
9191
default: "true"
92+
ajv_custom_regexp_formats:
93+
description: List of key value pairs of format_name=regexp. Each pair must be on a newline. (e.g. lowercase_chars=^[a-z]*$ )
94+
required: false
95+
default: ""
9296
outputs:
9397
success:
9498
description: Whether or not the validation was successful for all files - "true" or "false"

dist/index.js

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/functions/json-validator.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ async function schema(jsonSchema) {
4040
core.debug('ajv-formats will not be used with the json-validator')
4141
}
4242

43+
// add custom regexp format if provided
44+
core
45+
.getMultilineInput('ajv_custom_regexp_formats')
46+
.filter(Boolean)
47+
.forEach(customFormat => {
48+
customFormat = customFormat.trim().split(/=(.*)/s).filter(Boolean)
49+
ajv.addFormat(customFormat[0], new RegExp(customFormat[1]))
50+
})
51+
4352
// if a jsonSchema is provided, validate the json against it
4453
var schema
4554
if (jsonSchema && jsonSchema !== '') {

0 commit comments

Comments
 (0)