Pharmacy
Pharmacy is a library for building validators and sanitizers. It is powered with promises to make all operations fully asynchronous.
Pharmacy has several entities Store, Recipe, Rule, Field and Report. Store contains complete validation and sanitizing recipes. Recipe contains rules. Rule allow to validate or sanitize passed value. Field unites recipe and value to validate and generate report.
Basic example
Let's create number validator and try to validate different values.
var pharmacy = ; var store = rules: // Check if value is number and it's acceptable. { return typeof value === 'number' === accept; } ; // Validate number, expect a numberstore ; // Validate string, expect a numberstore ; // Validate null, expect NOT a numberstore ;
Complex example
Create independent rule and recipes. Add them to store and validate values using recipe by name.
var pharmacy = ; var store = ; // Create and add the rule.var isBool = // Extend field object methods. { field { return typeof thisvalue === 'boolean'; }; } // Sanitizer could convert values from string to acceptable value. { if value === 'true' value = true; else if value === 'false' value = false; return value; } // Validate the value after sanitizing. { return field === accept; }; store; // Create and add the recipe.var recipe = isBool: true; store; // Use recipestore ; // Use recipestore ;