Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. Download free →
CLSkills
General / UtilitybeginnerNew

Validation Utils

Share

Create validation utility functions

Works with OpenClaude

You are a utility developer creating reusable validation functions. The user wants to build a set of validation utilities that can be used across projects for common data validation scenarios.

What to check first

  • Determine target language/framework (JavaScript/TypeScript, Python, etc.)
  • Review existing validation libraries (Zod, Yup, Pydantic) to avoid reinventing
  • Identify which validation patterns are most frequently needed in your codebase

Steps

  1. Create a new validation.js or validation.ts file in your utils directory
  2. Define validators for primitive types: isString(), isNumber(), isBoolean(), isArray()
  3. Build composite validators: isEmail(), isUrl(), isPhoneNumber() using regex patterns
  4. Implement conditional validators: isRequired(), hasMinLength(), hasMaxLength()
  5. Add object/schema validators: validateObject() that checks multiple fields against rules
  6. Create error collection: return structured validation errors with field names and messages
  7. Export all validators as named exports for flexible imports
  8. Add type definitions if using TypeScript with clear return types (boolean or ValidationError[])

Code

export interface ValidationError {
  field: string;
  message: string;
}

export interface ValidationRule {
  [key: string]: Array<(value: any) => boolean | string>;
}

// Primitive validators
export const isString = (value: any): boolean => typeof value === 'string';
export const isNumber = (value: any): boolean => typeof value === 'number' && !isNaN(value);
export const isBoolean = (value: any): boolean => typeof value === 'boolean';
export const isArray = (value: any): boolean => Array.isArray(value);

// String format validators
export const isEmail = (value: string): boolean => {
  const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
  return emailRegex.test(value);
};

export const isUrl = (value: string): boolean => {
  try {
    new URL(value);
    return true;
  } catch {
    return false;
  }
};

export const isPhoneNumber = (value: string): boolean => {
  const phoneRegex = /^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/;
  return phoneRegex.test(value);
};

// Constraint validators
export const isRequired = (value: any): boolean => {
  if (typeof value === 'string') return value.trim().length > 0;
  return value !== null && value !== undefined;
};

export const hasMinLength = (min: number) => (value: string): boolean => {
  return isString(value) && value.length

Note: this example was truncated in the source. See the GitHub repo for the latest full version.

Common Pitfalls

  • Treating this skill as a one-shot solution — most workflows need iteration and verification
  • Skipping the verification steps — you don't know it worked until you measure
  • Applying this skill without understanding the underlying problem — read the related docs first

When NOT to Use This Skill

  • When a simpler manual approach would take less than 10 minutes
  • On critical production systems without testing in staging first
  • When you don't have permission or authorization to make these changes

How to Verify It Worked

  • Run the verification steps documented above
  • Compare the output against your expected baseline
  • Check logs for any warnings or errors — silent failures are the worst kind

Production Considerations

  • Test in staging before deploying to production
  • Have a rollback plan — every change should be reversible
  • Monitor the affected systems for at least 24 hours after the change

Quick Info

Difficultybeginner
Version1.0.0
AuthorClaude Skills Hub
generalvalidationutilities

Install command:

curl -o ~/.claude/skills/validation-utils.md https://claude-skills-hub.vercel.app/skills/general/validation-utils.md

Related General / Utility Skills

Other Claude Code skills in the same category — free to download.

Want a General / Utility skill personalized to YOUR project?

This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.