freeCodeCamp/server/middlewares/validator.js

58 lines
1.6 KiB
JavaScript
Raw Normal View History

import validator from 'express-validator';
2016-06-01 22:52:08 +00:00
import { isPoly } from '../../common/utils/polyvinyl';
const isObject = val => !!val && typeof val === 'object';
export default function() {
return validator({
customValidators: {
matchRegex(param, regex) {
return regex.test(param);
},
isString(value) {
return typeof value === 'string';
},
isNumber(value) {
return typeof value === 'number';
2016-06-01 22:52:08 +00:00
},
isFiles(value) {
if (!isObject(value)) {
return false;
}
const keys = Object.keys(value);
return !!keys.length &&
// every key is a file
keys.every(key => isObject(value[key])) &&
// every file has contents
keys.map(key => value[key]).every(file => isPoly(file));
}
},
customSanitizers: {
// Refer : http://stackoverflow.com/a/430240/1932901
trimTags(value) {
const tagBody = '(?:[^"\'>]|"[^"]*"|\'[^\']*\')*';
const tagOrComment = new RegExp(
'<(?:'
// Comment body.
+ '!--(?:(?:-*[^->])*--+|-?)'
// Special "raw text" elements whose content should be elided.
+ '|script\\b' + tagBody + '>[\\s\\S]*?</script\\s*'
+ '|style\\b' + tagBody + '>[\\s\\S]*?</style\\s*'
// Regular name
+ '|/?[a-z]'
+ tagBody
+ ')>',
'gi'
);
let rawValue;
do {
rawValue = value;
value = value.replace(tagOrComment, '');
} while (value !== rawValue);
return value.replace(/</g, '&lt;');
}
}
});
}