Skip to content
Snippets Groups Projects
Unverified Commit a65a86c6 authored by Prashant Rawat's avatar Prashant Rawat Committed by GitHub
Browse files

Add docstrings for validators service (#9197)


* add docstrings for validators service

* Update services/validators.js

---------

Co-authored-by: default avatarchris48s <chris48s@users.noreply.github.com>
Co-authored-by: default avatarrepo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
parent 8a11db82
Branches
Tags
No related merge requests found
/**
* This module contains commonly used validators.
*
* @module
*/
import { import {
semver as joiSemver, semver as joiSemver,
semverRange as joiSemverRange, semverRange as joiSemverRange,
...@@ -5,21 +11,71 @@ import { ...@@ -5,21 +11,71 @@ import {
import joi from 'joi' import joi from 'joi'
const Joi = joi.extend(joiSemver).extend(joiSemverRange) 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) const optionalNonNegativeInteger = Joi.number().integer().min(0)
export { optionalNonNegativeInteger } 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() 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() 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() 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() 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 = export const optionalDottedVersionNClausesWithOptionalSuffix =
Joi.string().regex(/^\d+(\.\d+)*([-+].*)?$/) 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'] }) 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() export const fileSize = Joi.string()
.regex(/^[0-9]+(b|kb|mb|gb|tb)$/i) .regex(/^[0-9]+(b|kb|mb|gb|tb)$/i)
.required() .required()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment