{"id":22092111,"url":"https://github.com/krakphp/schema","last_synced_at":"2025-07-24T20:31:58.241Z","repository":{"id":57009058,"uuid":"249078929","full_name":"krakphp/schema","owner":"krakphp","description":"Data Schema Declarations and Processing","archived":false,"fork":false,"pushed_at":"2023-05-17T10:44:55.000Z","size":20,"stargazers_count":1,"open_issues_count":1,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-11-13T08:47:38.168Z","etag":null,"topics":["config","declarative","dx","functional","json-schema","schema","schema-validation","symfony","validation"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/krakphp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-03-21T23:28:16.000Z","updated_at":"2023-09-07T13:51:05.000Z","dependencies_parsed_at":"2022-08-21T13:10:13.117Z","dependency_job_id":null,"html_url":"https://github.com/krakphp/schema","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krakphp%2Fschema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krakphp%2Fschema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krakphp%2Fschema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krakphp%2Fschema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/krakphp","download_url":"https://codeload.github.com/krakphp/schema/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227476144,"owners_count":17779417,"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":["config","declarative","dx","functional","json-schema","schema","schema-validation","symfony","validation"],"created_at":"2024-12-01T03:08:21.626Z","updated_at":"2024-12-01T03:08:22.490Z","avatar_url":"https://github.com/krakphp.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Schema\n\nThe schema library provides the ability to define schemas with a declarative API and in turn using any of the processors to act on that schema.\n\nWe separate the concepts of the schema definition and the processors to allow us to build an AST describing a schema, and then allow different processors handle that structure for things like validation, generate a symfony config tree, building valid json schema, etc etc.\n\n## Installation\n\nInstall with composer at `krak/schema`\n\n## Usage\n\n### Defining a Schema\n\n```php\n\u003c?php\nuse function Krak\\Schema\\{struct, listOf, dict, string, bool, int};\n\n$schema = struct([\n    'name' =\u003e string(),\n    'isAdmin' =\u003e bool(),\n    'age' =\u003e int(),\n    'tags' =\u003e listOf(string()),\n    'photos' =\u003e dict(struct([\n        'url' =\u003e string(),\n        'width' =\u003e int(),\n        'height' =\u003e int(),\n    ]))\n]);\n/* would match a structure like: \n{\n  \"name\": \"Bob\",\n  \"isAdmin\": true,\n  \"age\": 26,\n  \"tags\": [\"tall\", \"dark\", \"handsome\"],\n  \"photos\": {\n    \"small\": {\n      \"url\": \"https://mydomain.com/images/bob/small\",\n      \"width\": 100,\n      \"height\": 200\n    },\n    \"large\": {\n      \"url\": \"https://mydomain.com/images/bob/large\",\n      \"width\": 600,\n      \"height\": 1200\n    },\n  }\n}\n*/\n```\n\n### Validation (Coming Soon)\n\nEventually we'll support the ability to take a schema and validate array structures against them.\n\n### Symfony Config Tree Processor\n\nDeclare and build symfony config tree builders declaratively with the `configTree` schema processor.\n\n```php\n\u003c?php\n\nuse Symfony\\Component\\Config\\Definition\\{ConfigurationInterface, TreeBuilder};\nuse function Krak\\Schema\\ProcessSchema\\SymfonyConfig\\configTree;\nuse function Krak\\Schema\\{struct, string};\n\nfinal class Configuration implements ConfigurationInterface\n{\n    public function getConfigTreeBuilder() {\n        return configTree('aws', struct([\n            'version' =\u003e string(),\n            'region' =\u003e string(),\n            'credentials' =\u003e struct([\n                'key' =\u003e string(),\n                'secret' =\u003e string(),\n            ])\n        ]));\n    }\n}\n```\n\n*Note:* This currently supports Symfony 4 and 5 config.\n\n[Check out the feature test suite to see examples of all the supported api.](test/feature/SymfonyConfigTest.php)\n\n#### Comparison of Declarative vs Builder Syntax\n\nHere's a seemingly simple config file that we'd want to validate the schema of:\n\n```yaml\nmy_package:\n  string_key: 'abc'\n  int_key: 1\n  struct_key:\n    a: 1\n    b: 2\n  list_key: [1, 2, 3]\n  list_of_struct_key:\n    - a: 1\n      b: 2\n  struct_of_list:\n    a: ['', '']\n    b: [0, 0]\n```\n\nHere is the builder syntax:\n\n```php\nreturn (new TreeBuilder('my_package'))-\u003egetRootNode();\n    -\u003echildren()\n        -\u003escalarNode('string_key')-\u003eend()\n        -\u003eintegerNode('int_key')-\u003eend()\n        -\u003earrayNode('struct_key')\n            -\u003echildren()\n                -\u003escalarNode('a')-\u003eend()\n                -\u003eintegerNode('b')-\u003eend()\n            -\u003eend()\n        -\u003eend()\n        -\u003earrayNode('list_key')\n            -\u003eintegerPrototype()-\u003eend()\n        -\u003eend()\n        -\u003earrayNode('list_of_struct_key')\n            -\u003earrayPrototype()\n                -\u003echildren()\n                    -\u003eintegerNode('a')-\u003eend()\n                    -\u003eintegerNode('b')-\u003eend()\n                -\u003eend()\n            -\u003eend()\n        -\u003eend()\n        -\u003earrayNode('struct_of_list_key')\n            -\u003echildren()\n                -\u003earrayNode('a')\n                    -\u003escalarPrototype()-\u003eend()\n                -\u003eend()\n                -\u003earrayNode('b')\n                    -\u003eintegerPrototype()-\u003eend()\n                -\u003eend()\n            -\u003eend()\n        -\u003eend()\n    -\u003eend()\n-\u003eend();\n```\n\nHere is the declarative syntax for the same definition:\n\n```php\nreturn configTree('my_package', struct([\n    'string_key' =\u003e string(),\n    'int_key' =\u003e int(),\n    'struct_key' =\u003e struct([\n        'a' =\u003e int(),\n        'b' =\u003e int(),\n    ]),\n    'list_key' =\u003e listOf(int()),\n    'list_of_struct_key' =\u003e listOf(struct([\n        'a' =\u003e int(),\n        'b' =\u003e int(),\n    ])),\n    'struct_of_list_key' =\u003e struct([\n        'a' =\u003e listOf(string()),\n        'b' =\u003e listOf(int()),\n    ])\n]));\n```\n\n#### References\n\nOriginal RFC Pull Request to Symfony: https://github.com/symfony/symfony/issues/35127\n\n## Documentation\n\nNo formal API documentation is setup, but the src dir is under 200loc at this point. Also the tests directory gives a good overview of the various features as well.\n\n- [Symfony Config Feature Tests](./test/feature/SymfonyConfigTest.php)\n\n## Testing\n\nRun `composer test` to run the test suite.\n\n## Roadmap\n\n- Api Documentation\n- Additional schema fns to support more string/numeric constraints (regex, min, max, etc)\n- JSON Schema ProcessSchema\n  - Create the ability to export a schema definition to valid json schema json\n- Validation ProcessSchema\n  - Create a function validation library for basic schemas\n  - Support custom validators and schema fns\n- Symfony Validation ProcessSchema\n  - Export to symfony constraints to use with the SF validator. With one api, we could define schemas that export to SF config and SF validation!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrakphp%2Fschema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkrakphp%2Fschema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrakphp%2Fschema/lists"}