validata
Version:
Type safe data validation and sanitization
40 lines • 1.54 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.maybeAsUrl = exports.asUrl = exports.maybeUrl = exports.isUrl = void 0;
const common_1 = require("./common");
const types_1 = require("./types");
const check = (value) => {
return value instanceof URL;
};
const convert = (value) => {
if (typeof value === 'string') {
try {
return new URL(value);
}
catch (e) {
return undefined;
}
}
return undefined;
};
const coerce = (options) => (next) => (value, path) => {
if (!options)
return next(value, path);
const coerced = new URL(value.toString());
if (options.setProtocol) {
coerced.protocol = options.setProtocol;
}
return next(coerced, path);
};
const validate = (value, path, options) => {
const result = (0, common_1.basicValidation)(value, path, options);
if (options.protocol && value.protocol.replace(/:\s*$/, '') !== options.protocol.replace(/:\s*$/, '')) {
result.issues.push(types_1.Issue.forPath(path, value, 'invalid-protocol', { expectedProtocol: options.protocol }));
}
return result;
};
exports.isUrl = (0, common_1.createIsCheck)('url', check, coerce, validate);
exports.maybeUrl = (0, common_1.createMaybeCheck)('url', check, coerce, validate);
exports.asUrl = (0, common_1.createAsCheck)('url', check, convert, coerce, validate);
exports.maybeAsUrl = (0, common_1.createMaybeAsCheck)('url', check, convert, coerce, validate);
//# sourceMappingURL=url.js.map
;