{"id":22202178,"url":"https://github.com/rumkin/pharmacy","last_synced_at":"2025-03-25T00:59:22.381Z","repository":{"id":57322831,"uuid":"45531353","full_name":"rumkin/pharmacy","owner":"rumkin","description":"Browser and Nodejs library for creating validators and sanitisers","archived":false,"fork":false,"pushed_at":"2017-07-02T09:53:20.000Z","size":33,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-03T20:51:40.988Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/pharmacy","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rumkin.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-11-04T10:22:00.000Z","updated_at":"2016-11-20T19:25:46.000Z","dependencies_parsed_at":"2022-08-26T01:11:23.275Z","dependency_job_id":null,"html_url":"https://github.com/rumkin/pharmacy","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rumkin%2Fpharmacy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rumkin%2Fpharmacy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rumkin%2Fpharmacy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rumkin%2Fpharmacy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rumkin","download_url":"https://codeload.github.com/rumkin/pharmacy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245377989,"owners_count":20605377,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-02T16:12:39.124Z","updated_at":"2025-03-25T00:59:22.363Z","avatar_url":"https://github.com/rumkin.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Pharmacy\n\n![Travis CI](https://img.shields.io/travis/rumkin/pharmacy.png)\n[![Coverage Status](https://coveralls.io/repos/rumkin/pharmacy/badge.svg?branch=master)](https://coveralls.io/r/rumkin/pharmacy?branch=master)\n\nPharmacy is a library for building validators and sanitizers. It is powered with\npromises to make all operations fully asynchronous.\n\nPharmacy has several entities Store, Recipe, Rule, Field and Report. Store contains\ncomplete validation and sanitizing recipes. Recipe contains rules. Rule\nallow to validate or sanitize passed value. Field unites recipe and value\nto validate and generate report.\n\n## Basic example\n\nLet's create number validator and try to validate different values.\n\n```javascript\nvar pharmacy = require('pharmacy');\n\nvar store = new pharmacy.Store({\n  rules: {\n    // Check if value is number and it's acceptable.\n    isNumber(accept, value) {\n      return (typeof value === 'number') === accept;\n    }\n  }\n});\n\n// Validate number, expect a number\nstore\n  .validate(10, {isNumber: true})\n  .then(report =\u003e {\n    report.isValid(); // -\u003e true\n    report.value; // -\u003e 10\n  });\n\n// Validate string, expect a number\nstore\n  .validate(\"10\", {isNumber: true})\n  .then(report =\u003e {\n    report.isValid(); // -\u003e false\n    report.value; // -\u003e \"10\"\n  });\n\n// Validate null, expect NOT a number\nstore\n  .validate(null, {isNumber: false})\n  .then(report =\u003e {\n    report.isValid(); // -\u003e true\n    report.value; // -\u003e null\n  });\n```\n\n## Complex example\n\nCreate independent rule and recipes. Add them to store and validate values using\nrecipe by name.\n\n```javascript\nvar pharmacy = require('pharmacy');\n\nvar store = new pharmacy.Store();\n\n// Create and add the rule.\nvar isBool = new pharmacy.Rule({\n  // Extend field object methods.\n  field(field) {\n    field.isBool = function () {\n      return typeof this.value === 'boolean';\n    };\n  },\n  // Sanitizer could convert values from string to acceptable value.\n  sanitize(accept, value) {\n    if (value === 'true') {\n      value = true;\n    } else if (value === 'false') {\n      value = false;\n    }\n\n    return value;\n  },\n  // Validate the value after sanitizing.\n  validate(accept, value, field) {\n    return  field.isBool() === accept;\n  }\n});\n\nstore.addRule('isBool', isBool);\n\n// Create and add the recipe.\nvar recipe = new pharmacy.Recipe({\n  isBool: true\n});\n\nstore.addRecipe('isTrue', recipe);\n\n// Use recipe\nstore\n  .validate(true, 'isTrue')\n  .then(report =\u003e {\n    report.isValid(); // -\u003e true\n    report.value; // -\u003e true\n  });\n\n// Use recipe\nstore\n  .validate(10, 'isTrue')\n  .then(report =\u003e {\n    report.isValid(); // -\u003e false\n    report.value; // -\u003e 10\n  });\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frumkin%2Fpharmacy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frumkin%2Fpharmacy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frumkin%2Fpharmacy/lists"}