It’s hard to understand how #3813 is persisting. The schema should reject keys not in the object… it’s very puzzling. This replaces the code used to get the color with a function call that throws an explicit error when the key is missing. It should at least make it clear what the key value is when this fails in the future.
32 lines
600 B
JavaScript
32 lines
600 B
JavaScript
'use strict'
|
|
|
|
const Joi = require('@hapi/joi')
|
|
|
|
// https://support.codefactor.io/i14-glossary
|
|
const colorMap = {
|
|
A: 'brightgreen',
|
|
'A-': 'green',
|
|
'B+': 'yellowgreen',
|
|
B: 'yellowgreen',
|
|
'B-': 'yellowgreen',
|
|
'C+': 'yellow',
|
|
C: 'yellow',
|
|
'C-': 'yellow',
|
|
'D+': 'orange',
|
|
D: 'orange',
|
|
'D-': 'orange',
|
|
F: 'red',
|
|
}
|
|
|
|
const isValidGrade = Joi.allow(Object.keys(colorMap)).required()
|
|
|
|
function gradeColor(grade) {
|
|
const color = colorMap[grade]
|
|
if (color === undefined) {
|
|
throw Error(`Unknown grade: ${grade}`)
|
|
}
|
|
return color
|
|
}
|
|
|
|
module.exports = { isValidGrade, gradeColor }
|