Skip to content

Commit 075256e

Browse files
Merge pull request #710 from protofire/improve-ajv-schema-validation
improve ajv-schema validation
2 parents d439651 + 8fc3a64 commit 075256e

File tree

1 file changed

+16
-27
lines changed

1 file changed

+16
-27
lines changed

lib/rules/base-checker.js

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,9 @@ class BaseChecker {
1313
if (!reporter) return
1414

1515
const userConfig = reporter.config[ruleId]
16-
1716
if (userConfig === undefined) return
1817

19-
// build schema in memory to use better errors
20-
// can be skipped if the rule has no options
21-
// can be disabled when solhint rules are compatible with ajv-errors
18+
// build schema related to userConfig and meta.schema
2219
const buildSchema = () => {
2320
const base = {
2421
type: 'object',
@@ -38,58 +35,46 @@ class BaseChecker {
3835
properties: {
3936
value: {
4037
...meta.schema,
41-
errorMessage: meta.schema.description,
38+
errorMessage: meta.schema.description, // used by ajv-errors
4239
},
4340
},
4441
required: ['value'],
4542
additionalProperties: false,
4643
}
4744
}
45+
4846
return base
4947
}
5048

51-
// console.log('userConfig :>> ', userConfig, ' - ', ruleId, ' - ', userConfig.length)
52-
5349
const data =
5450
Array.isArray(userConfig) && userConfig.length > 1
5551
? { severity: userConfig[0], options: { value: userConfig[1] } }
5652
: { severity: userConfig }
5753

58-
// console.log('data :>> ', data)
59-
54+
// build schema and cache by the actual content of the schema
6055
const schema = buildSchema()
56+
const schemaKey = ruleId + '//' + JSON.stringify(schema)
6157

62-
// console.log('schema :>> ', schema)
58+
if (!BaseChecker.validators.has(schemaKey)) {
59+
const validate = BaseChecker.ajv.compile(schema)
60+
BaseChecker.validators.set(schemaKey, validate)
61+
}
6362

64-
const ajv = new Ajv({ allErrors: true, jsonPointers: true })
65-
ajvErrors(ajv) // activate ajv-errors
66-
const validate = ajv.compile(schema)
63+
const validate = BaseChecker.validators.get(schemaKey)
6764

6865
if (!validate(data)) {
6966
this.enabled = false
7067
let message = ''
7168
try {
7269
message =
73-
betterAjvErrors(schema, data, validate.errors, {
70+
betterAjvErrors(validate.schema, data, validate.errors, {
7471
format: 'cli',
7572
indent: 2,
7673
json: JSON.stringify(data, null, 2),
7774
}) || ''
7875
} catch (_) {
79-
//
76+
// noop
8077
}
81-
// if (!message.trim()) {
82-
// message = validate.errors
83-
// .map((e) => {
84-
// const dp = e.instancePath || ''
85-
// let human
86-
// if (dp === '/severity') human = 'severity'
87-
// else if (dp.startsWith('/options/value')) human = 'options.value'
88-
// else human = dp.slice(1) || 'config'
89-
// return `→ ${human}: ${e.message}`
90-
// })
91-
// .join('\n')
92-
// }
9378
console.warn(
9479
chalk.yellow(`[solhint] Warning: invalid configuration for rule '${ruleId}':\n${message}`)
9580
)
@@ -114,4 +99,8 @@ class BaseChecker {
11499
}
115100
}
116101

102+
BaseChecker.ajv = new Ajv({ allErrors: true, jsonPointers: true })
103+
ajvErrors(BaseChecker.ajv) // <- kee support of errorMessage
104+
BaseChecker.validators = new Map()
105+
117106
module.exports = BaseChecker

0 commit comments

Comments
 (0)