appwrite-utils
Version:
`appwrite-utils` is a comprehensive TypeScript library designed to streamline the development process for Appwrite projects. It provides a suite of utilities and helper functions that facilitate data manipulation, schema management, and seamless integrati
77 lines (76 loc) • 3.53 kB
JavaScript
export const validationRules = {
isNumber: (value) => typeof value === "number",
isString: (value) => typeof value === "string",
isBoolean: (value) => typeof value === "boolean",
isArray: (value) => Array.isArray(value),
isObject: (value) => value !== null &&
typeof value === "object" &&
!Array.isArray(value) &&
typeof value !== "function",
isNull: (value) => value === null,
isValidEmail: (value) => /^[\w\-\.]+@([\w-]+\.)+[\w-]{2,}$/.test(value),
isValidPhone: (value) => /^[+]?[(]?[0-9]{3}[)]?[-\s.]?[0-9]{3}[-\s.]?[0-9]{4,6}$/im.test(value),
isValidPassword: (value) => /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{8,}$/.test(value),
isValidUrl: (value) => /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/.test(value),
isValidHex: (value) => /^#([a-f0-9]{6}|[a-f0-9]{3})$/i.test(value),
isValidHexColor: (value) => /^#([a-f0-9]{6}|[a-f0-9]{3})$/i.test(value),
isValidHexAlpha: (value) => /^#([a-f0-9]{8}|[a-f0-9]{4})$/i.test(value),
isValidDate: (value) => /^\d{4}-\d{2}-\d{2}$/.test(value),
isValidTime: (value) => /^\d{2}:\d{2}(:\d{2})?$/.test(value),
isNullish: (value) => value == null,
isUndefined: (value) => value === undefined,
isDefined: (value) => value !== undefined && value !== null && value !== "",
isDate: (value) => value instanceof Date,
isEmpty: (value) => value == null ||
(typeof value === "object" && Object.keys(value).length === 0) ||
(Array.isArray(value) && value.length === 0),
isInteger: (value) => Number.isInteger(value),
isFloat: (value) => typeof value === "number" && !Number.isInteger(value),
isArrayLike: (value) => value != null &&
typeof value !== "function" &&
typeof value.length === "number" &&
value.length >= 0,
isArrayLikeObject: (value) => typeof value === "object" &&
value !== null &&
typeof value.length === "number" &&
value.length >= 0,
isFunction: (value) => typeof value === "function",
isLength: (value) => typeof value === "number" && value >= 0 && Number.isInteger(value),
isMap: (value) => value instanceof Map,
isSet: (value) => value instanceof Set,
isRegExp: (value) => value instanceof RegExp,
isSymbol: (value) => typeof value === "symbol",
isObjectLike: (value) => typeof value === "object" && value !== null,
isPlainObject: (value) => Object.prototype.toString.call(value) === "[object Object]",
isSafeInteger: (value) => Number.isSafeInteger(value),
isTypedArray: (value) => ArrayBuffer.isView(value) && !(value instanceof DataView),
isEqual: (value, other) => JSON.stringify(value) === JSON.stringify(other),
isMatch: (object, source) => {
for (let key in source) {
if (source[key] !== object[key]) {
return false;
}
}
return true;
},
has: (object, path) => {
const keys = path.split(".");
for (let key of keys) {
if (object == null || !object.hasOwnProperty(key)) {
return false;
}
object = object[key];
}
return true;
},
get: (object, path, defaultValue) => {
const keys = path.split(".");
for (let key of keys) {
if (object == null || !object.hasOwnProperty(key)) {
return defaultValue;
}
object = object[key];
}
return object;
},
};