{"id":20015463,"url":"https://github.com/ryanwinchester/plasm","last_synced_at":"2025-05-04T22:31:32.948Z","repository":{"id":57042508,"uuid":"105609082","full_name":"ryanwinchester/plasm","owner":"ryanwinchester","description":"Schema and Changeset Validation for PHP","archived":false,"fork":false,"pushed_at":"2017-10-07T17:11:47.000Z","size":77,"stargazers_count":4,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-08T12:51:48.393Z","etag":null,"topics":["changeset","php","validation"],"latest_commit_sha":null,"homepage":"","language":"PHP","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/ryanwinchester.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":"2017-10-03T03:00:47.000Z","updated_at":"2022-03-29T01:16:00.000Z","dependencies_parsed_at":"2022-08-23T23:40:09.515Z","dependency_job_id":null,"html_url":"https://github.com/ryanwinchester/plasm","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/ryanwinchester%2Fplasm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryanwinchester%2Fplasm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryanwinchester%2Fplasm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryanwinchester%2Fplasm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ryanwinchester","download_url":"https://codeload.github.com/ryanwinchester/plasm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252408263,"owners_count":21743083,"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":["changeset","php","validation"],"created_at":"2024-11-13T07:46:11.991Z","updated_at":"2025-05-04T22:31:32.548Z","avatar_url":"https://github.com/ryanwinchester.png","language":"PHP","readme":"# Plasm\n\n[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/ryanwinchester/plasm/master/LICENSE)\n [![Build Status](https://travis-ci.org/ryanwinchester/plasm.svg?branch=master)](https://travis-ci.org/ryanwinchester/plasm)\n [![codecov](https://img.shields.io/codecov/c/github/ryanwinchester/plasm.svg)](https://codecov.io/gh/ryanwinchester/plasm)\n [![Code Climate](https://codeclimate.com/github/ryanwinchester/plasm/badges/gpa.svg)](https://codeclimate.com/github/ryanwinchester/plasm)\n\nFilter, cast, and validate incoming data from **forms**, **API**s, **CLI**, etc.\n\nSchema and Changeset for PHP are inspired by `Ecto.Changeset`\nfrom [Elixir's Ecto library](https://hexdocs.pm/ecto/Ecto.Changeset.html).\n\n### In Development!\n\n\n### Planned for V1.0:\n\n- [ ] Default messages str replacements\n- [ ] Finish default validators\n- [ ] One or two provided Framework/ORM integrations\n- [ ] Figure how to implement DB constraints from Integrations (unique, etc.)\n\n## Install\n\nUse [composer](https://getcomposer.org).\n\n```sh\ncomposer require plasm/plasm:dev-master@dev\n```\n\n## Usage\n\n### 1) Define a Schema:\n\nIn the schema we specify all the fields we care about and specify what type\nwe want them to be cast to.\n\nOptions:\n\n- `type`: required. the type the field should be cast to\n- `default`: Will default to this value if not present in changeset `$attrs`\n- `virtual`: This will be a future to mark fields as not for storing\n\n```php\n\u003c?php\n\nuse Plasm\\Schema;\n\nclass UserSchema extends Schema\n{\n    public function definition()\n    {\n        return [\n            'name' =\u003e ['type' =\u003e 'string'],\n            'email' =\u003e ['type' =\u003e 'string'],\n            'is_admin' =\u003e ['type' =\u003e 'boolean', 'default' =\u003e false],\n            'age' =\u003e ['type' =\u003e 'integer'],\n            'money' =\u003e ['type' =\u003e 'float'],\n            'password' =\u003e ['type' =\u003e 'string', 'virtual' =\u003e true],\n            'password_confirmation' =\u003e ['type' =\u003e 'string', 'virtual' =\u003e true],\n            'password_hash' =\u003e ['type' =\u003e 'string'],\n            'nothing' =\u003e ['type' =\u003e 'string', 'default' =\u003e null]\n        ];\n    }\n}\n```\n\n### 2) Define a Changeset:\n\nYou can define multiple changesets in the same class. You can create completely different ones or build on top of others.\n\nFor example, below we'll have a `createChangeset` for creating a user that just builds off our generic `changeset` and\nmaking some of the fields required.\n\n```php\n\u003c?php\n\nuse Plasm\\Changeset;\n\nclass UserChangeset extends Changeset\n{\n    /**\n     * Changeset for a User.\n     */\n    public function changeset($attrs)\n    {\n        return $this\n            -\u003ecast(['name', 'email', 'is_admin', 'age', 'money', 'password', 'nothing'])\n            -\u003evalidateFormat('email', '/.+@.+\\..+/')\n            -\u003evalidateLength('password', ['min' =\u003e 8])\n            -\u003evalidateConfirmation('password')\n            -\u003evalidateNumber('age', ['greater_than' =\u003e 12], 'You need to be at least 13');\n    }\n\n    /**\n     * Changeset for creating a User.\n     */\n    public function createChangeset($attrs)\n    {\n        return $this\n            -\u003echangeset($attrs)\n            -\u003evalidateRequired(['name', 'email', 'age', 'password'])\n            -\u003evalidateChange(\n                'password',\n                $this-\u003evalidatePassStrength(),\n                'Your password is too weak'\n            );\n    }\n\n    /**\n     * A custom validator for checking password strength.\n     */\n    private function validatePassStrength()\n    {\n        return function($password) {\n            $zxcvbn = new \\ZxcvbnPhp\\Zxcvbn();\n            $strength = $zxcvbn-\u003epasswordStrength($password);\n\n            return $strength['score'] \u003e= 3;\n        };\n    }\n}\n```\n\n### 3) Use them somewhere:\n\nJust for example's sake, the example below looks a lot like a typical Laravel controller's\n`store` method.\n\nWe'll pass all the request data into the `createChangeset` changeset and\nnot worry since our `cast` method will filter out the fields we specify, cast them\nto their specified types, and validate them.\n\nIf we used the `EloquentChangesets` trait we could call the `createModel` method after checking\nif the changeset is valid. If it wasn't valid we can return to the view with the changeset\nand display the changeset errors to the user.\n\n```php\nfunction store($request)\n{\n    $changeset = UserChangeset::using(UserSchema::class)\n        -\u003ecreateChangeset($request-\u003eall());\n\n    if (! $changeset-\u003evalid()) {\n        return back()-\u003ewith('changeset', $changeset);\n    }\n\n    $user = $changeset-\u003ecreateModel();\n\n    return redirect()-\u003eroute('users/index')\n        -\u003ewith('success', \"User {$user-\u003eemail} added\");\n}\n```\n\n## License\n\nMIT\n\n## Credits\n\n- [Ecto.Changeset](https://hexdocs.pm/ecto/Ecto.Changeset.html): Most of the ideas come from here.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryanwinchester%2Fplasm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fryanwinchester%2Fplasm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryanwinchester%2Fplasm/lists"}