compose-form-up
Advanced tools
Comparing version 1.7.2 to 1.8.0
@@ -106,3 +106,3 @@ var toolbox = require( 'compose-toolbox' ), | ||
}) | ||
}, 20) | ||
@@ -124,3 +124,3 @@ } | ||
if ( typeof callback === 'function' ) callback() | ||
}) | ||
}, 20) | ||
} | ||
@@ -127,0 +127,0 @@ } |
// Dependencies | ||
var toolbox = require( 'compose-toolbox' ), | ||
Event = toolbox.event, | ||
tTime = toolbox.time, | ||
getClosest = toolbox.getClosest, | ||
@@ -124,3 +125,3 @@ wordCount = toolbox.wordCount, | ||
// Set a custom validation message for word count | ||
var message = checkValue( input ) || checkLength( input ) || '' | ||
var message = checkValue( input ) || checkLength( input ) || checkTime( input ) || '' | ||
input.setCustomValidity( message ) | ||
@@ -175,7 +176,5 @@ | ||
if ( goal ) { | ||
var phrasing = ( limit == 'min' ) ? 'at least ' : 'no more than ', | ||
valid = ( limit == 'min' ) ? ( goal <= wordCount(input.value) ) : ( wordCount(input.value) <= goal ) | ||
var lessThanGoal = wordCount( input.value ) < goal | ||
phrasing = ( limit == 'min' ) ? 'at least ' : 'no more than ', | ||
valid = ( limit == 'min' ) ? !lessThanGoal : lessThanGoal | ||
// Return a custom error message | ||
@@ -189,2 +188,62 @@ if ( input.value && !valid ) | ||
// Test custom validation for maximum and minimum time | ||
function checkTime ( input ) { | ||
var before = input.dataset.beforeTime, | ||
after = input.dataset.afterTime | ||
if ( before || after || input.dataset.validateTime ) { | ||
var zone = input.dataset.zone || 'utc', | ||
options = { zone: zone }, | ||
timeValue = tTime.parse( input.value, options ), | ||
err = '', | ||
isAfter, afterTime, isBefore, beforeTime | ||
// Enure that the time value is actually | ||
if ( !timeValue ) return "Please enter a valid date string: YYYY-MM-DD HH:MM:SS" | ||
if ( before ) { | ||
beforeTime = tTime.parse( before ) | ||
if ( !beforeTime ) { | ||
err = `data-before-time='${before}' is not a valid date string` | ||
console.error( err ) | ||
return err | ||
} | ||
} | ||
if ( after ) { | ||
afterTime = tTime.parse( after ) | ||
if ( !afterTime ) { | ||
err = `data-after-time='${after}' is not a valid date string` | ||
console.error( err ) | ||
return err | ||
} | ||
} | ||
if ( beforeTime && afterTime ) { | ||
if ( !tTime.isBetween( timeValue, afterTime, beforeTime ) ) { | ||
return "Please enter a time between "+formatDate( afterTime, options )+" and "+formatDate( beforeTime, options ) | ||
} | ||
} | ||
else if ( afterTime ) { | ||
if ( !tTime.isAfter( timeValue, afterTime ) ) { | ||
return "Please enter a time after "+formatDate( afterTime, options ) | ||
} | ||
} | ||
else if ( beforeTime ) { | ||
if ( !tTime.isBefore( timeValue, beforeTime ) ) { | ||
return "Please enter a time before "+formatDate( beforeTime, options ) | ||
} | ||
} | ||
} | ||
} | ||
function formatDate( date, options ) { | ||
return tTime.prettyPrint( date, options ).replace( ' Z', '') | ||
} | ||
// If input is nested in a label, treat the label as the | ||
@@ -191,0 +250,0 @@ // target for assigning status (class names and messages). |
{ | ||
"name": "compose-form-up", | ||
"version": "1.7.2", | ||
"version": "1.8.0", | ||
"description": "A lightweight HTML5 form validation utility", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mochify --reporter spec" | ||
"test": "jest" | ||
}, | ||
@@ -26,8 +26,11 @@ "repository": { | ||
"dependencies": { | ||
"compose-toolbox": "^1.0.0" | ||
"compose-toolbox": "^1.6.0" | ||
}, | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"mochify": "^2.18.1" | ||
"browserify": "^16.2.0", | ||
"http-server": "^0.11.1", | ||
"jest": "^22.4", | ||
"jest-puppeteer": "^2.3.0", | ||
"puppeteer": "^1.0.0" | ||
} | ||
} |
@@ -62,2 +62,5 @@ # FormUp | ||
- `data-min-words="3"` - Ensure at least 3 words are entered. | ||
- `data-before="YYYY-MM-DD HH:MM:SS Z"` - Ensure that a date string occurs before a certian date. | ||
- `data-after="YYYY-MM-DD HH:MM:SS Z"` - Ensure that a date string occurs after a certian date. | ||
- `data-zone="utc-5"` - Select a timezone for parsing date strings, defaults to UTC. | ||
- `data-invalid-value="superman"` - Superman cannot be entered. | ||
@@ -64,0 +67,0 @@ - `data-invalid-value-message="Invalid value: superman"` - Sets a custom error message when a value matching the `data-invalid-value` is set. |
@@ -1,74 +0,53 @@ | ||
var assert = require('chai').assert | ||
var toolbox = require( 'compose-toolbox' ) | ||
var Event = toolbox.event | ||
module.exports = u = { | ||
validate: async () => { | ||
await page.evaluate("FormUp.validate(document.querySelector('form'))") | ||
}, | ||
// Utlitiy function for easily appending to HTML | ||
var Utils = { | ||
type: async (selector, data) => { | ||
await expect(page).toFill(selector, data) | ||
await page.evaluate("FormUp.validate(document.querySelector('form'))") | ||
await u.validate() | ||
}, | ||
injectHTML: function( el, html ) { | ||
select: async (selector, option) => { | ||
await expect(page).toSelect(selector, option) | ||
await u.validate() | ||
}, | ||
el.insertAdjacentHTML( 'beforeend', html ) | ||
invalidateField: async (selector, text) => { | ||
await page.evaluate(`FormUp.invalidateField( document.querySelector('${selector}'), '${text}')`) | ||
await u.validate() | ||
}, | ||
return el.lastChild | ||
isValid: async (selector) => { | ||
await expect(page).toMatchElement(`${selector}.valid`) | ||
}, | ||
container: function() { | ||
var div = document.querySelector('.container') | ||
if ( !div ){ | ||
div = Utils.injectHTML( document.body, '<div class="container"></div>' ) | ||
} else { | ||
div.innerHTML = '' | ||
} | ||
return div | ||
isInvalid: async (selector) => { | ||
await u.findElement(`${selector}.invalid`) | ||
}, | ||
addInput: function( form, options, tag ) { | ||
options = options || {} | ||
tag = tag || '<input type="text">' | ||
defaults = { | ||
required: true, | ||
} | ||
var label = Utils.injectHTML( form, '<label></label>' ) | ||
var input = Utils.injectHTML( label, tag ) | ||
for ( var attr in defaults ) { input.setAttribute( attr, defaults[attr] ) } | ||
for ( var attr in options ) { input.setAttribute( attr, options[attr] ) } | ||
return input | ||
findElement: async (selector, options) => { | ||
await expect(page).toMatchElement(`${selector}`, options) | ||
}, | ||
submit: function( form ) { | ||
Event.fire( form.querySelector('[type=submit]'), 'click' ) | ||
find: async (text) => { | ||
await expect(page).toMatch(text) | ||
}, | ||
setValue: function( input, value ) { | ||
input.setAttribute( 'value', value ) | ||
input.value = value | ||
Event.fire( input, 'blur' ) | ||
click: async (selector, options) => { | ||
await expect(page).toClick(selector, options) | ||
}, | ||
selectOption: function( select, index ) { | ||
select.selectedIndex = index | ||
// Shabby test code justification: It's really hard to trigger events in tests, so this | ||
// short circuits the system. It's not ideal but for now | ||
// it'll do. | ||
select.parentNode.classList.toggle( 'invalid', !select.checkValidity() ) | ||
select.parentNode.classList.toggle( 'valid', select.checkValidity() ) | ||
html: async (selector) => { | ||
return await page.$eval(selector, e => e.outerHTML); | ||
}, | ||
isValid: function( input ) { | ||
assert.isTrue( input.checkValidity() == true && input.parentNode.classList.contains( 'valid' ) ) | ||
matchText: async (selector, text) => { | ||
return expect( await u.text(selector)).toBe(text) | ||
}, | ||
isInvalid: function( input ) { | ||
assert.isTrue( input.checkValidity() == false && input.parentNode.classList.contains( 'invalid' ) ) | ||
text: async (selector) => { | ||
return await page.$eval(selector, e => e.textContent); | ||
} | ||
} | ||
module.exports = Utils |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
176954
15
642
208
5
Updatedcompose-toolbox@^1.6.0