Setup eslint and enforce rules
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
82e7a1f299
commit
1d5056b753
@ -0,0 +1,3 @@
|
|||||||
|
node_modules
|
||||||
|
lib
|
||||||
|
dist
|
@ -0,0 +1,113 @@
|
|||||||
|
{
|
||||||
|
"env": {
|
||||||
|
"browser": true,
|
||||||
|
"es2021": true
|
||||||
|
},
|
||||||
|
"extends": [
|
||||||
|
"eslint:recommended",
|
||||||
|
"plugin:@typescript-eslint/eslint-recommended",
|
||||||
|
"plugin:@typescript-eslint/recommended"
|
||||||
|
],
|
||||||
|
"parser": "@typescript-eslint/parser",
|
||||||
|
"parserOptions": {
|
||||||
|
"ecmaVersion": 12,
|
||||||
|
"sourceType": "module"
|
||||||
|
},
|
||||||
|
"plugins": [
|
||||||
|
"@typescript-eslint"
|
||||||
|
],
|
||||||
|
"rules": {
|
||||||
|
"indent": [
|
||||||
|
"error",
|
||||||
|
4
|
||||||
|
],
|
||||||
|
"linebreak-style": [
|
||||||
|
"error",
|
||||||
|
"unix"
|
||||||
|
],
|
||||||
|
"quotes": [
|
||||||
|
"error",
|
||||||
|
"single",
|
||||||
|
{
|
||||||
|
"allowTemplateLiterals": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"semi": [
|
||||||
|
"error",
|
||||||
|
"never"
|
||||||
|
],
|
||||||
|
"no-console": "error",
|
||||||
|
"curly": "error",
|
||||||
|
"eqeqeq": "error",
|
||||||
|
"guard-for-in": "error",
|
||||||
|
"no-alert": "error",
|
||||||
|
"no-caller": "error",
|
||||||
|
"no-constructor-return": "error",
|
||||||
|
"no-eval": "error",
|
||||||
|
"no-implicit-coercion": "error",
|
||||||
|
"no-implied-eval": "error",
|
||||||
|
"no-invalid-this": "error",
|
||||||
|
"no-return-await": "error",
|
||||||
|
"no-throw-literal": "error",
|
||||||
|
"no-useless-call": "error",
|
||||||
|
"radix": "error",
|
||||||
|
"yoda": "error",
|
||||||
|
"@typescript-eslint/no-shadow": "error",
|
||||||
|
"brace-style": "error",
|
||||||
|
"camelcase": "error",
|
||||||
|
"comma-dangle": [
|
||||||
|
"error",
|
||||||
|
"always-multiline"
|
||||||
|
],
|
||||||
|
"comma-spacing": [
|
||||||
|
"error",
|
||||||
|
{
|
||||||
|
"before": false,
|
||||||
|
"after": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"comma-style": [
|
||||||
|
"error",
|
||||||
|
"last"
|
||||||
|
],
|
||||||
|
"computed-property-spacing": [
|
||||||
|
"error",
|
||||||
|
"never"
|
||||||
|
],
|
||||||
|
"eol-last": "error",
|
||||||
|
"func-call-spacing": [
|
||||||
|
"error",
|
||||||
|
"never"
|
||||||
|
],
|
||||||
|
"keyword-spacing": [
|
||||||
|
"error",
|
||||||
|
{
|
||||||
|
"before": true,
|
||||||
|
"after": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"lines-between-class-members": "error",
|
||||||
|
"max-params": [
|
||||||
|
"error",
|
||||||
|
4
|
||||||
|
],
|
||||||
|
"new-parens": [
|
||||||
|
"error",
|
||||||
|
"always"
|
||||||
|
],
|
||||||
|
"newline-per-chained-call": "error",
|
||||||
|
"no-trailing-spaces": "error",
|
||||||
|
"no-underscore-dangle": "error",
|
||||||
|
"no-unneeded-ternary": "error",
|
||||||
|
"no-whitespace-before-property": "error",
|
||||||
|
"object-property-newline": "error",
|
||||||
|
"prefer-exponentiation-operator": "error",
|
||||||
|
"prefer-object-spread": "error",
|
||||||
|
"spaced-comment": [
|
||||||
|
"error",
|
||||||
|
"always"
|
||||||
|
],
|
||||||
|
"prefer-const": "error",
|
||||||
|
"@typescript-eslint/no-explicit-any": "off"
|
||||||
|
}
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,22 +1,21 @@
|
|||||||
import {Template} from "../Template"
|
import {Template} from '../Template'
|
||||||
import {UniversalPath} from "../../util"
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A template that generates a new configuration file in the app/configs directory.
|
* A template that generates a new configuration file in the app/configs directory.
|
||||||
*/
|
*/
|
||||||
const config_template: Template = {
|
const templateConfig: Template = {
|
||||||
name: 'config',
|
name: 'config',
|
||||||
fileSuffix: '.config.ts',
|
fileSuffix: '.config.ts',
|
||||||
description: 'Create a new config file.',
|
description: 'Create a new config file.',
|
||||||
baseAppPath: ['configs'],
|
baseAppPath: ['configs'],
|
||||||
render(name: string, fullCanonicalName: string, targetFilePath: UniversalPath) {
|
render() {
|
||||||
return `import { env } from '@extollo/lib'
|
return `import { env } from '@extollo/lib'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
key: env('VALUE_ENV_VAR', 'default value'),
|
key: env('VALUE_ENV_VAR', 'default value'),
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
export { config_template }
|
export { templateConfig }
|
||||||
|
@ -1,130 +1,150 @@
|
|||||||
import {ValidationResult, ValidatorFunction} from "./types";
|
import {ValidationResult, ValidatorFunction} from './types'
|
||||||
|
|
||||||
export namespace Arr {
|
/** Requires the input value to be an array. */
|
||||||
/** Requires the input value to be an array. */
|
function is(fieldName: string, inputValue: unknown): ValidationResult {
|
||||||
export function is(fieldName: string, inputValue: any): ValidationResult {
|
if ( Array.isArray(inputValue) ) {
|
||||||
if ( Array.isArray(inputValue) ) {
|
return { valid: true }
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
valid: false,
|
||||||
|
message: 'must be an array',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Requires the values in the input value array to be distinct. */
|
||||||
|
function distinct(fieldName: string, inputValue: unknown): ValidationResult {
|
||||||
|
const arr = is(fieldName, inputValue)
|
||||||
|
if ( !arr.valid ) {
|
||||||
|
return arr
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( Array.isArray(inputValue) && (new Set(inputValue)).size === inputValue.length ) {
|
||||||
|
return { valid: true }
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
valid: false,
|
||||||
|
message: 'must not contain duplicate values',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds a validator function that requires the input array to contain the given value.
|
||||||
|
* @param value
|
||||||
|
*/
|
||||||
|
function includes(value: unknown): ValidatorFunction {
|
||||||
|
return (fieldName: string, inputValue: unknown): ValidationResult => {
|
||||||
|
const arr = is(fieldName, inputValue)
|
||||||
|
if ( !arr.valid ) {
|
||||||
|
return arr
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( Array.isArray(inputValue) && inputValue.includes(value) ) {
|
||||||
return { valid: true }
|
return { valid: true }
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
valid: false,
|
valid: false,
|
||||||
message: 'must be an array'
|
message: `must include ${value}`,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Requires the values in the input value array to be distinct. */
|
/**
|
||||||
export function distinct(fieldName: string, inputValue: any): ValidationResult {
|
* Builds a validator function that requires the input array NOT to contain the given value.
|
||||||
|
* @param value
|
||||||
|
*/
|
||||||
|
function excludes(value: unknown): ValidatorFunction {
|
||||||
|
return (fieldName: string, inputValue: unknown): ValidationResult => {
|
||||||
const arr = is(fieldName, inputValue)
|
const arr = is(fieldName, inputValue)
|
||||||
if ( !arr.valid ) return arr
|
if ( !arr.valid ) {
|
||||||
|
return arr
|
||||||
|
}
|
||||||
|
|
||||||
if ( (new Set(inputValue)).size === inputValue.length ) {
|
if ( Array.isArray(inputValue) && !inputValue.includes(value) ) {
|
||||||
return { valid: true }
|
return { valid: true }
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
valid: false,
|
valid: false,
|
||||||
message: 'must not contain duplicate values'
|
message: `must not include ${value}`,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builds a validator function that requires the input array to contain the given value.
|
* Builds a validator function that requires the input array to have exactly `len` many entries.
|
||||||
* @param value
|
* @param len
|
||||||
*/
|
*/
|
||||||
export function includes(value: any): ValidatorFunction {
|
function length(len: number): ValidatorFunction {
|
||||||
return function includes(fieldName: string, inputValue: any): ValidationResult {
|
return (fieldName: string, inputValue: unknown): ValidationResult => {
|
||||||
const arr = is(fieldName, inputValue)
|
const arr = is(fieldName, inputValue)
|
||||||
if ( !arr.valid ) return arr
|
if ( !arr.valid ) {
|
||||||
|
return arr
|
||||||
if ( inputValue.includes(value) ) {
|
|
||||||
return { valid: true }
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
valid: false,
|
|
||||||
message: `must include ${value}`
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
if ( Array.isArray(inputValue) && inputValue.length === len ) {
|
||||||
* Builds a validator function that requires the input array NOT to contain the given value.
|
return { valid: true }
|
||||||
* @param value
|
|
||||||
*/
|
|
||||||
export function excludes(value: any): ValidatorFunction {
|
|
||||||
return function excludes(fieldName: string, inputValue: any): ValidationResult {
|
|
||||||
const arr = is(fieldName, inputValue)
|
|
||||||
if ( !arr.valid ) return arr
|
|
||||||
|
|
||||||
if ( !inputValue.includes(value) ) {
|
|
||||||
return { valid: true }
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
valid: false,
|
|
||||||
message: `must not include ${value}`
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
return {
|
||||||
* Builds a validator function that requires the input array to have exactly `len` many entries.
|
valid: false,
|
||||||
* @param len
|
message: `must be exactly of length ${len}`,
|
||||||
*/
|
|
||||||
export function length(len: number): ValidatorFunction {
|
|
||||||
return function length(fieldName: string, inputValue: any): ValidationResult {
|
|
||||||
const arr = is(fieldName, inputValue)
|
|
||||||
if ( !arr.valid ) return arr
|
|
||||||
|
|
||||||
if ( inputValue.length === len ) {
|
|
||||||
return { valid: true }
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
valid: false,
|
|
||||||
message: `must be exactly of length ${len}`
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds a validator function that requires the input array to have at least `len` many entries.
|
||||||
|
* @param len
|
||||||
|
*/
|
||||||
|
function lengthMin(len: number): ValidatorFunction {
|
||||||
|
return (fieldName: string, inputValue: unknown): ValidationResult => {
|
||||||
|
const arr = is(fieldName, inputValue)
|
||||||
|
if ( !arr.valid ) {
|
||||||
|
return arr
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
if ( Array.isArray(inputValue) && inputValue.length >= len ) {
|
||||||
* Builds a validator function that requires the input array to have at least `len` many entries.
|
return { valid: true }
|
||||||
* @param len
|
}
|
||||||
*/
|
|
||||||
export function lengthMin(len: number): ValidatorFunction {
|
return {
|
||||||
return function lengthMin(fieldName: string, inputValue: any): ValidationResult {
|
valid: false,
|
||||||
const arr = is(fieldName, inputValue)
|
message: `must be at least length ${len}`,
|
||||||
if ( !arr.valid ) return arr
|
|
||||||
|
|
||||||
if ( inputValue.length >= len ) {
|
|
||||||
return { valid: true }
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
valid: false,
|
|
||||||
message: `must be at least length ${len}`
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds a validator function that requires the input array to have at most `len` many entries.
|
||||||
|
* @param len
|
||||||
|
*/
|
||||||
|
function lengthMax(len: number): ValidatorFunction {
|
||||||
|
return (fieldName: string, inputValue: unknown): ValidationResult => {
|
||||||
|
const arr = is(fieldName, inputValue)
|
||||||
|
if ( !arr.valid ) {
|
||||||
|
return arr
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
if ( Array.isArray(inputValue) && inputValue.length <= len ) {
|
||||||
* Builds a validator function that requires the input array to have at most `len` many entries.
|
return { valid: true }
|
||||||
* @param len
|
}
|
||||||
*/
|
|
||||||
export function lengthMax(len: number): ValidatorFunction {
|
return {
|
||||||
return function lengthMax(fieldName: string, inputValue: any): ValidationResult {
|
valid: false,
|
||||||
const arr = is(fieldName, inputValue)
|
message: `must be at most length ${len}`,
|
||||||
if ( !arr.valid ) return arr
|
|
||||||
|
|
||||||
if ( inputValue.length <= len ) {
|
|
||||||
return { valid: true }
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
valid: false,
|
|
||||||
message: `must be at most length ${len}`
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const Arr = {
|
||||||
|
is,
|
||||||
|
distinct,
|
||||||
|
includes,
|
||||||
|
excludes,
|
||||||
|
length,
|
||||||
|
lengthMin,
|
||||||
|
lengthMax,
|
||||||
|
}
|
||||||
|
@ -1,70 +1,80 @@
|
|||||||
import {infer as inferUtil} from '../../util'
|
import {infer as inferUtil} from '../../util'
|
||||||
import {ValidationResult} from "./types";
|
import {ValidationResult} from './types'
|
||||||
|
|
||||||
export namespace Cast {
|
/** Attempt to infer the native type of a string value. */
|
||||||
/** Attempt to infer the native type of a string value. */
|
function infer(fieldName: string, inputValue: unknown): ValidationResult {
|
||||||
export function infer(fieldName: string, inputValue: any): ValidationResult {
|
return {
|
||||||
return {
|
valid: true,
|
||||||
valid: true,
|
castValue: typeof inputValue === 'string' ? inferUtil(inputValue) : inputValue,
|
||||||
castValue: typeof inputValue === 'string' ? inferUtil(inputValue) : inputValue,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Casts the input value to a boolean.
|
* Casts the input value to a boolean.
|
||||||
* Note that this assumes the value may be boolish. The strings "true", "True",
|
* Note that this assumes the value may be boolish. The strings "true", "True",
|
||||||
* "TRUE", and "1" evaluate to `true`, while "false", "False", "FALSE", and "0"
|
* "TRUE", and "1" evaluate to `true`, while "false", "False", "FALSE", and "0"
|
||||||
* evaluate to `false`.
|
* evaluate to `false`.
|
||||||
* @param fieldName
|
* @param fieldName
|
||||||
* @param inputValue
|
* @param inputValue
|
||||||
*/
|
*/
|
||||||
export function boolean(fieldName: string, inputValue: any): ValidationResult {
|
function boolean(fieldName: string, inputValue: unknown): ValidationResult {
|
||||||
let castValue = !!inputValue
|
let castValue = Boolean(inputValue)
|
||||||
|
|
||||||
if ( ['true', 'True', 'TRUE', '1'].includes(inputValue) ) castValue = true
|
if ( ['true', 'True', 'TRUE', '1'].includes(String(inputValue)) ) {
|
||||||
if ( ['false', 'False', 'FALSE', '0'].includes(inputValue) ) castValue = false
|
castValue = true
|
||||||
|
}
|
||||||
|
if ( ['false', 'False', 'FALSE', '0'].includes(String(inputValue)) ) {
|
||||||
|
castValue = false
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
valid: true,
|
valid: true,
|
||||||
castValue,
|
castValue,
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Casts the input value to a string. */
|
/** Casts the input value to a string. */
|
||||||
export function string(fieldName: string, inputValue: any): ValidationResult {
|
function string(fieldName: string, inputValue: unknown): ValidationResult {
|
||||||
|
return {
|
||||||
|
valid: true,
|
||||||
|
castValue: String(inputValue),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Casts the input value to a number, if it is numerical. Fails otherwise. */
|
||||||
|
function numeric(fieldName: string, inputValue: unknown): ValidationResult {
|
||||||
|
if ( !isNaN(parseFloat(String(inputValue))) ) {
|
||||||
return {
|
return {
|
||||||
valid: true,
|
valid: true,
|
||||||
castValue: String(inputValue),
|
castValue: parseFloat(String(inputValue)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Casts the input value to a number, if it is numerical. Fails otherwise. */
|
return {
|
||||||
export function numeric(fieldName: string, inputValue: any): ValidationResult {
|
valid: false,
|
||||||
if ( !isNaN(parseFloat(inputValue)) ) {
|
message: 'must be numeric',
|
||||||
return {
|
}
|
||||||
valid: true,
|
}
|
||||||
castValue: parseFloat(inputValue)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/** Casts the input value to an integer. Fails otherwise. */
|
||||||
|
function integer(fieldName: string, inputValue: unknown): ValidationResult {
|
||||||
|
if ( !isNaN(parseInt(String(inputValue), 10)) ) {
|
||||||
return {
|
return {
|
||||||
valid: false,
|
valid: true,
|
||||||
message: 'must be numeric',
|
castValue: parseInt(String(inputValue), 10),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Casts the input value to an integer. Fails otherwise. */
|
return {
|
||||||
export function integer(fieldName: string, inputValue: any): ValidationResult {
|
valid: false,
|
||||||
if ( !isNaN(parseInt(inputValue)) ) {
|
message: 'must be an integer',
|
||||||
return {
|
|
||||||
valid: true,
|
|
||||||
castValue: parseInt(inputValue)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
valid: false,
|
|
||||||
message: 'must be an integer',
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const Cast = {
|
||||||
|
infer,
|
||||||
|
boolean,
|
||||||
|
string,
|
||||||
|
numeric,
|
||||||
|
integer,
|
||||||
|
}
|
||||||
|
@ -1,197 +1,210 @@
|
|||||||
import {ValidationResult, ValidatorFunction} from "./types";
|
import {ValidationResult, ValidatorFunction} from './types'
|
||||||
|
|
||||||
export namespace Num {
|
/**
|
||||||
/**
|
* Builds a validator function that requires the input value to be greater than some value.
|
||||||
* Builds a validator function that requires the input value to be greater than some value.
|
* @param value
|
||||||
* @param value
|
*/
|
||||||
*/
|
function greaterThan(value: number): ValidatorFunction {
|
||||||
export function greaterThan(value: number): ValidatorFunction {
|
return (fieldName: string, inputValue: unknown): ValidationResult => {
|
||||||
return function greaterThan(fieldName: string, inputValue: any): ValidationResult {
|
if ( Number(inputValue) > value ) {
|
||||||
if ( inputValue > value ) {
|
return { valid: true }
|
||||||
return { valid: true }
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
valid: false,
|
|
||||||
message: `must be greater than ${value}`
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
return {
|
||||||
* Builds a validator function that requires the input value to be at least some value.
|
valid: false,
|
||||||
* @param value
|
message: `must be greater than ${value}`,
|
||||||
*/
|
|
||||||
export function atLeast(value: number): ValidatorFunction {
|
|
||||||
return function atLeast(fieldName: string, inputValue: any): ValidationResult {
|
|
||||||
if ( inputValue >= value ) {
|
|
||||||
return { valid: true }
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
valid: false,
|
|
||||||
message: `must be at least ${value}`
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builds a validator function that requires the input value to be less than some value.
|
* Builds a validator function that requires the input value to be at least some value.
|
||||||
* @param value
|
* @param value
|
||||||
*/
|
*/
|
||||||
export function lessThan(value: number): ValidatorFunction {
|
function atLeast(value: number): ValidatorFunction {
|
||||||
return function lessThan(fieldName: string, inputValue: any): ValidationResult {
|
return (fieldName: string, inputValue: unknown): ValidationResult => {
|
||||||
if ( inputValue < value ) {
|
if ( Number(inputValue) >= value ) {
|
||||||
return { valid: true }
|
return { valid: true }
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
valid: false,
|
|
||||||
message: `must be less than ${value}`
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
return {
|
||||||
* Builds a validator function that requires the input value to be at most some value.
|
valid: false,
|
||||||
* @param value
|
message: `must be at least ${value}`,
|
||||||
*/
|
|
||||||
export function atMost(value: number): ValidatorFunction {
|
|
||||||
return function atMost(fieldName: string, inputValue: any): ValidationResult {
|
|
||||||
if ( inputValue <= value ) {
|
|
||||||
return { valid: true }
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
valid: false,
|
|
||||||
message: `must be at most ${value}`
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builds a validator function that requires the input value to have exactly `num` many digits.
|
* Builds a validator function that requires the input value to be less than some value.
|
||||||
* @param num
|
* @param value
|
||||||
*/
|
*/
|
||||||
export function digits(num: number): ValidatorFunction {
|
function lessThan(value: number): ValidatorFunction {
|
||||||
return function digits(fieldName: string, inputValue: any): ValidationResult {
|
return (fieldName: string, inputValue: unknown): ValidationResult => {
|
||||||
if ( String(inputValue).replace('.', '').length === num ) {
|
if ( Number(inputValue) < value ) {
|
||||||
return { valid: true }
|
return { valid: true }
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
valid: false,
|
valid: false,
|
||||||
message: `must have exactly ${num} digits`
|
message: `must be less than ${value}`,
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds a validator function that requires the input value to be at most some value.
|
||||||
|
* @param value
|
||||||
|
*/
|
||||||
|
function atMost(value: number): ValidatorFunction {
|
||||||
|
return (fieldName: string, inputValue: unknown): ValidationResult => {
|
||||||
|
if ( Number(inputValue) <= value ) {
|
||||||
|
return { valid: true }
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
return {
|
||||||
* Builds a validator function that requires the input value to have at least `num` many digits.
|
valid: false,
|
||||||
* @param num
|
message: `must be at most ${value}`,
|
||||||
*/
|
|
||||||
export function digitsMin(num: number): ValidatorFunction {
|
|
||||||
return function digitsMin(fieldName: string, inputValue: any): ValidationResult {
|
|
||||||
if ( String(inputValue).replace('.', '').length >= num ) {
|
|
||||||
return { valid: true }
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
valid: false,
|
|
||||||
message: `must have at least ${num} digits`
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builds a validator function that requires the input value to have at most `num` many digits.
|
* Builds a validator function that requires the input value to have exactly `num` many digits.
|
||||||
* @param num
|
* @param num
|
||||||
*/
|
*/
|
||||||
export function digitsMax(num: number): ValidatorFunction {
|
function digits(num: number): ValidatorFunction {
|
||||||
return function digitsMax(fieldName: string, inputValue: any): ValidationResult {
|
return (fieldName: string, inputValue: unknown): ValidationResult => {
|
||||||
if ( String(inputValue).replace('.', '').length <= num ) {
|
if ( String(inputValue).replace('.', '').length === num ) {
|
||||||
return { valid: true }
|
return { valid: true }
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
valid: false,
|
valid: false,
|
||||||
message: `must have at most ${num} digits`
|
message: `must have exactly ${num} digits`,
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builds a validator function that requires the input value to end with the given number sequence.
|
* Builds a validator function that requires the input value to have at least `num` many digits.
|
||||||
* @param num
|
* @param num
|
||||||
*/
|
*/
|
||||||
export function ends(num: number): ValidatorFunction {
|
function digitsMin(num: number): ValidatorFunction {
|
||||||
return function ends(fieldName: string, inputValue: any): ValidationResult {
|
return (fieldName: string, inputValue: unknown): ValidationResult => {
|
||||||
if ( String(inputValue).endsWith(String(num)) ) {
|
if ( String(inputValue).replace('.', '').length >= num ) {
|
||||||
return { valid: true }
|
return { valid: true }
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
valid: false,
|
valid: false,
|
||||||
message: `must end with "${num}"`
|
message: `must have at least ${num} digits`,
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builds a validator function that requires the input value to begin with the given number sequence.
|
* Builds a validator function that requires the input value to have at most `num` many digits.
|
||||||
* @param num
|
* @param num
|
||||||
*/
|
*/
|
||||||
export function begins(num: number): ValidatorFunction {
|
function digitsMax(num: number): ValidatorFunction {
|
||||||
return function begins(fieldName: string, inputValue: any): ValidationResult {
|
return (fieldName: string, inputValue: unknown): ValidationResult => {
|
||||||
if ( String(inputValue).startsWith(String(num)) ) {
|
if ( String(inputValue).replace('.', '').length <= num ) {
|
||||||
return { valid: true }
|
return { valid: true }
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
valid: false,
|
valid: false,
|
||||||
message: `must begin with "${num}"`
|
message: `must have at most ${num} digits`,
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builds a validator function that requires the input value to be a multiple of the given number.
|
* Builds a validator function that requires the input value to end with the given number sequence.
|
||||||
* @param num
|
* @param num
|
||||||
*/
|
*/
|
||||||
export function multipleOf(num: number): ValidatorFunction {
|
function ends(num: number): ValidatorFunction {
|
||||||
return function multipleOf(fieldName: string, inputValue: any): ValidationResult {
|
return (fieldName: string, inputValue: unknown): ValidationResult => {
|
||||||
if ( inputValue % num === 0 ) {
|
if ( String(inputValue).endsWith(String(num)) ) {
|
||||||
return { valid: true }
|
return { valid: true }
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
valid: false,
|
valid: false,
|
||||||
message: `must be a multiple of ${num}`
|
message: `must end with "${num}"`,
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Requires the input value to be even. */
|
/**
|
||||||
export function even(fieldName: string, inputValue: any): ValidationResult {
|
* Builds a validator function that requires the input value to begin with the given number sequence.
|
||||||
if ( inputValue % 2 === 0 ) {
|
* @param num
|
||||||
|
*/
|
||||||
|
function begins(num: number): ValidatorFunction {
|
||||||
|
return (fieldName: string, inputValue: unknown): ValidationResult => {
|
||||||
|
if ( String(inputValue).startsWith(String(num)) ) {
|
||||||
return { valid: true }
|
return { valid: true }
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
valid: false,
|
valid: false,
|
||||||
message: 'must be even',
|
message: `must begin with "${num}"`,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Requires the input value to be odd. */
|
/**
|
||||||
export function odd(fieldName: string, inputValue: any): ValidationResult {
|
* Builds a validator function that requires the input value to be a multiple of the given number.
|
||||||
if ( inputValue % 2 === 0 ) {
|
* @param num
|
||||||
|
*/
|
||||||
|
function multipleOf(num: number): ValidatorFunction {
|
||||||
|
return (fieldName: string, inputValue: unknown): ValidationResult => {
|
||||||
|
if ( parseFloat(String(inputValue)) % num === 0 ) {
|
||||||
return { valid: true }
|
return { valid: true }
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
valid: false,
|
valid: false,
|
||||||
message: 'must be odd',
|
message: `must be a multiple of ${num}`,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Requires the input value to be even. */
|
||||||
|
function even(fieldName: string, inputValue: unknown): ValidationResult {
|
||||||
|
if ( parseFloat(String(inputValue)) % 2 === 0 ) {
|
||||||
|
return { valid: true }
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
valid: false,
|
||||||
|
message: 'must be even',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Requires the input value to be odd. */
|
||||||
|
function odd(fieldName: string, inputValue: unknown): ValidationResult {
|
||||||
|
if ( parseFloat(String(inputValue)) % 2 === 0 ) {
|
||||||
|
return { valid: true }
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
valid: false,
|
||||||
|
message: 'must be odd',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const Num = {
|
||||||
|
greaterThan,
|
||||||
|
atLeast,
|
||||||
|
lessThan,
|
||||||
|
atMost,
|
||||||
|
digits,
|
||||||
|
digitsMin,
|
||||||
|
digitsMax,
|
||||||
|
ends,
|
||||||
|
begins,
|
||||||
|
multipleOf,
|
||||||
|
even,
|
||||||
|
odd,
|
||||||
|
}
|
||||||
|
@ -1,175 +1,191 @@
|
|||||||
import {ValidationResult, ValidatorFunction} from "./types";
|
import {ValidationResult, ValidatorFunction} from './types'
|
||||||
import {UniversalPath} from '../../util'
|
import {UniversalPath} from '../../util'
|
||||||
|
|
||||||
export namespace Is {
|
/** Requires the given input value to be some form of affirmative boolean. */
|
||||||
/** Requires the given input value to be some form of affirmative boolean. */
|
function accepted(fieldName: string, inputValue: unknown): ValidationResult {
|
||||||
export function accepted(fieldName: string, inputValue: any): ValidationResult {
|
if ( ['yes', 'Yes', 'YES', 1, true, 'true', 'True', 'TRUE'].includes(String(inputValue)) ) {
|
||||||
if ( ['yes', 'Yes', 'YES', 1, true, 'true', 'True', 'TRUE'].includes(inputValue) ) {
|
return { valid: true }
|
||||||
return { valid: true }
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
valid: false,
|
|
||||||
message: 'must be accepted'
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Requires the given input value to be some form of boolean. */
|
return {
|
||||||
export function boolean(fieldName: string, inputValue: any): ValidationResult {
|
valid: false,
|
||||||
const boolish = ['true', 'True', 'TRUE', '1', 'false', 'False', 'FALSE', '0', true, false, 1, 0]
|
message: 'must be accepted',
|
||||||
if ( boolish.includes(inputValue) ) {
|
|
||||||
return { valid: true }
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
valid: false,
|
|
||||||
message: 'must be true or false'
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Requires the input value to be of type string. */
|
/** Requires the given input value to be some form of boolean. */
|
||||||
export function string(fieldName: string, inputValue: any): ValidationResult {
|
function boolean(fieldName: string, inputValue: unknown): ValidationResult {
|
||||||
if ( typeof inputValue === 'string' ) {
|
const boolish = ['true', 'True', 'TRUE', '1', 'false', 'False', 'FALSE', '0', true, false, 1, 0]
|
||||||
return { valid: true }
|
if ( boolish.includes(String(inputValue)) ) {
|
||||||
}
|
return { valid: true }
|
||||||
|
|
||||||
return {
|
|
||||||
valid: false,
|
|
||||||
message: 'must be a string'
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Requires the given input value to be present and non-nullish. */
|
return {
|
||||||
export function required(fieldName: string, inputValue: any): ValidationResult {
|
valid: false,
|
||||||
if ( typeof inputValue !== 'undefined' && inputValue !== null && inputValue !== '' ) {
|
message: 'must be true or false',
|
||||||
return { valid: true }
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
valid: false,
|
|
||||||
message: 'is required',
|
|
||||||
stopValidation: true,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Alias of required(). */
|
/** Requires the input value to be of type string. */
|
||||||
export function present(fieldName: string, inputValue: any): ValidationResult {
|
function string(fieldName: string, inputValue: unknown): ValidationResult {
|
||||||
return required(fieldName, inputValue)
|
if ( typeof inputValue === 'string' ) {
|
||||||
|
return { valid: true }
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Alias of required(). */
|
return {
|
||||||
export function filled(fieldName: string, inputValue: any): ValidationResult {
|
valid: false,
|
||||||
return required(fieldName, inputValue)
|
message: 'must be a string',
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Requires the given input value to be absent or nullish. */
|
/** Requires the given input value to be present and non-nullish. */
|
||||||
export function prohibited(fieldName: string, inputValue: any): ValidationResult {
|
function required(fieldName: string, inputValue: unknown): ValidationResult {
|
||||||
if ( typeof inputValue === 'undefined' || inputValue === null || inputValue === '' ) {
|
if ( typeof inputValue !== 'undefined' && inputValue !== null && inputValue !== '' ) {
|
||||||
return { valid: true }
|
return { valid: true }
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
valid: false,
|
|
||||||
message: 'is not allowed',
|
|
||||||
stopValidation: true,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Alias of prohibited(). */
|
return {
|
||||||
export function absent(fieldName: string, inputValue: any): ValidationResult {
|
valid: false,
|
||||||
return prohibited(fieldName, inputValue)
|
message: 'is required',
|
||||||
|
stopValidation: true,
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Alias of prohibited(). */
|
/** Alias of required(). */
|
||||||
export function empty(fieldName: string, inputValue: any): ValidationResult {
|
function present(fieldName: string, inputValue: unknown): ValidationResult {
|
||||||
return prohibited(fieldName, inputValue)
|
return required(fieldName, inputValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Alias of required(). */
|
||||||
* Builds a validator function that requires the given input to be found in an array of values.
|
function filled(fieldName: string, inputValue: unknown): ValidationResult {
|
||||||
* @param values
|
return required(fieldName, inputValue)
|
||||||
*/
|
}
|
||||||
export function foundIn(values: any[]): ValidatorFunction {
|
|
||||||
return function foundIn(fieldName: string, inputValue: any): ValidationResult {
|
|
||||||
if ( values.includes(inputValue) ) {
|
|
||||||
return { valid: true }
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
/** Requires the given input value to be absent or nullish. */
|
||||||
valid: false,
|
function prohibited(fieldName: string, inputValue: unknown): ValidationResult {
|
||||||
message: `must be one of: ${values.join(', ')}`
|
if ( typeof inputValue === 'undefined' || inputValue === null || inputValue === '' ) {
|
||||||
}
|
return { valid: true }
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
return {
|
||||||
* Builds a validator function that requires the given input NOT to be found in an array of values.
|
valid: false,
|
||||||
* @param values
|
message: 'is not allowed',
|
||||||
*/
|
stopValidation: true,
|
||||||
export function notFoundIn(values: any[]): ValidatorFunction {
|
|
||||||
return function foundIn(fieldName: string, inputValue: any): ValidationResult {
|
|
||||||
if ( values.includes(inputValue) ) {
|
|
||||||
return { valid: true }
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
valid: false,
|
|
||||||
message: `must be one of: ${values.join(', ')}`
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Alias of prohibited(). */
|
||||||
|
function absent(fieldName: string, inputValue: unknown): ValidationResult {
|
||||||
|
return prohibited(fieldName, inputValue)
|
||||||
|
}
|
||||||
|
|
||||||
/** Requires the input value to be number-like. */
|
/** Alias of prohibited(). */
|
||||||
export function numeric(fieldName: string, inputValue: any): ValidationResult {
|
function empty(fieldName: string, inputValue: unknown): ValidationResult {
|
||||||
if ( !isNaN(parseFloat(inputValue)) ) {
|
return prohibited(fieldName, inputValue)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds a validator function that requires the given input to be found in an array of values.
|
||||||
|
* @param values
|
||||||
|
*/
|
||||||
|
function foundIn(values: any[]): ValidatorFunction {
|
||||||
|
return (fieldName: string, inputValue: unknown): ValidationResult => {
|
||||||
|
if ( values.includes(inputValue) ) {
|
||||||
return { valid: true }
|
return { valid: true }
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
valid: false,
|
valid: false,
|
||||||
message: 'must be numeric',
|
message: `must be one of: ${values.join(', ')}`,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Requires the given input value to be integer-like. */
|
/**
|
||||||
export function integer(fieldName: string, inputValue: any): ValidationResult {
|
* Builds a validator function that requires the given input NOT to be found in an array of values.
|
||||||
if ( !isNaN(parseInt(inputValue)) && parseInt(inputValue) === parseFloat(inputValue) ) {
|
* @param values
|
||||||
|
*/
|
||||||
|
function notFoundIn(values: any[]): ValidatorFunction {
|
||||||
|
return (fieldName: string, inputValue: unknown): ValidationResult => {
|
||||||
|
if ( values.includes(inputValue) ) {
|
||||||
return { valid: true }
|
return { valid: true }
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
valid: false,
|
valid: false,
|
||||||
message: 'must be an integer',
|
message: `must be one of: ${values.join(', ')}`,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Requires the given input value to be a UniversalPath. */
|
/** Requires the input value to be number-like. */
|
||||||
export function file(fieldName: string, inputValue: any): ValidationResult {
|
function numeric(fieldName: string, inputValue: unknown): ValidationResult {
|
||||||
if ( inputValue instanceof UniversalPath ) {
|
if ( !isNaN(parseFloat(String(inputValue))) ) {
|
||||||
return { valid: true }
|
return { valid: true }
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
valid: false,
|
valid: false,
|
||||||
message: 'must be a file'
|
message: 'must be numeric',
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/** Requires the given input value to be integer-like. */
|
||||||
* A special validator function that marks a field as optional.
|
function integer(fieldName: string, inputValue: unknown): ValidationResult {
|
||||||
* If the value of the field is nullish, no further validation rules will be applied.
|
if ( !isNaN(parseInt(String(inputValue), 10)) && parseInt(String(inputValue), 10) === parseFloat(String(inputValue)) ) {
|
||||||
* If it is non-nullish, validation will continue.
|
return { valid: true }
|
||||||
* @param fieldName
|
}
|
||||||
* @param inputValue
|
|
||||||
*/
|
|
||||||
export function optional(fieldName: string, inputValue: any): ValidationResult {
|
|
||||||
if ( inputValue ?? true ) {
|
|
||||||
return {
|
|
||||||
valid: true,
|
|
||||||
stopValidation: true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
return {
|
||||||
|
valid: false,
|
||||||
|
message: 'must be an integer',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Requires the given input value to be a UniversalPath. */
|
||||||
|
function file(fieldName: string, inputValue: unknown): ValidationResult {
|
||||||
|
if ( inputValue instanceof UniversalPath ) {
|
||||||
return { valid: true }
|
return { valid: true }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
valid: false,
|
||||||
|
message: 'must be a file',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A special validator function that marks a field as optional.
|
||||||
|
* If the value of the field is nullish, no further validation rules will be applied.
|
||||||
|
* If it is non-nullish, validation will continue.
|
||||||
|
* @param fieldName
|
||||||
|
* @param inputValue
|
||||||
|
*/
|
||||||
|
function optional(fieldName: string, inputValue: unknown): ValidationResult {
|
||||||
|
if ( inputValue ?? true ) {
|
||||||
|
return {
|
||||||
|
valid: true,
|
||||||
|
stopValidation: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return { valid: true }
|
||||||
|
}
|
||||||
|
|
||||||
|
export const Is = {
|
||||||
|
accepted,
|
||||||
|
boolean,
|
||||||
|
string,
|
||||||
|
required,
|
||||||
|
present,
|
||||||
|
filled,
|
||||||
|
prohibited,
|
||||||
|
absent,
|
||||||
|
empty,
|
||||||
|
foundIn,
|
||||||
|
notFoundIn,
|
||||||
|
numeric,
|
||||||
|
integer,
|
||||||
|
file,
|
||||||
|
optional,
|
||||||
}
|
}
|
||||||
|
@ -1,224 +1,245 @@
|
|||||||
import {ValidationResult, ValidatorFunction} from "./types";
|
import {ValidationResult, ValidatorFunction} from './types'
|
||||||
import {isJSON} from '../../util'
|
import {isJSON} from '../../util'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* String-related validation rules.
|
* String-related validation rules.
|
||||||
*/
|
*/
|
||||||
export namespace Str {
|
const regexes: {[key: string]: RegExp} = {
|
||||||
const regexes: {[key: string]: RegExp} = {
|
'string.is.alpha': /[a-zA-Z]*/,
|
||||||
'string.is.alpha': /[a-zA-Z]*/,
|
'string.is.alpha_num': /[a-zA-Z0-9]*/,
|
||||||
'string.is.alpha_num': /[a-zA-Z0-9]*/,
|
'string.is.alpha_dash': /[a-zA-Z-]*/,
|
||||||
'string.is.alpha_dash': /[a-zA-Z\-]*/,
|
'string.is.alpha_score': /[a-zA-Z_]*/,
|
||||||
'string.is.alpha_score': /[a-zA-Z_]*/,
|
'string.is.alpha_num_dash_score': /[a-zA-Z\-_0-9]*/,
|
||||||
'string.is.alpha_num_dash_score': /[a-zA-Z\-_0-9]*/,
|
'string.is.email': /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)])/, // eslint-disable-line no-control-regex
|
||||||
'string.is.email': /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)])/,
|
'string.is.ip': /((^\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\s*$)|(^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$))/,
|
||||||
'string.is.ip': /((^\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\s*$)|(^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$))/,
|
'string.is.ip.v4': /(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}/,
|
||||||
'string.is.ip.v4': /(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}/,
|
'string.is.ip.v6': /(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]+|::(ffff(:0{1,4})?:)?((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9]))/,
|
||||||
'string.is.ip.v6': /(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]+|::(ffff(:0{1,4})?:)?((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9]))/,
|
'string.is.mime': /^(?=[-a-z]{1,127}\/[-.a-z0-9]{1,127}$)[a-z]+(-[a-z]+)*\/[a-z0-9]+([-.][a-z0-9]+)*$/,
|
||||||
'string.is.mime': /^(?=[-a-z]{1,127}\/[-.a-z0-9]{1,127}$)[a-z]+(-[a-z]+)*\/[a-z0-9]+([-.][a-z0-9]+)*$/,
|
'string.is.url': /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=+$,\w]+@)?[A-Za-z0-9.-]+|(?:www\.|[-;:&=+$,\w]+@)[A-Za-z0-9.-]+)((?:\/[+~%/.\w\-_]*)?\??(?:[-+=&;%@.\w_]*)#?(?:[.!/\\\w]*))?)/,
|
||||||
'string.is.url': /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=+$,\w]+@)?[A-Za-z0-9.\-]+|(?:www\.|[\-;:&=+$,\w]+@)[A-Za-z0-9.\-]+)((?:\/[+~%\/.\w\-_]*)?\??(?:[\-+=&;%@.\w_]*)#?(?:[.!\/\\\w]*))?)/,
|
'string.is.uuid': /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/,
|
||||||
'string.is.uuid': /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/,
|
}
|
||||||
}
|
|
||||||
|
|
||||||
function validateRex(key: string, inputValue: any, message: string): ValidationResult {
|
|
||||||
if ( regexes[key].test(inputValue) ) {
|
|
||||||
return { valid: true }
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
function validateRex(key: string, inputValue: unknown, message: string): ValidationResult {
|
||||||
valid: false,
|
if ( regexes[key].test(String(inputValue)) ) {
|
||||||
message
|
return { valid: true }
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Requires the input value to be alphabetical characters only. */
|
return {
|
||||||
export function alpha(fieldName: string, inputValue: any): ValidationResult {
|
valid: false,
|
||||||
return validateRex('string.is.alpha', inputValue, 'must be alphabetical only')
|
message,
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Requires the input value to be alphanumeric characters only. */
|
/** Requires the input value to be alphabetical characters only. */
|
||||||
export function alphaNum(fieldName: string, inputValue: any): ValidationResult {
|
function alpha(fieldName: string, inputValue: unknown): ValidationResult {
|
||||||
return validateRex('string.is.alpha_num', inputValue, 'must be alphanumeric only')
|
return validateRex('string.is.alpha', inputValue, 'must be alphabetical only')
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Requires the input value to be alphabetical characters or the "-" character only. */
|
/** Requires the input value to be alphanumeric characters only. */
|
||||||
export function alphaDash(fieldName: string, inputValue: any): ValidationResult {
|
function alphaNum(fieldName: string, inputValue: unknown): ValidationResult {
|
||||||
return validateRex('string.is.alpha_dash', inputValue, 'must be alphabetical and dashes only')
|
return validateRex('string.is.alpha_num', inputValue, 'must be alphanumeric only')
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Requires the input value to be alphabetical characters or the "_" character only. */
|
/** Requires the input value to be alphabetical characters or the "-" character only. */
|
||||||
export function alphaScore(fieldName: string, inputValue: any): ValidationResult {
|
function alphaDash(fieldName: string, inputValue: unknown): ValidationResult {
|
||||||
return validateRex('string.is.alpha_score', inputValue, 'must be alphabetical and underscores only')
|
return validateRex('string.is.alpha_dash', inputValue, 'must be alphabetical and dashes only')
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Requires the input value to be alphabetical characters, numeric characters, "-", or "_" only. */
|
/** Requires the input value to be alphabetical characters or the "_" character only. */
|
||||||
export function alphaNumDashScore(fieldName: string, inputValue: any): ValidationResult {
|
function alphaScore(fieldName: string, inputValue: unknown): ValidationResult {
|
||||||
return validateRex('string.is.alpha_num_dash_score', inputValue, 'must be alphanumeric, dashes, and underscores only')
|
return validateRex('string.is.alpha_score', inputValue, 'must be alphabetical and underscores only')
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Requires the input value to be a valid RFC email address format. */
|
/** Requires the input value to be alphabetical characters, numeric characters, "-", or "_" only. */
|
||||||
export function email(fieldName: string, inputValue: any): ValidationResult {
|
function alphaNumDashScore(fieldName: string, inputValue: unknown): ValidationResult {
|
||||||
return validateRex('string.is.email', inputValue, 'must be an email address')
|
return validateRex('string.is.alpha_num_dash_score', inputValue, 'must be alphanumeric, dashes, and underscores only')
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Requires the input value to be a valid IPv4 or IPv6 address. */
|
/** Requires the input value to be a valid RFC email address format. */
|
||||||
export function ip(fieldName: string, inputValue: any): ValidationResult {
|
function email(fieldName: string, inputValue: unknown): ValidationResult {
|
||||||
return validateRex('string.is.ip', inputValue, 'must be a valid IP address')
|
return validateRex('string.is.email', inputValue, 'must be an email address')
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Requires the input value to be a valid IPv4 address. */
|
/** Requires the input value to be a valid IPv4 or IPv6 address. */
|
||||||
export function ipv4(fieldName: string, inputValue: any): ValidationResult {
|
function ip(fieldName: string, inputValue: unknown): ValidationResult {
|
||||||
return validateRex('string.is.ip.v4', inputValue, 'must be a valid IP version 4 address')
|
return validateRex('string.is.ip', inputValue, 'must be a valid IP address')
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Requires the input value to be a valid IPv6 address. */
|
/** Requires the input value to be a valid IPv4 address. */
|
||||||
export function ipv6(fieldName: string, inputValue: any): ValidationResult {
|
function ipv4(fieldName: string, inputValue: unknown): ValidationResult {
|
||||||
return validateRex('string.is.ip.v6', inputValue, 'must be a valid IP version 6 address')
|
return validateRex('string.is.ip.v4', inputValue, 'must be a valid IP version 4 address')
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Requires the input value to be a valid file MIME type. */
|
/** Requires the input value to be a valid IPv6 address. */
|
||||||
export function mime(fieldName: string, inputValue: any): ValidationResult {
|
function ipv6(fieldName: string, inputValue: unknown): ValidationResult {
|
||||||
return validateRex('string.is.mime', inputValue, 'must be a valid MIME-type')
|
return validateRex('string.is.ip.v6', inputValue, 'must be a valid IP version 6 address')
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Requires the input value to be a valid RFC URL format. */
|
/** Requires the input value to be a valid file MIME type. */
|
||||||
export function url(fieldName: string, inputValue: any): ValidationResult {
|
function mime(fieldName: string, inputValue: unknown): ValidationResult {
|
||||||
return validateRex('string.is.url', inputValue, 'must be a valid URL')
|
return validateRex('string.is.mime', inputValue, 'must be a valid MIME-type')
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Requires the input value to be a valid RFC UUID format. */
|
/** Requires the input value to be a valid RFC URL format. */
|
||||||
export function uuid(fieldName: string, inputValue: any): ValidationResult {
|
function url(fieldName: string, inputValue: unknown): ValidationResult {
|
||||||
return validateRex('string.is.uuid', inputValue, 'must be a valid UUID')
|
return validateRex('string.is.url', inputValue, 'must be a valid URL')
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Requires the input value to be a valid RFC UUID format. */
|
||||||
* Builds a validation function that requires the input value to match the given regex.
|
function uuid(fieldName: string, inputValue: unknown): ValidationResult {
|
||||||
* @param rex
|
return validateRex('string.is.uuid', inputValue, 'must be a valid UUID')
|
||||||
*/
|
}
|
||||||
export function regex(rex: RegExp): ValidatorFunction {
|
|
||||||
return function regex(fieldName: string, inputValue: any): ValidationResult {
|
/**
|
||||||
if ( rex.test(inputValue) ) {
|
* Builds a validation function that requires the input value to match the given regex.
|
||||||
return { valid: true }
|
* @param rex
|
||||||
}
|
*/
|
||||||
|
function regex(rex: RegExp): ValidatorFunction {
|
||||||
return {
|
return (fieldName: string, inputValue: unknown): ValidationResult => {
|
||||||
valid: false,
|
if ( rex.test(String(inputValue)) ) {
|
||||||
message: 'is not valid'
|
return { valid: true }
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
return {
|
||||||
* Builds a validation function that requires the input to NOT match the given regex.
|
valid: false,
|
||||||
* @param rex
|
message: 'is not valid',
|
||||||
*/
|
|
||||||
export function notRegex(rex: RegExp): ValidatorFunction {
|
|
||||||
return function notRegex(fieldName: string, inputValue: any): ValidationResult {
|
|
||||||
if ( !rex.test(inputValue) ) {
|
|
||||||
return { valid: true }
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
valid: false,
|
|
||||||
message: 'is not valid'
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builds a validation function that requires the given input to end with the substring.
|
* Builds a validation function that requires the input to NOT match the given regex.
|
||||||
* @param substr
|
* @param rex
|
||||||
*/
|
*/
|
||||||
export function ends(substr: string): ValidatorFunction {
|
function notRegex(rex: RegExp): ValidatorFunction {
|
||||||
return function ends(fieldName: string, inputValue: any): ValidationResult {
|
return (fieldName: string, inputValue: unknown): ValidationResult => {
|
||||||
if ( String(inputValue).endsWith(substr) ) {
|
if ( !rex.test(String(inputValue)) ) {
|
||||||
return { valid: true }
|
return { valid: true }
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
valid: false,
|
valid: false,
|
||||||
message: `must end with "${substr}"`
|
message: 'is not valid',
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builds a validation function that requires the given input to begin with the substring.
|
* Builds a validation function that requires the given input to end with the substring.
|
||||||
* @param substr
|
* @param substr
|
||||||
*/
|
*/
|
||||||
export function begins(substr: string): ValidatorFunction {
|
function ends(substr: string): ValidatorFunction {
|
||||||
return function begins(fieldName: string, inputValue: any): ValidationResult {
|
return (fieldName: string, inputValue: unknown): ValidationResult => {
|
||||||
if ( String(inputValue).startsWith(substr) ) {
|
if ( String(inputValue).endsWith(substr) ) {
|
||||||
return { valid: true }
|
return { valid: true }
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
valid: false,
|
valid: false,
|
||||||
message: `must begin with "${substr}"`
|
message: `must end with "${substr}"`,
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Requires the input value to be a valid JSON string. */
|
/**
|
||||||
export function json(fieldName: string, inputValue: any): ValidationResult {
|
* Builds a validation function that requires the given input to begin with the substring.
|
||||||
if ( isJSON(inputValue) ) {
|
* @param substr
|
||||||
|
*/
|
||||||
|
function begins(substr: string): ValidatorFunction {
|
||||||
|
return (fieldName: string, inputValue: unknown): ValidationResult => {
|
||||||
|
if ( String(inputValue).startsWith(substr) ) {
|
||||||
return { valid: true }
|
return { valid: true }
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
valid: false,
|
valid: false,
|
||||||
message: 'must be valid JSON'
|
message: `must begin with "${substr}"`,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Requires the input value to be a valid JSON string. */
|
||||||
|
function json(fieldName: string, inputValue: unknown): ValidationResult {
|
||||||
|
if ( isJSON(String(inputValue)) ) {
|
||||||
|
return { valid: true }
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
valid: false,
|
||||||
|
message: 'must be valid JSON',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds a validator function that requires the input value to have exactly len many characters.
|
||||||
|
* @param len
|
||||||
|
*/
|
||||||
|
function length(len: number): ValidatorFunction {
|
||||||
|
return (fieldName: string, inputValue: unknown): ValidationResult => {
|
||||||
|
if ( String(inputValue).length === len ) {
|
||||||
|
return { valid: true }
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
return {
|
||||||
* Builds a validator function that requires the input value to have exactly len many characters.
|
valid: false,
|
||||||
* @param len
|
message: `must be exactly of length ${len}`,
|
||||||
*/
|
|
||||||
export function length(len: number): ValidatorFunction {
|
|
||||||
return function length(fieldName: string, inputValue: any): ValidationResult {
|
|
||||||
if ( inputValue.length === len ) {
|
|
||||||
return { valid: true }
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
valid: false,
|
|
||||||
message: `must be exactly of length ${len}`
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builds a validator function that requires the input value to have at least len many characters.
|
* Builds a validator function that requires the input value to have at least len many characters.
|
||||||
* @param len
|
* @param len
|
||||||
*/
|
*/
|
||||||
export function lengthMin(len: number): ValidatorFunction {
|
function lengthMin(len: number): ValidatorFunction {
|
||||||
return function lengthMin(fieldName: string, inputValue: any): ValidationResult {
|
return (fieldName: string, inputValue: unknown): ValidationResult => {
|
||||||
if ( inputValue.length >= len ) {
|
if ( String(inputValue).length >= len ) {
|
||||||
return { valid: true }
|
return { valid: true }
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
valid: false,
|
valid: false,
|
||||||
message: `must be at least length ${len}`
|
message: `must be at least length ${len}`,
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builds a validator function that requires the input value to have at most len many characters.
|
* Builds a validator function that requires the input value to have at most len many characters.
|
||||||
* @param len
|
* @param len
|
||||||
*/
|
*/
|
||||||
export function lengthMax(len: number): ValidatorFunction {
|
function lengthMax(len: number): ValidatorFunction {
|
||||||
return function lengthMax(fieldName: string, inputValue: any): ValidationResult {
|
return (fieldName: string, inputValue: unknown): ValidationResult => {
|
||||||
if ( inputValue.length <= len ) {
|
if ( String(inputValue).length <= len ) {
|
||||||
return { valid: true }
|
return { valid: true }
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
valid: false,
|
valid: false,
|
||||||
message: `must be at most length ${len}`
|
message: `must be at most length ${len}`,
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const Str = {
|
||||||
|
alpha,
|
||||||
|
alphaNum,
|
||||||
|
alphaDash,
|
||||||
|
alphaScore,
|
||||||
|
alphaNumDashScore,
|
||||||
|
email,
|
||||||
|
ip,
|
||||||
|
ipv4,
|
||||||
|
ipv6,
|
||||||
|
mime,
|
||||||
|
url,
|
||||||
|
uuid,
|
||||||
|
regex,
|
||||||
|
notRegex,
|
||||||
|
ends,
|
||||||
|
begins,
|
||||||
|
json,
|
||||||
|
length,
|
||||||
|
lengthMin,
|
||||||
|
lengthMax,
|
||||||
|
}
|
||||||
|
@ -1,23 +1,23 @@
|
|||||||
import {ErrorWithContext} from "../../util";
|
import {ErrorWithContext} from '../../util'
|
||||||
import {Container} from "../../di";
|
import {Container} from '../../di'
|
||||||
import {ResultIterable} from "./result/ResultIterable";
|
import {ResultIterable} from './result/ResultIterable'
|
||||||
import {QueryRow} from "../types";
|
import {QueryRow} from '../types'
|
||||||
import {AbstractBuilder} from "./AbstractBuilder";
|
import {AbstractBuilder} from './AbstractBuilder'
|
||||||
import {AbstractResultIterable} from "./result/AbstractResultIterable";
|
import {AbstractResultIterable} from './result/AbstractResultIterable'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implementation of the abstract builder class that returns simple QueryRow objects.
|
* Implementation of the abstract builder class that returns simple QueryRow objects.
|
||||||
*/
|
*/
|
||||||
export class Builder extends AbstractBuilder<QueryRow> {
|
export class Builder extends AbstractBuilder<QueryRow> {
|
||||||
public getNewInstance(): AbstractBuilder<QueryRow> {
|
public getNewInstance(): AbstractBuilder<QueryRow> {
|
||||||
return Container.getContainer().make<Builder>(Builder);
|
return Container.getContainer().make<Builder>(Builder)
|
||||||
}
|
}
|
||||||
|
|
||||||
public getResultIterable(): AbstractResultIterable<QueryRow> {
|
public getResultIterable(): AbstractResultIterable<QueryRow> {
|
||||||
if ( !this._connection ) {
|
if ( !this.registeredConnection ) {
|
||||||
throw new ErrorWithContext(`No connection specified to fetch iterator for query.`)
|
throw new ErrorWithContext(`No connection specified to fetch iterator for query.`)
|
||||||
}
|
}
|
||||||
|
|
||||||
return Container.getContainer().make<ResultIterable>(ResultIterable, this, this._connection)
|
return Container.getContainer().make<ResultIterable>(ResultIterable, this, this.registeredConnection)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue