Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

form-validity

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

form-validity - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

150

index.js

@@ -5,3 +5,3 @@ 'use strict';

const attributesByType = {
var attributesByType = {
// 'button': [],

@@ -34,7 +34,7 @@ checkbox: ['required'],

};
const symbol = Symbol('constraints');
var symbol = Symbol('constraints');
function createField(type, message) {
const supportedAttributes = attributesByType[type];
const constraint = {
var supportedAttributes = attributesByType[type];
var constraint = {
type: {

@@ -45,3 +45,3 @@ value: type,

};
const field = {
var field = {
required(message) {

@@ -88,5 +88,7 @@ constraint.required = {

if (value.global || value.ignoreCase || value.multiline) {
console.warn(`global, ignoreCase, and multiline flags are not supported on the pattern attribute`);
console.warn("global, ignoreCase, and multiline flags are not supported on the pattern attribute");
} else {
constraint.pattern = (constraint.pattern ?? []).concat({
var _constraint$pattern;
constraint.pattern = ((_constraint$pattern = constraint.pattern) !== null && _constraint$pattern !== void 0 ? _constraint$pattern : []).concat({
value,

@@ -120,3 +122,3 @@ message

const f = {
var f = {
// button: () => createField('button'),

@@ -173,3 +175,5 @@ checkbox: () => createField('checkbox'),

} else if (isElement(element, 'select')) {
return element.value !== Array.from(element.options).find(option => option.defaultSelected)?.value;
var _Array$from$find;
return element.value !== ((_Array$from$find = Array.from(element.options).find(option => option.defaultSelected)) === null || _Array$from$find === void 0 ? void 0 : _Array$from$find.value);
} else {

@@ -190,4 +194,5 @@ return false;

function flatten(item, isLeaf, prefix = '') {
let entries = [];
function flatten(item, isLeaf) {
var prefix = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
var entries = [];

@@ -198,7 +203,7 @@ if (isLeaf(item)) {

for (var i = 0; i < item.length; i++) {
entries.push(...flatten(item[i], isLeaf, `${prefix}[${i}]`));
entries.push(...flatten(item[i], isLeaf, "".concat(prefix, "[").concat(i, "]")));
}
} else {
for (const [key, value] of Object.entries(item)) {
entries.push(...flatten(value, isLeaf, prefix ? `${prefix}.${key}` : key));
for (var [key, _value] of Object.entries(item)) {
entries.push(...flatten(_value, isLeaf, prefix ? "".concat(prefix, ".").concat(key) : key));
}

@@ -211,8 +216,8 @@ }

function unflatten(entries) {
const pattern = /(\w+)\[(\d+)\]/;
const result = {};
var pattern = /(\w+)\[(\d+)\]/;
var result = {};
for (let [key, value] of entries) {
let paths = key.split('.').flatMap(key => {
const matches = pattern.exec(key);
for (var [key, _value2] of entries) {
var paths = key.split('.').flatMap(key => {
var matches = pattern.exec(key);

@@ -225,18 +230,20 @@ if (!matches) {

});
let length = paths.length;
let lastIndex = length - 1;
let index = -1;
let pointer = result;
var length = paths.length;
var lastIndex = length - 1;
var index = -1;
var pointer = result;
while (pointer != null && ++index < length) {
let key = paths[index];
let next = paths[index + 1];
let newValue = value;
var _key = paths[index];
var next = paths[index + 1];
var newValue = _value2;
if (index != lastIndex) {
newValue = pointer[key] ?? (typeof next === 'number' ? [] : {});
var _pointer$_key;
newValue = (_pointer$_key = pointer[_key]) !== null && _pointer$_key !== void 0 ? _pointer$_key : typeof next === 'number' ? [] : {};
}
pointer[key] = newValue;
pointer = pointer[key];
pointer[_key] = newValue;
pointer = pointer[_key];
}

@@ -249,2 +256,4 @@ }

function validate(value, constraint) {
var _constraint$pattern2;
if (value instanceof File) {

@@ -256,3 +265,5 @@ return 'File is not supported yet';

if (typeof value === 'undefined' || value === '') {
return constraint.required.message ?? 'This field is required';
var _constraint$required$;
return (_constraint$required$ = constraint.required.message) !== null && _constraint$required$ !== void 0 ? _constraint$required$ : 'This field is required';
}

@@ -263,3 +274,5 @@ }

if (typeof value === 'undefined' || value.length < constraint.minLength.value) {
return constraint.minLength.message ?? `This field must be at least ${constraint.minLength.value} characters`;
var _constraint$minLength;
return (_constraint$minLength = constraint.minLength.message) !== null && _constraint$minLength !== void 0 ? _constraint$minLength : "This field must be at least ".concat(constraint.minLength.value, " characters");
}

@@ -270,3 +283,5 @@ }

if (typeof value !== 'undefined' && value.length > constraint.maxLength.value) {
return constraint.maxLength.message ?? `This field must be at most ${constraint.maxLength.value} characters`;
var _constraint$maxLength;
return (_constraint$maxLength = constraint.maxLength.message) !== null && _constraint$maxLength !== void 0 ? _constraint$maxLength : "This field must be at most ".concat(constraint.maxLength.value, " characters");
}

@@ -276,6 +291,10 @@ }

if (constraint.min) {
if (constraint.min.value instanceof Date && new Date(value ?? '') < constraint.min.value) {
return constraint.min.message ?? `This field must be later than ${constraint.min.value.toISOString()}`;
} else if (typeof constraint.min.value === 'number' && Number(value ?? '') < constraint.min.value) {
return constraint.min.message ?? `This field must be greater than or equal to ${constraint.min.value}`;
if (constraint.min.value instanceof Date && new Date(value !== null && value !== void 0 ? value : '') < constraint.min.value) {
var _constraint$min$messa;
return (_constraint$min$messa = constraint.min.message) !== null && _constraint$min$messa !== void 0 ? _constraint$min$messa : "This field must be later than ".concat(constraint.min.value.toISOString());
} else if (typeof constraint.min.value === 'number' && Number(value !== null && value !== void 0 ? value : '') < constraint.min.value) {
var _constraint$min$messa2;
return (_constraint$min$messa2 = constraint.min.message) !== null && _constraint$min$messa2 !== void 0 ? _constraint$min$messa2 : "This field must be greater than or equal to ".concat(constraint.min.value);
}

@@ -286,5 +305,9 @@ }

if (typeof value !== 'undefined' && constraint.max.value instanceof Date && new Date(value) > constraint.max.value) {
return constraint.max.message ?? `This field must be at earlier than ${constraint.max.value.toISOString()}`;
var _constraint$max$messa;
return (_constraint$max$messa = constraint.max.message) !== null && _constraint$max$messa !== void 0 ? _constraint$max$messa : "This field must be at earlier than ".concat(constraint.max.value.toISOString());
} else if (typeof value !== 'undefined' && typeof constraint.max.value === 'number' && Number(value) > constraint.max.value) {
return constraint.max.message ?? `This field must be less than or equal to ${constraint.max.value}`;
var _constraint$max$messa2;
return (_constraint$max$messa2 = constraint.max.message) !== null && _constraint$max$messa2 !== void 0 ? _constraint$max$messa2 : "This field must be less than or equal to ".concat(constraint.max.value);
}

@@ -298,4 +321,6 @@ }

case 'email':
if (!/^\S+\@\S+$/.test(value ?? '')) {
return constraint.type.message ?? `This field must be a valid email`;
if (!/^\S+\@\S+$/.test(value !== null && value !== void 0 ? value : '')) {
var _constraint$type$mess;
return (_constraint$type$mess = constraint.type.message) !== null && _constraint$type$mess !== void 0 ? _constraint$type$mess : "This field must be a valid email";
}

@@ -306,7 +331,7 @@

case 'url':
const isURL = value => {
var isURL = value => {
try {
new URL(value);
return true;
} catch {
} catch (_unused) {
return false;

@@ -316,4 +341,6 @@ }

if (!isURL(value ?? '')) {
return constraint.type.message ?? `This field must be a valid URL`;
if (!isURL(value !== null && value !== void 0 ? value : '')) {
var _constraint$type$mess2;
return (_constraint$type$mess2 = constraint.type.message) !== null && _constraint$type$mess2 !== void 0 ? _constraint$type$mess2 : "This field must be a valid URL";
}

@@ -325,10 +352,12 @@

if (constraint.pattern?.length) {
const pattern = constraint.pattern.find(pattern => {
const match = value?.match(pattern.value);
if ((_constraint$pattern2 = constraint.pattern) !== null && _constraint$pattern2 !== void 0 && _constraint$pattern2.length) {
var _pattern = constraint.pattern.find(pattern => {
var match = value === null || value === void 0 ? void 0 : value.match(pattern.value);
return !match || value !== match[0];
});
if (pattern) {
return pattern.message ?? `This field must be a valid format`;
if (_pattern) {
var _pattern$message;
return (_pattern$message = _pattern.message) !== null && _pattern$message !== void 0 ? _pattern$message : "This field must be a valid format";
}

@@ -341,15 +370,16 @@ }

function parse(payload, fieldsetCreator) {
const valueEntries = payload instanceof URLSearchParams || payload instanceof FormData ? payload : new URLSearchParams(payload);
const value = unflatten(valueEntries);
const fieldset = typeof fieldsetCreator === 'function' ? fieldsetCreator(value) : fieldsetCreator;
const values = Object.fromEntries(valueEntries);
const errorEntries = [];
var valueEntries = payload instanceof URLSearchParams || payload instanceof FormData ? payload : new URLSearchParams(payload);
var value = unflatten(valueEntries);
var fieldset = typeof fieldsetCreator === 'function' ? fieldsetCreator(value) : fieldsetCreator;
var values = Object.fromEntries(valueEntries);
var errorEntries = [];
for (const [name, field] of flatten(fieldset, f => typeof f[symbol] === 'function')) {
const constraint = getConstraint(field);
const value = values[name];
const message = validate(value, constraint);
for (var [name, field] of flatten(fieldset, f => typeof f[symbol] === 'function')) {
var constraint = getConstraint(field);
var _value3 = values[name];
if (message) {
errorEntries.push([name, message]);
var _message = validate(_value3, constraint);
if (_message) {
errorEntries.push([name, _message]);
}

@@ -356,0 +386,0 @@ }

@@ -1,2 +0,2 @@

const attributesByType = {
var attributesByType = {
// 'button': [],

@@ -29,7 +29,7 @@ checkbox: ['required'],

};
const symbol = Symbol('constraints');
var symbol = Symbol('constraints');
function createField(type, message) {
const supportedAttributes = attributesByType[type];
const constraint = {
var supportedAttributes = attributesByType[type];
var constraint = {
type: {

@@ -40,3 +40,3 @@ value: type,

};
const field = {
var field = {
required(message) {

@@ -83,5 +83,7 @@ constraint.required = {

if (value.global || value.ignoreCase || value.multiline) {
console.warn(`global, ignoreCase, and multiline flags are not supported on the pattern attribute`);
console.warn("global, ignoreCase, and multiline flags are not supported on the pattern attribute");
} else {
constraint.pattern = (constraint.pattern ?? []).concat({
var _constraint$pattern;
constraint.pattern = ((_constraint$pattern = constraint.pattern) !== null && _constraint$pattern !== void 0 ? _constraint$pattern : []).concat({
value,

@@ -115,3 +117,3 @@ message

const f = {
var f = {
// button: () => createField('button'),

@@ -168,3 +170,5 @@ checkbox: () => createField('checkbox'),

} else if (isElement(element, 'select')) {
return element.value !== Array.from(element.options).find(option => option.defaultSelected)?.value;
var _Array$from$find;
return element.value !== ((_Array$from$find = Array.from(element.options).find(option => option.defaultSelected)) === null || _Array$from$find === void 0 ? void 0 : _Array$from$find.value);
} else {

@@ -185,4 +189,5 @@ return false;

function flatten(item, isLeaf, prefix = '') {
let entries = [];
function flatten(item, isLeaf) {
var prefix = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
var entries = [];

@@ -193,7 +198,7 @@ if (isLeaf(item)) {

for (var i = 0; i < item.length; i++) {
entries.push(...flatten(item[i], isLeaf, `${prefix}[${i}]`));
entries.push(...flatten(item[i], isLeaf, "".concat(prefix, "[").concat(i, "]")));
}
} else {
for (const [key, value] of Object.entries(item)) {
entries.push(...flatten(value, isLeaf, prefix ? `${prefix}.${key}` : key));
for (var [key, _value] of Object.entries(item)) {
entries.push(...flatten(_value, isLeaf, prefix ? "".concat(prefix, ".").concat(key) : key));
}

@@ -206,8 +211,8 @@ }

function unflatten(entries) {
const pattern = /(\w+)\[(\d+)\]/;
const result = {};
var pattern = /(\w+)\[(\d+)\]/;
var result = {};
for (let [key, value] of entries) {
let paths = key.split('.').flatMap(key => {
const matches = pattern.exec(key);
for (var [key, _value2] of entries) {
var paths = key.split('.').flatMap(key => {
var matches = pattern.exec(key);

@@ -220,18 +225,20 @@ if (!matches) {

});
let length = paths.length;
let lastIndex = length - 1;
let index = -1;
let pointer = result;
var length = paths.length;
var lastIndex = length - 1;
var index = -1;
var pointer = result;
while (pointer != null && ++index < length) {
let key = paths[index];
let next = paths[index + 1];
let newValue = value;
var _key = paths[index];
var next = paths[index + 1];
var newValue = _value2;
if (index != lastIndex) {
newValue = pointer[key] ?? (typeof next === 'number' ? [] : {});
var _pointer$_key;
newValue = (_pointer$_key = pointer[_key]) !== null && _pointer$_key !== void 0 ? _pointer$_key : typeof next === 'number' ? [] : {};
}
pointer[key] = newValue;
pointer = pointer[key];
pointer[_key] = newValue;
pointer = pointer[_key];
}

@@ -244,2 +251,4 @@ }

function validate(value, constraint) {
var _constraint$pattern2;
if (value instanceof File) {

@@ -251,3 +260,5 @@ return 'File is not supported yet';

if (typeof value === 'undefined' || value === '') {
return constraint.required.message ?? 'This field is required';
var _constraint$required$;
return (_constraint$required$ = constraint.required.message) !== null && _constraint$required$ !== void 0 ? _constraint$required$ : 'This field is required';
}

@@ -258,3 +269,5 @@ }

if (typeof value === 'undefined' || value.length < constraint.minLength.value) {
return constraint.minLength.message ?? `This field must be at least ${constraint.minLength.value} characters`;
var _constraint$minLength;
return (_constraint$minLength = constraint.minLength.message) !== null && _constraint$minLength !== void 0 ? _constraint$minLength : "This field must be at least ".concat(constraint.minLength.value, " characters");
}

@@ -265,3 +278,5 @@ }

if (typeof value !== 'undefined' && value.length > constraint.maxLength.value) {
return constraint.maxLength.message ?? `This field must be at most ${constraint.maxLength.value} characters`;
var _constraint$maxLength;
return (_constraint$maxLength = constraint.maxLength.message) !== null && _constraint$maxLength !== void 0 ? _constraint$maxLength : "This field must be at most ".concat(constraint.maxLength.value, " characters");
}

@@ -271,6 +286,10 @@ }

if (constraint.min) {
if (constraint.min.value instanceof Date && new Date(value ?? '') < constraint.min.value) {
return constraint.min.message ?? `This field must be later than ${constraint.min.value.toISOString()}`;
} else if (typeof constraint.min.value === 'number' && Number(value ?? '') < constraint.min.value) {
return constraint.min.message ?? `This field must be greater than or equal to ${constraint.min.value}`;
if (constraint.min.value instanceof Date && new Date(value !== null && value !== void 0 ? value : '') < constraint.min.value) {
var _constraint$min$messa;
return (_constraint$min$messa = constraint.min.message) !== null && _constraint$min$messa !== void 0 ? _constraint$min$messa : "This field must be later than ".concat(constraint.min.value.toISOString());
} else if (typeof constraint.min.value === 'number' && Number(value !== null && value !== void 0 ? value : '') < constraint.min.value) {
var _constraint$min$messa2;
return (_constraint$min$messa2 = constraint.min.message) !== null && _constraint$min$messa2 !== void 0 ? _constraint$min$messa2 : "This field must be greater than or equal to ".concat(constraint.min.value);
}

@@ -281,5 +300,9 @@ }

if (typeof value !== 'undefined' && constraint.max.value instanceof Date && new Date(value) > constraint.max.value) {
return constraint.max.message ?? `This field must be at earlier than ${constraint.max.value.toISOString()}`;
var _constraint$max$messa;
return (_constraint$max$messa = constraint.max.message) !== null && _constraint$max$messa !== void 0 ? _constraint$max$messa : "This field must be at earlier than ".concat(constraint.max.value.toISOString());
} else if (typeof value !== 'undefined' && typeof constraint.max.value === 'number' && Number(value) > constraint.max.value) {
return constraint.max.message ?? `This field must be less than or equal to ${constraint.max.value}`;
var _constraint$max$messa2;
return (_constraint$max$messa2 = constraint.max.message) !== null && _constraint$max$messa2 !== void 0 ? _constraint$max$messa2 : "This field must be less than or equal to ".concat(constraint.max.value);
}

@@ -293,4 +316,6 @@ }

case 'email':
if (!/^\S+\@\S+$/.test(value ?? '')) {
return constraint.type.message ?? `This field must be a valid email`;
if (!/^\S+\@\S+$/.test(value !== null && value !== void 0 ? value : '')) {
var _constraint$type$mess;
return (_constraint$type$mess = constraint.type.message) !== null && _constraint$type$mess !== void 0 ? _constraint$type$mess : "This field must be a valid email";
}

@@ -301,7 +326,7 @@

case 'url':
const isURL = value => {
var isURL = value => {
try {
new URL(value);
return true;
} catch {
} catch (_unused) {
return false;

@@ -311,4 +336,6 @@ }

if (!isURL(value ?? '')) {
return constraint.type.message ?? `This field must be a valid URL`;
if (!isURL(value !== null && value !== void 0 ? value : '')) {
var _constraint$type$mess2;
return (_constraint$type$mess2 = constraint.type.message) !== null && _constraint$type$mess2 !== void 0 ? _constraint$type$mess2 : "This field must be a valid URL";
}

@@ -320,10 +347,12 @@

if (constraint.pattern?.length) {
const pattern = constraint.pattern.find(pattern => {
const match = value?.match(pattern.value);
if ((_constraint$pattern2 = constraint.pattern) !== null && _constraint$pattern2 !== void 0 && _constraint$pattern2.length) {
var _pattern = constraint.pattern.find(pattern => {
var match = value === null || value === void 0 ? void 0 : value.match(pattern.value);
return !match || value !== match[0];
});
if (pattern) {
return pattern.message ?? `This field must be a valid format`;
if (_pattern) {
var _pattern$message;
return (_pattern$message = _pattern.message) !== null && _pattern$message !== void 0 ? _pattern$message : "This field must be a valid format";
}

@@ -336,15 +365,16 @@ }

function parse(payload, fieldsetCreator) {
const valueEntries = payload instanceof URLSearchParams || payload instanceof FormData ? payload : new URLSearchParams(payload);
const value = unflatten(valueEntries);
const fieldset = typeof fieldsetCreator === 'function' ? fieldsetCreator(value) : fieldsetCreator;
const values = Object.fromEntries(valueEntries);
const errorEntries = [];
var valueEntries = payload instanceof URLSearchParams || payload instanceof FormData ? payload : new URLSearchParams(payload);
var value = unflatten(valueEntries);
var fieldset = typeof fieldsetCreator === 'function' ? fieldsetCreator(value) : fieldsetCreator;
var values = Object.fromEntries(valueEntries);
var errorEntries = [];
for (const [name, field] of flatten(fieldset, f => typeof f[symbol] === 'function')) {
const constraint = getConstraint(field);
const value = values[name];
const message = validate(value, constraint);
for (var [name, field] of flatten(fieldset, f => typeof f[symbol] === 'function')) {
var constraint = getConstraint(field);
var _value3 = values[name];
if (message) {
errorEntries.push([name, message]);
var _message = validate(_value3, constraint);
if (_message) {
errorEntries.push([name, _message]);
}

@@ -351,0 +381,0 @@ }

2

package.json

@@ -5,3 +5,3 @@ {

"license": "MIT",
"version": "0.0.1",
"version": "0.0.2",
"main": "index.js",

@@ -8,0 +8,0 @@ "module": "module/index.js",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc