A utility library for working with Table Schema in Ruby.
Add this line to your application's Gemfile:
And then execute:
Or install it yourself as:
The library and its corresponding gem was previously called
Since version 0.3 the library was renamed
tableschema and has a gem with the same name.
jsontableschema is no longer maintained. Here are the steps to transition your code to
withgem 'tableschema', '0.3.0'
Replace module name
JsonTableSchemawith module name
TableSchema. For example:JsonTableSchema::Table.new(source, schema: schema)
withTableSchema::Table.new(source, schema: schema)
Validate and cast data from a CSV as described by a schema.
read take the optional parameters:
keyed: boolean, default:
false- return the rows as Hashes with headers as keys
cast: boolean, default
true- cast values for each row
limit: integer, default
nil- stop at this many rows
If you don't have a schema for a CSV, and want to generate one, you can infer a schema like so:
You can also build a schema from scratch or modify an existing one:
add_field will ignore the updates if the updated version of the the schema fails validation.
If 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:
There are multiple methods to inspect a schema:
To check if a given set of values complies with the schema, you can use
By 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:
If the schema is invalid, you can access the errors via the
Data 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:
Casting 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.
Value that can't be cast will raise an
Casting a value that doesn't meet the constraints will raise a
After 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.
To 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.
Bug 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 code of conduct.
The gem is available as open source under the terms of the MIT License.