tableschema-rb
A utility library for working with Table Schema in Ruby.
#
InstallationAdd this line to your application's Gemfile:
And then execute:
Or install it yourself as:
jsontableschema
#
Update from The library and its corresponding gem was previously called jsontableschema
.
Since version 0.3 the library was renamed tableschema
and has a gem with the same name.
The gem jsontableschema
is no longer maintained. Here are the steps to transition your code to tableschema
:
Replace
with
Replace module name
JsonTableSchema
with module nameTableSchema
. For example:with
#
Usage#
Parse a CSVValidate and cast data from a CSV as described by a schema.
Both iter
and read
take the optional parameters:
keyed
: boolean, default:false
- return the rows as Hashes with headers as keyscast
: boolean, defaulttrue
- cast values for each rowlimit
: integer, defaultnil
- stop at this many rows
#
Infer a schemaIf you don't have a schema for a CSV, and want to generate one, you can infer a schema like so:
#
Build a SchemaYou 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:
#
Cast rowTo check if a given set of values complies with the schema, you can use cast_row
:
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:
#
Validate a schemaTo make sure a schema complies with Table Schema spec, we validate each custom schema against the official Table Schema schema:
If the schema is invalid, you can access the errors via the errors
attribute
#
FieldData 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 InvalidCast
exception.
Casting a value that doesn't meet the constraints will raise a ConstraintError
exception.
#
DevelopmentAfter 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.
#
ContributingBug 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.
#
LicenseThe gem is available as open source under the terms of the MIT License.