Skip to content

Commit

Permalink
fix(ajv-custom-format): tighten implemenation and provide missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cfuerst committed Apr 18, 2024
1 parent 6aecea1 commit ae050de
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 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 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 ae050de

Please sign in to comment.