Skip to content

Commit

Permalink
Merge pull request #64 from cfuerst/fix/support-custom-ajv-format/tests
Browse files Browse the repository at this point in the history
fix(ajv-custom-format): tighten implemenation and provide missing tests
  • Loading branch information
GrantBirki authored Apr 18, 2024
2 parents 6aecea1 + 5dab15a commit d7814b9
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 9 deletions.
39 changes: 37 additions & 2 deletions __tests__/functions/json-validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,41 @@ test('fails to validate a json file with a schema containing a custom ajv format
})
})

test('todo - testcase needed for format referenced in schema but not added?', async () => {})
test('test that validator throws error when custom_ajv_regexp_format does not comply to key=value format', async () => {
expect.assertions(1)
try {
process.env.INPUT_AJV_CUSTOM_REGEXP_FORMATS = 'foobar'
expect(await jsonValidator(excludeMock))
} catch (e) {
expect(e.message).toBe(
'Invalid ajv_custom_regexp_formats format: "foobar" is not in expected format "key=regex"'
)
}
})

test('test that validator throws error when custom_ajv_regexp_format does not contain a valid regexp', async () => {
expect.assertions(1)
try {
process.env.INPUT_AJV_CUSTOM_REGEXP_FORMATS = 'foobar=\\'
expect(await jsonValidator(excludeMock))
} catch (e) {
expect(e.message).toBe(
'Invalid regular expression: Invalid regular expression: /\\/: \\ at end of pattern'
)
}
})

test('todo - testcase needed for invalid INPUT_AJV_CUSTOM_REGEXP_FORMATS input (structure, regex etc.)?', async () => {})
test('test that schema compile throws error when attemting to validate a json to a schema with unknown format', async () => {
expect.assertions(1)
try {
process.env.INPUT_JSON_SCHEMA =
'__tests__/fixtures/schemas/schema_with_custom_ajv_regexp_format.json'
process.env.INPUT_FILES =
'__tests__/fixtures/json/custom_ajv_regexp_format/valid.json'
expect(await jsonValidator(excludeMock))
} catch (e) {
expect(e.message).toBe(
'unknown format "lowercase_char" ignored in schema at path "#/properties/lowercase_char_property"'
)
}
})
24 changes: 21 additions & 3 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.

24 changes: 21 additions & 3 deletions src/functions/json-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,28 @@ async function schema(jsonSchema) {
// add custom regexp format if provided
core
.getMultilineInput('ajv_custom_regexp_formats')
.filter(Boolean)
.filter(Boolean) // Filter out any empty lines
.forEach(customFormat => {
customFormat = customFormat.trim().split(/=(.*)/s).filter(Boolean)
ajv.addFormat(customFormat[0], new RegExp(customFormat[1]))
// Check format using a regex
if (!/^[\w-]+=.+$/.test(customFormat.trim())) {
throw Error(
`Invalid ajv_custom_regexp_formats format: "${customFormat}" is not in expected format "key=regex"`
)
}

// Split into key-value pair
const keyValuePair = customFormat.trim().split(/=(.*)/s)

// Validate and compile the regular expression
let regex
try {
regex = new RegExp(keyValuePair[1])
} catch (syntaxError) {
throw Error(`Invalid regular expression: ${syntaxError.message}`)
}

// Add format if the regex is successfully compiled
ajv.addFormat(keyValuePair[0], regex)
})

// if a jsonSchema is provided, validate the json against it
Expand Down

0 comments on commit d7814b9

Please sign in to comment.