{"id":21366146,"url":"https://github.com/tonytonyjan/sukima","last_synced_at":"2025-06-11T04:38:43.460Z","repository":{"id":246348431,"uuid":"819736002","full_name":"tonytonyjan/sukima","owner":"tonytonyjan","description":"  Sukima is a lightweight data schema validation library for Ruby written in only ~100 lines of code. It provides a simple and flexible way to define constraints for data and validate it.","archived":false,"fork":false,"pushed_at":"2024-07-23T02:02:01.000Z","size":10,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-22T20:11:23.986Z","etag":null,"topics":["ruby","schema","validation"],"latest_commit_sha":null,"homepage":"","language":"Ruby","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/tonytonyjan.png","metadata":{"files":{"readme":"README.adoc","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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-25T05:31:04.000Z","updated_at":"2024-07-23T02:02:05.000Z","dependencies_parsed_at":"2024-06-27T10:02:18.359Z","dependency_job_id":"8195b0f4-3e62-406d-b787-72af1b9c8de5","html_url":"https://github.com/tonytonyjan/sukima","commit_stats":null,"previous_names":["tonytonyjan/sukima"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tonytonyjan%2Fsukima","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tonytonyjan%2Fsukima/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tonytonyjan%2Fsukima/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tonytonyjan%2Fsukima/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tonytonyjan","download_url":"https://codeload.github.com/tonytonyjan/sukima/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243841206,"owners_count":20356441,"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":["ruby","schema","validation"],"created_at":"2024-11-22T07:13:55.571Z","updated_at":"2025-03-16T07:42:35.321Z","avatar_url":"https://github.com/tonytonyjan.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"= Sukima image:https://github.com/tonytonyjan/sukima/actions/workflows/test.yml/badge.svg[]\n\nSukima is a lightweight data schema validation library for Ruby written in only ~100 lines of code.\nIt provides a simple and flexible way to define constraints for data schema and validate it.\n\n== Usage\n\nConstraints are defined by keyword arguments such as `:type`, `:nonnil`, `:format`.\n\n[source,ruby]\n----\nsukima = Sukima.new **constraints\nresult = sukima.validate(data)\nresult.to_h # =\u003e Nested hash of validation results\nresult.message # =\u003e Array of error messages\n----\n\nTo see built-in constraints, check `lib/sukima/constraints.rb`.\n\n== Quick Start\n\n[source,ruby]\n----\nrequire 'sukima'\n\nsukima = Sukima.new type: Hash do\n  field :id, required: true\n  field :name, type: String, format: /\\A[a-z]+\\z/\n  field :age, type: Integer, in: 20..100\n  field :nicknames, type: Array, length: 3 do\n    items type: String, nonnil: true\n  end\nend\n\nresult = sukima.validate( {name: 'JOHN', age: 18, nicknames: [1, nil] })\n\nresult.valid? # =\u003e false\nresult[:nicknames][1].messages # =\u003e [\"should not be nil\"]\nresult.messages\n# =\u003e\n# [\"id is required\",\n#  \"name should match \\\\A[a-z]+\\\\z\",\n#  \"age should be in 20..100\",\n#  \"nicknames should have length of 3\",\n#  \"nicknames.0 should be String\",\n#  \"nicknames.1 should not be nil\"]\n----\n\n== Custom Constraints\n\nAny singleton method of `Sukima::Constraints` becomes a constraint.\nA constraint method accepts two arguments, the first is the constraint configuration passed by the user and the second is the value to be validated.\nIt returnes a message if the value is invalid, otherwise `nil`.\n\nBelow is how `lib/sukima/constraints.rb` implements the built-in constraint `:type`:\n\n[source,ruby]\n----\nclass Sukima::Constraints\n  def self.type(type, value)\n    \"should be #{type}\" unless value.is_a?(type)\n  end\nend\n----\n\n== Reusing Constraints\n\n`#field` and `#items` can take an `Sukima` object as an argument:\n\n[source,ruby]\n----\nshared = Sukima.new type: Integer, in: 1..10\n\nsukima = Sukima.new type: Hash do\n  field :score, shared\n  field :scores, type: Array do\n    items shared\n  end\nend\nsukima.validate({score: 0, scores: [11]}).messages \n# =\u003e [\"score should be in 1..10\", \"scores.0 should be in 1..10\"]\n----\n\nReusing blocks is also possible:\n\n[source,ruby]\n----\nshared = proc do\n  field :name, type: String\n  field :age, type: Integer\nend\n\nsukima = Sukima.new type: Hash do\n  field :email, type: String\n  instance_eval(\u0026shared)\nend\n\nsukima.validate({ name: 1, age: '20', email: 1 }).messages\n# =\u003e [\"email should be String\", \"name should be String\", \"age should be Integer\"]\n----\n\n== Conditional Validation\n\n`#field` and `#items` yields the value to the block so that you can define complex rules based on the value, this is useful for cases like polymorphic associations:\n\n[source,ruby]\n----\nsukima = Sukima.new type: Array do\n  items type: Hash do |hash|\n    field :type, type: String, in: %w[website user]\n\n    case hash[:type]\n    when 'website'\n      field :url, required: true\n    when 'user'\n      field :email, required: true\n    end\n  end\nend\n\nsukima.validate(\n  [\n    { type: 'website' },\n    { type: 'user' }\n  ]\n).messages\n# =\u003e [\"0.url is required\", \"1.email is required\"]\n----","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftonytonyjan%2Fsukima","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftonytonyjan%2Fsukima","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftonytonyjan%2Fsukima/lists"}