diff --git a/services/validators.js b/services/validators.js index 4586f08a3716c79109e6dd7ca0d3d09ebf7a50ec..df8d72a4d46969e6883c30f6627d8f75a8a1450d 100644 --- a/services/validators.js +++ b/services/validators.js @@ -1,3 +1,9 @@ +/** + * This module contains commonly used validators. + * + * @module + */ + import { semver as joiSemver, semverRange as joiSemverRange, @@ -5,21 +11,71 @@ import { import joi from 'joi' const Joi = joi.extend(joiSemver).extend(joiSemverRange) +/** + * Joi validator that checks if a value is a number, an integer, and greater than or equal to zero. + * + * @type {Joi} + */ const optionalNonNegativeInteger = Joi.number().integer().min(0) export { optionalNonNegativeInteger } + +/** + * Joi validator that checks if a value is a number, an integer, greater than or equal to zero and the value must be present. + * + * @type {Joi} + */ export const nonNegativeInteger = optionalNonNegativeInteger.required() + +/** + * Joi validator that checks if a value is a number, an integer and the value must be present. + * + * @type {Joi} + */ export const anyInteger = Joi.number().integer().required() + +/** + * Joi validator that checks if a value is a valid semantic versioning string and the value must be present. + * + * @type {Joi} + */ export const semver = Joi.semver().valid().required() + +/** + * Joi validator that checks if a value is a valid semantic versioning range and the value must be present. + * + * @type {Joi} + */ export const semverRange = Joi.semverRange().valid().required() + +/** + * Joi validator that checks if a value is a string that matches a regular expression. + * The regular expression matches strings that start with one or more digits, followed by zero or more groups of a dot and one or more digits, + * followed by an optional suffix that starts with a dash or a plus sign and can contain any characters. + * This validator can be used to validate properties that can be version strings with an optional suffix or absent. + * For example, some valid values for this validator are: 1, 1.0, 1.0.0, 1.0.0-beta + * Some invalid values for this validator are: abc, 1.a, 1.0-, .1 + * + * @type {Joi} + */ export const optionalDottedVersionNClausesWithOptionalSuffix = Joi.string().regex(/^\d+(\.\d+)*([-+].*)?$/) -// TODO This accepts URLs with query strings and fragments, which for some -// purposes should be rejected. + +/** + * Joi validator that checks if a value is a URL + * + * TODO: This accepts URLs with query strings and fragments, which for some purposes should be rejected. + * + * @type {Joi} + */ export const optionalUrl = Joi.string().uri({ scheme: ['http', 'https'] }) -// validator for a file size we are going to pass to bytes.parse -// see https://github.com/visionmedia/bytes.js#bytesparsestringnumber-value-numbernull +/** + * Joi validator for a file size we are going to pass to bytes.parse + * see https://github.com/visionmedia/bytes.js#bytesparsestringnumber-value-numbernull + * + * @type {Joi} + */ export const fileSize = Joi.string() .regex(/^[0-9]+(b|kb|mb|gb|tb)$/i) .required()