{"id":31530211,"url":"https://github.com/frictionlessdata/tableschema-rb","last_synced_at":"2025-10-04T01:19:32.696Z","repository":{"id":11122812,"uuid":"68029508","full_name":"frictionlessdata/tableschema-rb","owner":"frictionlessdata","description":"A Ruby library for working with JSON Table Schema. ","archived":false,"fork":false,"pushed_at":"2022-11-07T07:39:36.000Z","size":252,"stargazers_count":12,"open_issues_count":6,"forks_count":9,"subscribers_count":9,"default_branch":"main","last_synced_at":"2025-09-19T22:22:44.300Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","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/frictionlessdata.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-09-12T17:02:38.000Z","updated_at":"2024-03-06T17:41:40.000Z","dependencies_parsed_at":"2023-01-13T16:20:40.086Z","dependency_job_id":null,"html_url":"https://github.com/frictionlessdata/tableschema-rb","commit_stats":null,"previous_names":["theodi/jsontableschema.rb"],"tags_count":15,"template":false,"template_full_name":null,"purl":"pkg:github/frictionlessdata/tableschema-rb","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frictionlessdata%2Ftableschema-rb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frictionlessdata%2Ftableschema-rb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frictionlessdata%2Ftableschema-rb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frictionlessdata%2Ftableschema-rb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/frictionlessdata","download_url":"https://codeload.github.com/frictionlessdata/tableschema-rb/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frictionlessdata%2Ftableschema-rb/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278252373,"owners_count":25956288,"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","status":"online","status_checked_at":"2025-10-03T02:00:06.070Z","response_time":53,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":"2025-10-04T01:19:20.905Z","updated_at":"2025-10-04T01:19:32.687Z","avatar_url":"https://github.com/frictionlessdata.png","language":"Ruby","readme":"# tableschema-rb\n\n[![Build](https://img.shields.io/github/workflow/status/frictionlessdata/tableschema-rb/general/main)](https://github.com/frictionlessdata/tableschema-rb/actions)\n[![Coverage](https://img.shields.io/codecov/c/github/frictionlessdata/tableschema-rb/main)](https://codecov.io/gh/frictionlessdata/tableschema-rb)\n[![Release](http://img.shields.io/gem/v/tableschema.svg)](https://rubygems.org/gems/tableschema)\n[![Codebase](https://img.shields.io/badge/codebase-github-brightgreen)](https://github.com/frictionlessdata/tableschema-rb)\n[![Support](https://img.shields.io/badge/support-discord-brightgreen)](https://discordapp.com/invite/Sewv6av)\n\nA utility library for working with [Table Schema](https://specs.frictionlessdata.io/table-schema/) in Ruby.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'tableschema'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install tableschema\n\n### Update from `jsontableschema`\n\nThe library and its corresponding gem was previously called `jsontableschema`.\nSince version 0.3 the library was renamed `tableschema` and has a gem with the same name.\n\nThe gem `jsontableschema` is no longer maintained. Here are the steps to transition your code to `tableschema`:\n\n1. Replace\n\n    ```ruby\n    gem 'jsontableschema'\n    ```\n    with\n\n    ```ruby\n    gem 'tableschema', '0.3.0'\n    ```\n\n2. Replace module name `JsonTableSchema` with module name `TableSchema`. For example:\n\n    ```ruby\n    JsonTableSchema::Table.new(source, schema: schema)\n    ```\n    with\n    ```ruby\n    TableSchema::Table.new(source, schema: schema)\n    ```\n\n## Usage\n\n### Parse a CSV\n\nValidate and cast data from a CSV as described by a schema.\n\n```ruby\nschema = {\n    fields: [\n        {\n            name: 'id',\n            title: 'Identifier',\n            type: 'integer'\n        },\n        {\n            name: 'title',\n            title: 'Title',\n            type: 'string'\n        }\n    ]\n}\n\nsource = 'https://github.com/frictionlessdata/tableschema-rb/raw/main/spec/fixtures/simple_data.csv'\n\ntable = TableSchema::Table.new(source, schema: schema)\n\n# Iterate through rows\ntable.iter{ |row| print row }\n# [1, \"foo\"]\n# [2, \"bar\"]\n# [3, \"baz\"]\n\n# Read the entire CSV in memory\ntable.read\n#=\u003e [[1,'foo'],[2,'bar'],[3,'baz']]\n```\n\nBoth `iter` and `read` take the optional parameters:\n- `keyed`: boolean, default: `false` - return the rows as Hashes with headers as keys\n- `cast`: boolean, default `true` - cast values for each row\n- `limit`: integer, default `nil` - stop at this many rows\n\n### Infer a schema\n\nIf you don't have a schema for a CSV, and want to generate one, you can infer a schema like so:\n\n```ruby\nsource = 'https://github.com/frictionlessdata/tableschema-rb/raw/main/spec/fixtures/simple_data.csv' # Can also be a url or array of arrays\n\ntable = TableSchema::Table.new(source)\ntable.infer()\ntable.schema\n#=\u003e {:fields=\u003e[{:name=\u003e\"id\", :title=\u003e\"\", :description=\u003e\"\", :type=\u003e\"integer\", :format=\u003e\"default\", :constraints=\u003e{}}, {:name=\u003e\"title\", :title=\u003e\"\", :description=\u003e\"\", :type=\u003e\"string\", :format=\u003e\"default\", :constraints=\u003e{}}]}\n```\n\n### Build a Schema\n\nYou can also build a schema from scratch or modify an existing one:\n\n```ruby\nschema = TableSchema::Schema.new({\n  fields: [],\n})\n\n# Add a field\nschema.add_field({\n  name: 'id',\n  type: 'string',\n  constraints: {\n    required: true,\n  }\n})\n\n# Remove a field\nschema.remove_field('id')\n```\n\n`add_field` will ignore the updates if the updated version of the the schema fails [validation](#validate-a-schema).\nIf you wish to prevent an invalid schema from being created or updated by raising validation errors, you can pass the `strict: true` argument to the Schema initializer:\n\n```ruby\nschema = TableSchema::Schema.new(schema_hash, strict: true)\n```\n\nThere are multiple methods to inspect a schema:\n\n```ruby\nschema_hash = {\n  fields: [\n    {\n      name: 'id',\n      type: 'string',\n      constraints: {\n        required: true,\n      },\n    },\n    {\n      name: 'height',\n      type: 'number',\n    },\n    {\n      name: 'state',\n    },\n  ],\n  primaryKey: 'id',\n  foreignKeys: [\n    {\n      fields: 'state',\n      reference: {\n          resource: 'the-resource',\n          fields: 'state_id',\n      },\n    },\n  ]\n}\nschema = TableSchema::Schema.new(schema_hash)\n\nschema.field_names\n#=\u003e [\"id\", \"height\"]\nschema.fields\n#=\u003e [{:name=\u003e\"id\", :type=\u003e\"string\", :constraints=\u003e{:required=\u003etrue}, :format=\u003e\"default\"}, {:name=\u003e\"height\", :type=\u003e\"number\", :format=\u003e\"default\", :constraints=\u003e{}}]\nschema.primary_key\n#=\u003e [\"id\"]\nschema.foreign_keys\n# =\u003e [{:fields=\u003e\"state\", :reference=\u003e{:resource=\u003e\"the-resource\", :fields=\u003e\"state_id\"}}]\nschema.get_field('id')\n# =\u003e {:name=\u003e\"id\", :type=\u003e\"string\", :constraints=\u003e{:required=\u003etrue}, :format=\u003e\"default\"}\n```\n\n#### Cast row\n\nTo check if a given set of values complies with the schema, you can use `cast_row`:\n\n```\nschema.cast_row(['string', '10.0', 'State'])\n#=\u003e ['string', 10.0, 'State']\n```\n\nBy default the converter will fail on the first error it finds. However, by passing `fail_fast: false` as the second argument the errors will be collected into an `exception.errors` attribute for you to review later. For example:\n\n```ruby\nrow = [3, 'nan', 'State']\n\nschema.cast_row(row)\n#=\u003e TableSchema::InvalidCast: 3 is not a string\nbegin\n  schema.cast_row(row, fail_fast: false)\nrescue TableSchema::MultipleInvalid =\u003e exception\n  exception.errors\nend\n#=\u003e #\u003cSet: {#\u003cTableSchema::InvalidCast: 3 is not a string\u003e,\n            #\u003cTableSchema::InvalidCast: nan is not a number\u003e}\u003e\n```\n\n### Validate a schema\n\nTo make sure a schema complies with [Table Schema spec](https://specs.frictionlessdata.io/table-schema), we validate each custom schema against the\nofficial [Table Schema schema](https://specs.frictionlessdata.io/schemas/table-schema.json):\n\n```ruby\nschema_hash = {\n  fields: [\n      { name: 'id' },\n  ]\n}\nschema = TableSchema::Schema.new(schema_hash)\nschema.validate\n#=\u003e true\n```\n\nIf the schema is invalid, you can access the errors via the `errors` attribute\n\n```ruby\nschema_hash = {\n  fields: [\n    {\n      name: 'id',\n      title: 'Identifier',\n      type: 'integer'\n    },\n    {\n      name: 'title',\n      title: 'Title',\n      type: 'string'\n    }\n  ],\n  primaryKey: 'identifier'\n}\n\nschema = TableSchema::Schema.new(schema_hash)\nschema.validate\n#=\u003e false\nschema.errors\n#=\u003e #\u003cSet: {\"The TableSchema primaryKey value `identifier` is not found in any of the schema's field names\"}\u003e\n\n# Raise error if validation fails\nschema.validate!\n#=\u003e TableSchema::SchemaException: The TableSchema primaryKey value `identifier` is not found in any of the schema's field names\n```\n\n## Field\n\nData values can be cast to native Ruby objects with a Field instance. This allows formats and constraints to be defined for the field in the [field descriptor](https://specs.frictionlessdata.io/table-schema/#field-descriptors):\n\n```ruby\n# Init field\nfield = TableSchema::Field.new({\n  name: 'over_1700',\n  type: 'number',\n  constraints: {\n    minimum: '1700',\n  },\n})\n\n# Cast a value\nfield.cast_value('12345')\n#=\u003e 12345.0\n```\n\nCasting a value will check the value is of the expected `type`, is in the correct `format`, and complies with any `constraints` imposed in the descriptor.\n\nValue that can't be cast will raise an `InvalidCast` exception.\n\nCasting a value that doesn't meet the constraints will raise a `ConstraintError` exception.\n\n```ruby\nfield.cast_value('nan')\n#=\u003e TableSchema::InvalidCast: nan is not a number\nfield.cast_value('1200')\n#=\u003e TableSchema::ConstraintError: The field `over_1700` must not be less than 1700\n```\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/frictionlessdata/tableschema-rb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrictionlessdata%2Ftableschema-rb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffrictionlessdata%2Ftableschema-rb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrictionlessdata%2Ftableschema-rb/lists"}