livr
Version:
Lightweight validator supporting Language Independent Validation Rules Specification
16 lines (12 loc) • 735 B
JavaScript
const util = require('../../util');
// Pre-compiled regex at module load time (not per-call)
const URL_RE = /^(?:(?:http|https):\/\/)(?:\S+(?::\S*)?@)?(?:(?:(?:[1-9]\d?|1\d\d|2[0-1]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[0-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))\.?|localhost)(?::\d{2,5})?(?:[/?#]\S*)?$/i;
function url() {
return value => {
if (util.isNoValue(value)) return;
if (!util.isPrimitiveValue(value)) return 'FORMAT_ERROR';
if (value.length < 2083 && URL_RE.test(value)) return;
return 'WRONG_URL';
};
}
module.exports = url;