Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ajv-custom-format): tighten implemenation and provide missing tests #64

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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