diff --git a/rules/date.js b/rules/date.js index bad3a75..82cb925 100644 --- a/rules/date.js +++ b/rules/date.js @@ -1,12 +1,11 @@ import { ValidationError } from '../error'; export const date = function ( formDataTree ) { - const values = formDataTree.getAll( this.field ); + const values = formDataTree.getAll( this.field ) + .map( val => val.trim() ).filter( val => '' !== val ); // https://html.spec.whatwg.org/multipage/input.html#date-state-(type=date) const isValidDateString = text => { - text = text.trim(); - if ( ! /^[0-9]{4,}-[0-9]{2}-[0-9]{2}$/.test( text ) ) { return false; } diff --git a/rules/dayofweek.js b/rules/dayofweek.js index 45fee79..db51209 100644 --- a/rules/dayofweek.js +++ b/rules/dayofweek.js @@ -1,12 +1,13 @@ import { ValidationError } from '../error'; export const dayofweek = function ( formDataTree ) { - const values = formDataTree.getAll( this.field ); + const values = formDataTree.getAll( this.field ) + .map( val => val.trim() ).filter( val => '' !== val ); const convertToIso8601 = jsDow => ( 0 === jsDow ) ? 7 : jsDow; const isAcceptableValue = value => { - const date = new Date( value.trim() ); + const date = new Date( value ); const day = convertToIso8601( date.getDay() ); return this.accept?.some( diff --git a/rules/email.js b/rules/email.js index 475f9ce..84368d9 100644 --- a/rules/email.js +++ b/rules/email.js @@ -1,13 +1,12 @@ import { ValidationError } from '../error'; export const email = function ( formDataTree ) { - const values = formDataTree.getAll( this.field ); + const values = formDataTree.getAll( this.field ) + .map( val => val.trim() ).filter( val => '' !== val ); // Equivalent to is_email() // https://developer.wordpress.org/reference/functions/is_email/ const isValidEmail = text => { - text = text.trim(); - if ( text.length < 6 ) { return false; } diff --git a/rules/enum.js b/rules/enum.js index 271305f..167ac3a 100644 --- a/rules/enum.js +++ b/rules/enum.js @@ -1,10 +1,11 @@ import { ValidationError } from '../error'; export const enumeration = function ( formDataTree ) { - const values = formDataTree.getAll( this.field ); + const values = formDataTree.getAll( this.field ) + .map( val => val.trim() ).filter( val => '' !== val ); const isAcceptableValue = value => this.accept?.some( - acceptableValue => value.trim() === String( acceptableValue ) + acceptableValue => value === String( acceptableValue ) ); if ( ! values.every( isAcceptableValue ) ) { diff --git a/rules/maxdate.js b/rules/maxdate.js index e450a97..243eafc 100644 --- a/rules/maxdate.js +++ b/rules/maxdate.js @@ -1,11 +1,10 @@ import { ValidationError } from '../error'; export const maxdate = function ( formDataTree ) { - const values = formDataTree.getAll( this.field ); + const values = formDataTree.getAll( this.field ) + .map( val => val.trim() ).filter( val => '' !== val ); const isAcceptableDate = text => { - text = text.trim(); - if ( /^[0-9]{4,}-[0-9]{2}-[0-9]{2}$/.test( text ) && /^[0-9]{4,}-[0-9]{2}-[0-9]{2}$/.test( this.threshold ) && diff --git a/rules/maxitems.js b/rules/maxitems.js index 25e1a7e..a4a1e01 100644 --- a/rules/maxitems.js +++ b/rules/maxitems.js @@ -1,9 +1,8 @@ import { ValidationError } from '../error'; export const maxitems = function ( formDataTree ) { - const values = formDataTree.getAll( this.field ).filter( - val => '' !== val.trim() - ); + const values = formDataTree.getAll( this.field ) + .map( val => val.trim() ).filter( val => '' !== val ); if ( parseInt( this.threshold ) < values.length ) { throw new ValidationError( this ); diff --git a/rules/maxlength.js b/rules/maxlength.js index 2ea6de0..b7f5313 100644 --- a/rules/maxlength.js +++ b/rules/maxlength.js @@ -1,13 +1,14 @@ import { ValidationError } from '../error'; export const maxlength = function ( formDataTree ) { - const values = formDataTree.getAll( this.field ); + const values = formDataTree.getAll( this.field ) + .map( val => val.trim() ).filter( val => '' !== val ); let totalLength = 0; values.forEach( text => { if ( 'string' === typeof text ) { - totalLength += text.trim().length; + totalLength += text.length; } } ); diff --git a/rules/maxnumber.js b/rules/maxnumber.js index 574ab1f..4820c74 100644 --- a/rules/maxnumber.js +++ b/rules/maxnumber.js @@ -1,7 +1,8 @@ import { ValidationError } from '../error'; export const maxnumber = function ( formDataTree ) { - const values = formDataTree.getAll( this.field ); + const values = formDataTree.getAll( this.field ) + .map( val => val.trim() ).filter( val => '' !== val ); const isAcceptableNumber = text => { if ( parseFloat( this.threshold ) < parseFloat( text ) ) { diff --git a/rules/mindate.js b/rules/mindate.js index b02a5ab..c0f1f8f 100644 --- a/rules/mindate.js +++ b/rules/mindate.js @@ -1,11 +1,10 @@ import { ValidationError } from '../error'; export const mindate = function ( formDataTree ) { - const values = formDataTree.getAll( this.field ); + const values = formDataTree.getAll( this.field ) + .map( val => val.trim() ).filter( val => '' !== val ); const isAcceptableDate = text => { - text = text.trim(); - if ( /^[0-9]{4,}-[0-9]{2}-[0-9]{2}$/.test( text ) && /^[0-9]{4,}-[0-9]{2}-[0-9]{2}$/.test( this.threshold ) && diff --git a/rules/minitems.js b/rules/minitems.js index 523779c..6f93ab4 100644 --- a/rules/minitems.js +++ b/rules/minitems.js @@ -1,9 +1,8 @@ import { ValidationError } from '../error'; export const minitems = function ( formDataTree ) { - const values = formDataTree.getAll( this.field ).filter( - val => '' !== val.trim() - ); + const values = formDataTree.getAll( this.field ) + .map( val => val.trim() ).filter( val => '' !== val ); if ( values.length < parseInt( this.threshold ) ) { throw new ValidationError( this ); diff --git a/rules/minlength.js b/rules/minlength.js index 5b61fc9..9f51203 100644 --- a/rules/minlength.js +++ b/rules/minlength.js @@ -1,13 +1,14 @@ import { ValidationError } from '../error'; export const minlength = function ( formDataTree ) { - const values = formDataTree.getAll( this.field ); + const values = formDataTree.getAll( this.field ) + .map( val => val.trim() ).filter( val => '' !== val ); let totalLength = 0; values.forEach( text => { if ( 'string' === typeof text ) { - totalLength += text.trim().length; + totalLength += text.length; } } ); diff --git a/rules/minnumber.js b/rules/minnumber.js index 624f607..47de686 100644 --- a/rules/minnumber.js +++ b/rules/minnumber.js @@ -1,7 +1,8 @@ import { ValidationError } from '../error'; export const minnumber = function ( formDataTree ) { - const values = formDataTree.getAll( this.field ); + const values = formDataTree.getAll( this.field ) + .map( val => val.trim() ).filter( val => '' !== val ); const isAcceptableNumber = text => { if ( parseFloat( text ) < parseFloat( this.threshold ) ) { diff --git a/rules/number.js b/rules/number.js index d6da240..884a6a8 100644 --- a/rules/number.js +++ b/rules/number.js @@ -1,12 +1,11 @@ import { ValidationError } from '../error'; export const number = function ( formDataTree ) { - const values = formDataTree.getAll( this.field ); + const values = formDataTree.getAll( this.field ) + .map( val => val.trim() ).filter( val => '' !== val ); // https://html.spec.whatwg.org/multipage/input.html#number-state-(type=number) const isValidFloatingPointNumber = text => { - text = text.trim(); - if ( /^[-]?[0-9]+(?:[eE][+-]?[0-9]+)?$/.test( text ) ) { return true; } diff --git a/rules/required.js b/rules/required.js index fb53e69..6a1cf84 100644 --- a/rules/required.js +++ b/rules/required.js @@ -1,9 +1,8 @@ import { ValidationError } from '../error'; export const required = function ( formDataTree ) { - const values = formDataTree.getAll( this.field ).filter( - val => '' !== val.trim() - ); + const values = formDataTree.getAll( this.field ) + .map( val => val.trim() ).filter( val => '' !== val ); if ( 0 === values.length ) { throw new ValidationError( this ); diff --git a/rules/stepnumber.js b/rules/stepnumber.js index a9fcaa1..c7de6ef 100644 --- a/rules/stepnumber.js +++ b/rules/stepnumber.js @@ -1,7 +1,8 @@ import { ValidationError } from '../error'; export const stepnumber = function ( formDataTree ) { - const values = formDataTree.getAll( this.field ); + const values = formDataTree.getAll( this.field ) + .map( val => val.trim() ).filter( val => '' !== val ); const base = parseFloat( this.base ); const interval = parseFloat( this.interval ); @@ -11,8 +12,6 @@ export const stepnumber = function ( formDataTree ) { } const matchesStep = text => { - text = text.trim(); - const remainder = ( parseFloat( text ) - base ) % interval; if ( diff --git a/rules/tel.js b/rules/tel.js index 157cb93..aa57ea3 100644 --- a/rules/tel.js +++ b/rules/tel.js @@ -1,10 +1,10 @@ import { ValidationError } from '../error'; export const tel = function ( formDataTree ) { - const values = formDataTree.getAll( this.field ); + const values = formDataTree.getAll( this.field ) + .map( val => val.trim() ).filter( val => '' !== val ); const isTelephoneNumber = text => { - text = text.trim(); text = text.replaceAll( /[()/.*#\s-]+/g, '' ); return /^[+]?[0-9]+$/.test( text ); diff --git a/rules/time.js b/rules/time.js index f7cf0ed..3a0d679 100644 --- a/rules/time.js +++ b/rules/time.js @@ -1,12 +1,13 @@ import { ValidationError } from '../error'; export const time = function ( formDataTree ) { - const values = formDataTree.getAll( this.field ); + const values = formDataTree.getAll( this.field ) + .map( val => val.trim() ).filter( val => '' !== val ); // https://html.spec.whatwg.org/multipage/input.html#time-state-(type=time) const isValidTimeString = text => { const pattern = /^([0-9]{2})\:([0-9]{2})(?:\:([0-9]{2}))?$/; - const matches = text.trim().match( pattern ); + const matches = text.match( pattern ); if ( ! matches ) { return false; diff --git a/rules/url.js b/rules/url.js index f0079bd..655f7f7 100644 --- a/rules/url.js +++ b/rules/url.js @@ -1,15 +1,10 @@ import { ValidationError } from '../error'; export const url = function ( formDataTree ) { - const values = formDataTree.getAll( this.field ); + const values = formDataTree.getAll( this.field ) + .map( val => val.trim() ).filter( val => '' !== val ); const isAbsoluteUrl = text => { - text = text.trim(); - - if ( '' === text ) { - return false; - } - try { const urlObj = new URL( text ); const protocol = urlObj.protocol.replace( /:$/, '' );