UNPKG

fortify-schema

Version:

TypeScript interface-like schema validation system that's easier to use than Zod

240 lines (236 loc) 6.13 kB
'use strict'; var BaseSchema = require('./BaseSchema.js'); /** * String schema with comprehensive validation options */ class StringSchema extends BaseSchema.BaseSchema { /** * Set minimum length */ min(length) { const cloned = this.clone(); cloned._minLength = length; return cloned; } /** * Set maximum length */ max(length) { const cloned = this.clone(); cloned._maxLength = length; return cloned; } /** * Set exact length */ length(length) { const cloned = this.clone(); cloned._minLength = length; cloned._maxLength = length; return cloned; } /** * Set regex pattern */ regex(pattern) { const cloned = this.clone(); cloned._pattern = pattern; return cloned; } /** * Validate as email */ email() { const cloned = this.clone(); cloned._format = "email"; return cloned; } /** * Validate as URL */ url() { const cloned = this.clone(); cloned._format = "url"; return cloned; } /** * Validate as UUID */ uuid() { const cloned = this.clone(); cloned._format = "uuid"; return cloned; } /** * Validate as phone number */ phone() { const cloned = this.clone(); cloned._format = "phone"; return cloned; } /** * Validate as URL slug */ slug() { const cloned = this.clone(); cloned._format = "slug"; return cloned; } /** * Validate as username */ username() { const cloned = this.clone(); cloned._format = "username"; return cloned; } /** * Validate string value */ validate(value) { // Handle common validation (undefined/null) const commonResult = this.handleCommonValidation(value); if (commonResult) { return commonResult; } const result = { success: true, errors: [], warnings: [], data: undefined, }; // Type check if (typeof value !== "string") { result.success = false; result.errors.push("Expected string"); return result; } result.data = value; // Length validation if (this._minLength !== undefined && value.length < this._minLength) { result.success = false; result.errors.push(`String must be at least ${this._minLength} characters`); } if (this._maxLength !== undefined && value.length > this._maxLength) { result.success = false; result.errors.push(`String must be at most ${this._maxLength} characters`); } // Pattern validation if (this._pattern && !this._pattern.test(value)) { result.success = false; result.errors.push("String does not match required pattern"); } // Format validation if (this._format) { try { this.validateFormat(value, this._format); } catch (error) { result.success = false; result.errors.push(error.message); } } return result; } /** * Validate specific string formats */ validateFormat(value, format) { switch (format) { case "email": this.validateEmail(value); break; case "url": this.validateURL(value); break; case "uuid": this.validateUUID(value); break; case "phone": this.validatePhone(value); break; case "slug": this.validateSlug(value); break; case "username": this.validateUsername(value); break; default: throw new Error(`Unknown format: ${format}`); } } /** * Basic email validation (can be enhanced with external validators) */ validateEmail(value) { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; if (!emailRegex.test(value)) { throw new Error("Invalid email format"); } } /** * Basic URL validation */ validateURL(value) { try { new URL(value); } catch { throw new Error("Invalid URL format"); } } /** * Basic UUID validation */ validateUUID(value) { const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; if (!uuidRegex.test(value)) { throw new Error("Invalid UUID format"); } } /** * Basic phone validation */ validatePhone(value) { const phoneRegex = /^[\+]?[1-9][\d]{0,15}$/; const cleanPhone = value.replace(/[\s\-\(\)\.]/g, ""); if (!phoneRegex.test(cleanPhone)) { throw new Error("Invalid phone number format"); } } /** * Slug validation */ validateSlug(value) { const slugRegex = /^[a-z0-9]+(?:-[a-z0-9]+)*$/; if (!slugRegex.test(value)) { throw new Error("Invalid slug format"); } } /** * Username validation */ validateUsername(value) { const usernameRegex = /^[a-zA-Z0-9_-]+$/; if (!usernameRegex.test(value)) { throw new Error("Invalid username format"); } } /** * Get schema configuration */ getConfig() { return { minLength: this._minLength, maxLength: this._maxLength, pattern: this._pattern, format: this._format, optional: this._optional, nullable: this._nullable, default: this._default, }; } } exports.StringSchema = StringSchema; //# sourceMappingURL=StringSchema.js.map