{"id":13508036,"url":"https://github.com/frictionlessdata/tableschema-js","last_synced_at":"2025-10-04T01:19:42.045Z","repository":{"id":32717350,"uuid":"36307066","full_name":"frictionlessdata/tableschema-js","owner":"frictionlessdata","description":"A JavaScript library for working with Table Schema.","archived":false,"fork":false,"pushed_at":"2023-08-26T20:42:39.000Z","size":473,"stargazers_count":82,"open_issues_count":10,"forks_count":27,"subscribers_count":13,"default_branch":"main","last_synced_at":"2024-05-22T21:45:26.917Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://frictionlessdata.io/","language":"JavaScript","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":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2015-05-26T15:56:55.000Z","updated_at":"2023-08-26T20:34:48.000Z","dependencies_parsed_at":"2024-01-13T21:45:25.016Z","dependency_job_id":null,"html_url":"https://github.com/frictionlessdata/tableschema-js","commit_stats":{"total_commits":299,"total_committers":25,"mean_commits":11.96,"dds":0.5752508361204014,"last_synced_commit":"54b5a88cc417d35d0a38be338f501cd4a4bd3777"},"previous_names":["okfn/json-table-schema-js","frictionlessdata/jsontableschema-js"],"tags_count":64,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frictionlessdata%2Ftableschema-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frictionlessdata%2Ftableschema-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frictionlessdata%2Ftableschema-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frictionlessdata%2Ftableschema-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/frictionlessdata","download_url":"https://codeload.github.com/frictionlessdata/tableschema-js/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246301963,"owners_count":20755512,"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":[],"created_at":"2024-08-01T02:00:46.320Z","updated_at":"2025-10-04T01:19:36.977Z","avatar_url":"https://github.com/frictionlessdata.png","language":"JavaScript","readme":"# tableschema-js\n\n[![Build](https://img.shields.io/github/workflow/status/frictionlessdata/tableschema-js/general/main)](https://github.com/frictionlessdata/tableschema-js/actions)\n[![Coverage](https://img.shields.io/codecov/c/github/frictionlessdata/tableschema-js/main)](https://codecov.io/gh/frictionlessdata/tableschema-js)\n[![Registry](https://img.shields.io/npm/v/frictionless-tableschema-js.svg)](https://www.npmjs.com/package/frictionless-tableschema-js)\n[![Codebase](https://img.shields.io/badge/github-main-brightgreen)](https://github.com/frictionlessdata/tableschema-js)\n[![Support](https://img.shields.io/badge/support-discord-brightgreen)](https://discordapp.com/invite/Sewv6av)\n\nA library for working with [Table Schema](http://specs.frictionlessdata.io/table-schema/).\n\n## Features\n\n- `Table` class for working with data and schema\n- `Schema` class for working with schemas\n- `Field` class for working with schema fields\n- `validate` function for validating schema descriptors\n- `infer` function that creates a schema based on a data sample\n\n## Contents\n\n\u003c!-- START doctoc generated TOC please keep comment here to allow auto update --\u003e\n\u003c!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --\u003e\n\n\n- [Getting started](#getting-started)\n  - [Installation](#installation)\n- [Documentation](#documentation)\n  - [Introduction](#introduction)\n  - [Working with Table](#working-with-table)\n  - [Working with Schema](#working-with-schema)\n  - [Working with Field](#working-with-field)\n  - [Working with validate/infer](#working-with-validateinfer)\n- [API Reference](#api-reference)\n  - [Table](#table)\n  - [Schema](#schema)\n  - [Field](#field)\n  - [validate(descriptor) ⇒ \u003ccode\u003eObject\u003c/code\u003e](#validatedescriptor-%E2%87%92-codeobjectcode)\n  - [infer(source, headers, options) ⇒ \u003ccode\u003eObject\u003c/code\u003e](#infersource-headers-options-%E2%87%92-codeobjectcode)\n  - [DataPackageError](#datapackageerror)\n  - [TableSchemaError](#tableschemaerror)\n- [Contributing](#contributing)\n- [Changelog](#changelog)\n\n\u003c!-- END doctoc generated TOC please keep comment here to allow auto update --\u003e\n\n## Getting started\n\n\u003e To use the library with `webpack` please replicate the `webpack.config.js-\u003enode` configuration - https://github.com/frictionlessdata/tableschema-js/blob/master/webpack.config.js\n\n### Installation\n\nThe package use semantic versioning. It means that major versions could include breaking changes. It's highly recommended to specify `tableschema` version range in your `package.json` file e.g. `tabulator: ^1.0` which  will be added by default by `npm install --save`.\n\n#### NPM\n\n```bash\n$ npm install tableschema\n```\n\n#### CDN\n\n```html\n\u003cscript src=\"//unpkg.com/tableschema/dist/tableschema.min.js\"\u003e\u003c/script\u003e\n```\n\n## Documentation\n\n### Introduction\n\nLet's start with a simple example for Node.js:\n\n```javascript\nconst {Table} = require('tableschema')\n\nconst table = await Table.load('data.csv')\nawait table.infer() // infer a schema\nawait table.read({keyed: true}) // read the data\nawait table.schema.save() // save the schema\nawait table.save() // save the data\n```\n\nAnd for browser:\n\n\u003e https://jsfiddle.net/rollninja/ayngwd38/2/\n\nAfter the script registration the library will be available as a global variable `tableschema`:\n\n```html\n\u003c!DOCTYPE html\u003e\n\u003chtml lang=\"en\"\u003e\n  \u003chead\u003e\n    \u003cmeta charset=\"utf-8\"\u003e\n    \u003ctitle\u003etableschema-js\u003c/title\u003e\n  \u003c/head\u003e\n  \u003cbody\u003e\n    \u003cscript src=\"//unpkg.com/tableschema/dist/tableschema.min.js\"\u003e\u003c/script\u003e\n    \u003cscript\u003e\n      const main = async () =\u003e {\n        const table = await tableschema.Table.load('https://raw.githubusercontent.com/frictionlessdata/datapackage-js/master/data/data.csv')\n        const rows = await table.read()\n        document.body.innerHTML += `\u003cdiv\u003e${table.headers}\u003c/div\u003e`\n        for (const row of rows) {\n          document.body.innerHTML += `\u003cdiv\u003e${row}\u003c/div\u003e`\n        }\n      }\n      main()\n    \u003c/script\u003e\n  \u003c/body\u003e\n\u003c/html\u003e\n```\n\n### Working with Table\n\nA table is a core concept in a tabular data world. It represents data with metadata (Table Schema). Let's see how we could use it in practice.\n\nConsider we have some local csv file. It could be inline data or remote link - all supported by `Table` class (except local files for in-browser usage of course). But say it's `data.csv` for now:\n\n```csv\ncity,location\nlondon,\"51.50,-0.11\"\nparis,\"48.85,2.30\"\nrome,N/A\n```\n\nLet's create and read a table. We use static `Table.load` method and `table.read` method with a `keyed` option to get array of keyed rows:\n\n```javascript\nconst table = await Table.load('data.csv')\ntable.headers // ['city', 'location']\nawait table.read({keyed: true})\n// [\n//   {city: 'london', location: '51.50,-0.11'},\n//   {city: 'paris', location: '48.85,2.30'},\n//   {city: 'rome', location: 'N/A'},\n// ]\n```\n\nAs we could see our locations are just strings. But it should be geopoints. Also Rome's location is not available but it's also just a `N/A` string instead of JavaScript `null`. First we have to infer Table Schema:\n\n```javascript\nawait table.infer()\ntable.schema.descriptor\n// { fields:\n//   [ { name: 'city', type: 'string', format: 'default' },\n//     { name: 'location', type: 'geopoint', format: 'default' } ],\n//  missingValues: [ '' ] }\nawait table.read({keyed: true})\n// Fails with a data validation error\n```\n\nLet's fix not available location. There is a `missingValues` property in Table Schema specification. As a first try we set `missingValues` to `N/A` in `table.schema.descriptor`. Schema descriptor could be changed in-place but all changes should be committed by `table.schema.commit()`:\n\n```javascript\ntable.schema.descriptor['missingValues'] = 'N/A'\ntable.schema.commit()\ntable.schema.valid // false\ntable.schema.errors\n// Error: Descriptor validation error:\n//   Invalid type: string (expected array)\n//    at \"/missingValues\" in descriptor and\n//    at \"/properties/missingValues/type\" in profile\n```\n\nAs a good citizens we've decided to check out schema descriptor validity. And it's not valid! We should use an array for `missingValues` property. Also don't forget to have an empty string as a missing value:\n\n```javascript\ntable.schema.descriptor['missingValues'] = ['', 'N/A']\ntable.schema.commit()\ntable.schema.valid // true\n```\n\nAll good. It looks like we're ready to read our data again:\n\n```javascript\nawait table.read({keyed: true})\n// [\n//   {city: 'london', location: [51.50,-0.11]},\n//   {city: 'paris', location: [48.85,2.30]},\n//   {city: 'rome', location: null},\n// ]\n```\n\nNow we see that:\n\n- locations are arrays with numeric latitude and longitude\n- Rome's location is a native JavaScript `null`\n\nAnd because there are no errors on data reading we could be sure that our data is valid against our schema. Let's save it:\n\n```javascript\nawait table.schema.save('schema.json')\nawait table.save('data.csv')\n```\n\nOur `data.csv` looks the same because it has been stringified back to `csv` format. But now we have `schema.json`:\n\n```json\n{\n    \"fields\": [\n        {\n            \"name\": \"city\",\n            \"type\": \"string\",\n            \"format\": \"default\"\n        },\n        {\n            \"name\": \"location\",\n            \"type\": \"geopoint\",\n            \"format\": \"default\"\n        }\n    ],\n    \"missingValues\": [\n        \"\",\n        \"N/A\"\n    ]\n}\n\n```\n\nIf we decide to improve it even more we could update the schema file and then open it again. But now providing a schema path and iterating thru the data using Node Streams:\n\n```javascript\nconst table = await Table.load('data.csv', {schema: 'schema.json'})\nconst stream = await table.iter({stream: true})\nstream.on('data', (row) =\u003e {\n  // handle row ['london', [51.50,-0.11]] etc\n  // keyed/extended/cast supported in a stream mode too\n})\n```\n\nIt was only basic introduction to the `Table` class. To learn more let's take a look on `Table` class API reference.\n\n### Working with Schema\n\nA model of a schema with helpful methods for working with the schema and supported data. Schema instances can be initialized with a schema source as a url to a JSON file or a JSON object. The schema is initially validated (see [validate](#validate) below). By default validation errors will be stored in `schema.errors` but in a strict mode it will be instantly raised.\n\nLet's create a blank schema. It's not valid because `descriptor.fields` property is required by the [Table Schema](http://specs.frictionlessdata.io/table-schema/) specification:\n\n```javascript\nconst schema = await Schema.load({})\nschema.valid // false\nschema.errors\n// Error: Descriptor validation error:\n//         Missing required property: fields\n//         at \"\" in descriptor and\n//         at \"/required/0\" in profile\n```\n\nTo not create a schema descriptor by hands we will use a `schema.infer` method to infer the descriptor from given data:\n\n```javascript\nschema.infer([\n  ['id', 'age', 'name'],\n  ['1','39','Paul'],\n  ['2','23','Jimmy'],\n  ['3','36','Jane'],\n  ['4','28','Judy'],\n])\nschema.valid // true\nschema.descriptor\n//{ fields:\n//   [ { name: 'id', type: 'integer', format: 'default' },\n//     { name: 'age', type: 'integer', format: 'default' },\n//     { name: 'name', type: 'string', format: 'default' } ],\n//  missingValues: [ '' ] }\n```\n\nNow we have an inferred schema and it's valid. We could cast data row against our schema. We provide a string input by an output will be cast correspondingly:\n\n```javascript\nschema.castRow(['5', '66', 'Sam'])\n// [ 5, 66, 'Sam' ]\n```\n\nBut if we try provide some missing value to `age` field cast will fail because for now only one possible missing value is an empty string. Let's update our schema:\n\n```javascript\nschema.castRow(['6', 'N/A', 'Walt'])\n// Cast error\nschema.descriptor.missingValues = ['', 'N/A']\nschema.commit()\nschema.castRow(['6', 'N/A', 'Walt'])\n// [ 6, null, 'Walt' ]\n```\n\nWe could save the schema to a local file. And we could continue the work in any time just loading it from the local file:\n\n```javascript\nawait schema.save('schema.json')\nconst schema = await Schema.load('schema.json')\n```\n\nIt was only basic introduction to the `Schema` class. To learn more let's take a look on `Schema` class API reference.\n\n### Working with Field\n\nClass represents a field in the schema.\n\nData values can be cast to native JavaScript types. Casting a value will check the value is of the expected type, is in the correct format, and complies with any constraints imposed by a schema.\n\n```javascript\n{\n    'name': 'birthday',\n    'type': 'date',\n    'format': 'default',\n    'constraints': {\n        'required': True,\n        'minimum': '2015-05-30'\n    }\n}\n```\n\nFollowing code will not raise the exception, despite the fact our date is less than minimum constraints in the field, because we do not check constraints of the field descriptor\n\n```javascript\nvar dateType = field.castValue('2014-05-29')\n```\n\nAnd following example will raise exception, because we set flag 'skip constraints' to `false`, and our date is less than allowed by `minimum` constraints of the field. Exception will be raised as well in situation of trying to cast non-date format values, or empty values\n\n```javascript\ntry {\n    var dateType = field.castValue('2014-05-29', false)\n} catch(e) {\n    // uh oh, something went wrong\n}\n```\n\nValues that can't be cast will raise an `Error` exception.\nCasting a value that doesn't meet the constraints will raise an `Error` exception.\n\nAvailable types, formats and resultant value of the cast:\n\n| Type | Formats | Casting result |\n| ---- | ------- | -------------- |\n| any | default | Any |\n| array | default | Array |\n| boolean | default | Boolean |\n| date | default, any, \\\u003cPATTERN\\\u003e | Date |\n| datetime | default, any, \\\u003cPATTERN\\\u003e | Date |\n| duration | default | moment.Duration |\n| geojson | default, topojson | Object |\n| geopoint | default, array, object | [Number, Number] |\n| integer | default | Number |\n| number | default | Number |\n| object | default | Object |\n| string | default, uri, email, binary | String |\n| time | default, any, \\\u003cPATTERN\\\u003e | Date |\n| year | default | Number |\n| yearmonth | default | [Number, Number] |\n\n### Working with validate/infer\n\n\u003e `validate()` validates whether a **schema** is a validate Table Schema accordingly to the [specifications](http://schemas.datapackages.org/json-table-schema.json). It does **not** validate data against a schema.\n\nGiven a schema descriptor `validate` returns `Promise` with a validation object:\n\n```javascript\nconst {validate} = require('tableschema')\n\nconst {valid, errors} = await validate('schema.json')\nfor (const error of errors) {\n  // inspect Error objects\n}\n```\n\nGiven data source and headers `infer` will return a Table Schema as a JSON object based on the data values.\n\nGiven the data file, example.csv:\n\n```csv\nid,age,name\n1,39,Paul\n2,23,Jimmy\n3,36,Jane\n4,28,Judy\n```\n\nCall `infer` with headers and values from the datafile:\n\n```javascript\nconst descriptor = await infer('data.csv')\n```\n\nThe `descriptor` variable is now a JSON object:\n\n```javascript\n{\n  fields: [\n    {\n      name: 'id',\n      title: '',\n      description: '',\n      type: 'integer',\n      format: 'default'\n    },\n    {\n      name: 'age',\n      title: '',\n      description: '',\n      type: 'integer',\n      format: 'default'\n    },\n    {\n      name: 'name',\n      title: '',\n      description: '',\n      type: 'string',\n      format: 'default'\n    }\n  ]\n}\n```\n\n## API Reference\n\n### Table\nTable representation\n\n\n* [Table](#Table)\n    * _instance_\n        * [.headers](#Table+headers) ⇒ \u003ccode\u003eArray.\u0026lt;string\u0026gt;\u003c/code\u003e\n        * [.schema](#Table+schema) ⇒ \u003ccode\u003eSchema\u003c/code\u003e\n        * [.iter(keyed, extended, cast, forceCast, relations, stream)](#Table+iter) ⇒ \u003ccode\u003eAsyncIterator\u003c/code\u003e \\| \u003ccode\u003eStream\u003c/code\u003e\n        * [.read(limit)](#Table+read) ⇒ \u003ccode\u003eArray.\u0026lt;Array\u0026gt;\u003c/code\u003e \\| \u003ccode\u003eArray.\u0026lt;Object\u0026gt;\u003c/code\u003e\n        * [.infer(limit)](#Table+infer) ⇒ \u003ccode\u003eObject\u003c/code\u003e\n        * [.save(target)](#Table+save) ⇒ \u003ccode\u003eBoolean\u003c/code\u003e\n    * _static_\n        * [.load(source, schema, strict, headers, parserOptions)](#Table.load) ⇒ [\u003ccode\u003eTable\u003c/code\u003e](#Table)\n\n\n#### table.headers ⇒ \u003ccode\u003eArray.\u0026lt;string\u0026gt;\u003c/code\u003e\nHeaders\n\n**Returns**: \u003ccode\u003eArray.\u0026lt;string\u0026gt;\u003c/code\u003e - data source headers\n\n#### table.schema ⇒ \u003ccode\u003eSchema\u003c/code\u003e\nSchema\n\n**Returns**: \u003ccode\u003eSchema\u003c/code\u003e - table schema instance\n\n#### table.iter(keyed, extended, cast, forceCast, relations, stream) ⇒ \u003ccode\u003eAsyncIterator\u003c/code\u003e \\| \u003ccode\u003eStream\u003c/code\u003e\nIterate through the table data\n\nAnd emits rows cast based on table schema (async for loop).\nWith a `stream` flag instead of async iterator a Node stream will be returned.\nData casting can be disabled.\n\n**Returns**: \u003ccode\u003eAsyncIterator\u003c/code\u003e \\| \u003ccode\u003eStream\u003c/code\u003e - async iterator/stream of rows:\n - `[value1, value2]` - base\n - `{header1: value1, header2: value2}` - keyed\n - `[rowNumber, [header1, header2], [value1, value2]]` - extended\n**Throws**:\n\n- \u003ccode\u003eTableSchemaError\u003c/code\u003e raises any error occurred in this process\n\n\n| Param | Type | Description |\n| --- | --- | --- |\n| keyed | \u003ccode\u003eboolean\u003c/code\u003e | iter keyed rows |\n| extended | \u003ccode\u003eboolean\u003c/code\u003e | iter extended rows |\n| cast | \u003ccode\u003eboolean\u003c/code\u003e | disable data casting if false |\n| forceCast | \u003ccode\u003eboolean\u003c/code\u003e | instead of raising on the first row with cast error   return an error object to replace failed row. It will allow   to iterate over the whole data file even if it's not compliant to the schema.   Example of output stream:     `[['val1', 'val2'], TableSchemaError, ['val3', 'val4'], ...]` |\n| relations | \u003ccode\u003eObject\u003c/code\u003e | object of foreign key references in a form of   `{resource1: [{field1: value1, field2: value2}, ...], ...}`.   If provided foreign key fields will checked and resolved to its references |\n| stream | \u003ccode\u003eboolean\u003c/code\u003e | return Node Readable Stream of table rows |\n\n\n#### table.read(limit) ⇒ \u003ccode\u003eArray.\u0026lt;Array\u0026gt;\u003c/code\u003e \\| \u003ccode\u003eArray.\u0026lt;Object\u0026gt;\u003c/code\u003e\nRead the table data into memory\n\n\u003e The API is the same as `table.iter` has except for:\n\n**Returns**: \u003ccode\u003eArray.\u0026lt;Array\u0026gt;\u003c/code\u003e \\| \u003ccode\u003eArray.\u0026lt;Object\u0026gt;\u003c/code\u003e - list of rows:\n - `[value1, value2]` - base\n - `{header1: value1, header2: value2}` - keyed\n - `[rowNumber, [header1, header2], [value1, value2]]` - extended\n\n| Param | Type | Description |\n| --- | --- | --- |\n| limit | \u003ccode\u003einteger\u003c/code\u003e | limit of rows to read |\n\n\n#### table.infer(limit) ⇒ \u003ccode\u003eObject\u003c/code\u003e\nInfer a schema for the table.\n\nIt will infer and set Table Schema to `table.schema` based on table data.\n\n**Returns**: \u003ccode\u003eObject\u003c/code\u003e - Table Schema descriptor\n\n| Param | Type | Description |\n| --- | --- | --- |\n| limit | \u003ccode\u003enumber\u003c/code\u003e | limit rows sample size |\n\n\n#### table.save(target) ⇒ \u003ccode\u003eBoolean\u003c/code\u003e\nSave data source to file locally in CSV format with `,` (comma) delimiter\n\n**Returns**: \u003ccode\u003eBoolean\u003c/code\u003e - true on success\n**Throws**:\n\n- \u003ccode\u003eTableSchemaError\u003c/code\u003e an error if there is saving problem\n\n\n| Param | Type | Description |\n| --- | --- | --- |\n| target | \u003ccode\u003estring\u003c/code\u003e | path where to save a table data |\n\n\n#### Table.load(source, schema, strict, headers, parserOptions) ⇒ [\u003ccode\u003eTable\u003c/code\u003e](#Table)\nFactory method to instantiate `Table` class.\n\nThis method is async and it should be used with await keyword or as a `Promise`.\nIf `references` argument is provided foreign keys will be checked\non any reading operation.\n\n**Returns**: [\u003ccode\u003eTable\u003c/code\u003e](#Table) - data table class instance\n**Throws**:\n\n- \u003ccode\u003eTableSchemaError\u003c/code\u003e raises any error occurred in table creation process\n\n\n| Param | Type | Description |\n| --- | --- | --- |\n| source | \u003ccode\u003estring\u003c/code\u003e \\| \u003ccode\u003eArray.\u0026lt;Array\u0026gt;\u003c/code\u003e \\| \u003ccode\u003eStream\u003c/code\u003e \\| \u003ccode\u003efunction\u003c/code\u003e | data source (one of):   - local CSV file (path)   - remote CSV file (url)   - array of arrays representing the rows   - readable stream with CSV file contents   - function returning readable stream with CSV file contents |\n| schema | \u003ccode\u003estring\u003c/code\u003e \\| \u003ccode\u003eObject\u003c/code\u003e | data schema   in all forms supported by `Schema` class |\n| strict | \u003ccode\u003eboolean\u003c/code\u003e | strictness option to pass to `Schema` constructor |\n| headers | \u003ccode\u003enumber\u003c/code\u003e \\| \u003ccode\u003eArray.\u0026lt;string\u0026gt;\u003c/code\u003e | data source headers (one of):   - row number containing headers (`source` should contain headers rows)   - array of headers (`source` should NOT contain headers rows) |\n| parserOptions | \u003ccode\u003eObject\u003c/code\u003e | options to be used by CSV parser.   All options listed at \u003chttps://csv.js.org/parse/options/\u003e.   By default `ltrim` is true according to the CSV Dialect spec. |\n\n\n### Schema\nSchema representation\n\n\n* [Schema](#Schema)\n    * _instance_\n        * [.valid](#Schema+valid) ⇒ \u003ccode\u003eBoolean\u003c/code\u003e\n        * [.errors](#Schema+errors) ⇒ \u003ccode\u003eArray.\u0026lt;Error\u0026gt;\u003c/code\u003e\n        * [.descriptor](#Schema+descriptor) ⇒ \u003ccode\u003eObject\u003c/code\u003e\n        * [.primaryKey](#Schema+primaryKey) ⇒ \u003ccode\u003eArray.\u0026lt;string\u0026gt;\u003c/code\u003e\n        * [.foreignKeys](#Schema+foreignKeys) ⇒ \u003ccode\u003eArray.\u0026lt;Object\u0026gt;\u003c/code\u003e\n        * [.fields](#Schema+fields) ⇒ \u003ccode\u003eArray.\u0026lt;Field\u0026gt;\u003c/code\u003e\n        * [.fieldNames](#Schema+fieldNames) ⇒ \u003ccode\u003eArray.\u0026lt;string\u0026gt;\u003c/code\u003e\n        * [.getField(fieldName)](#Schema+getField) ⇒ \u003ccode\u003eField\u003c/code\u003e \\| \u003ccode\u003enull\u003c/code\u003e\n        * [.addField(descriptor)](#Schema+addField) ⇒ \u003ccode\u003eField\u003c/code\u003e\n        * [.removeField(name)](#Schema+removeField) ⇒ \u003ccode\u003eField\u003c/code\u003e \\| \u003ccode\u003enull\u003c/code\u003e\n        * [.castRow(row, failFalst)](#Schema+castRow) ⇒ \u003ccode\u003eArray.\u0026lt;Array\u0026gt;\u003c/code\u003e\n        * [.infer(rows, headers)](#Schema+infer) ⇒ \u003ccode\u003eObject\u003c/code\u003e\n        * [.commit(strict)](#Schema+commit) ⇒ \u003ccode\u003eBoolean\u003c/code\u003e\n        * [.save(target)](#Schema+save) ⇒ \u003ccode\u003eboolean\u003c/code\u003e\n    * _static_\n        * [.load(descriptor, strict)](#Schema.load) ⇒ [\u003ccode\u003eSchema\u003c/code\u003e](#Schema)\n\n\n#### schema.valid ⇒ \u003ccode\u003eBoolean\u003c/code\u003e\nValidation status\n\nIt always `true` in strict mode.\n\n**Returns**: \u003ccode\u003eBoolean\u003c/code\u003e - returns validation status\n\n#### schema.errors ⇒ \u003ccode\u003eArray.\u0026lt;Error\u0026gt;\u003c/code\u003e\nValidation errors\n\nIt always empty in strict mode.\n\n**Returns**: \u003ccode\u003eArray.\u0026lt;Error\u0026gt;\u003c/code\u003e - returns validation errors\n\n#### schema.descriptor ⇒ \u003ccode\u003eObject\u003c/code\u003e\nDescriptor\n\n**Returns**: \u003ccode\u003eObject\u003c/code\u003e - schema descriptor\n\n#### schema.primaryKey ⇒ \u003ccode\u003eArray.\u0026lt;string\u0026gt;\u003c/code\u003e\nPrimary Key\n\n**Returns**: \u003ccode\u003eArray.\u0026lt;string\u0026gt;\u003c/code\u003e - schema primary key\n\n#### schema.foreignKeys ⇒ \u003ccode\u003eArray.\u0026lt;Object\u0026gt;\u003c/code\u003e\nForeign Keys\n\n**Returns**: \u003ccode\u003eArray.\u0026lt;Object\u0026gt;\u003c/code\u003e - schema foreign keys\n\n#### schema.fields ⇒ \u003ccode\u003eArray.\u0026lt;Field\u0026gt;\u003c/code\u003e\nFields\n\n**Returns**: \u003ccode\u003eArray.\u0026lt;Field\u0026gt;\u003c/code\u003e - schema fields\n\n#### schema.fieldNames ⇒ \u003ccode\u003eArray.\u0026lt;string\u0026gt;\u003c/code\u003e\nField names\n\n**Returns**: \u003ccode\u003eArray.\u0026lt;string\u0026gt;\u003c/code\u003e - schema field names\n\n#### schema.getField(fieldName) ⇒ \u003ccode\u003eField\u003c/code\u003e \\| \u003ccode\u003enull\u003c/code\u003e\nReturn a field\n\n**Returns**: \u003ccode\u003eField\u003c/code\u003e \\| \u003ccode\u003enull\u003c/code\u003e - field instance if exists\n\n| Param | Type |\n| --- | --- |\n| fieldName | \u003ccode\u003estring\u003c/code\u003e |\n\n\n#### schema.addField(descriptor) ⇒ \u003ccode\u003eField\u003c/code\u003e\nAdd a field\n\n**Returns**: \u003ccode\u003eField\u003c/code\u003e - added field instance\n\n| Param | Type |\n| --- | --- |\n| descriptor | \u003ccode\u003eObject\u003c/code\u003e |\n\n\n#### schema.removeField(name) ⇒ \u003ccode\u003eField\u003c/code\u003e \\| \u003ccode\u003enull\u003c/code\u003e\nRemove a field\n\n**Returns**: \u003ccode\u003eField\u003c/code\u003e \\| \u003ccode\u003enull\u003c/code\u003e - removed field instance if exists\n\n| Param | Type |\n| --- | --- |\n| name | \u003ccode\u003estring\u003c/code\u003e |\n\n\n#### schema.castRow(row, failFalst) ⇒ \u003ccode\u003eArray.\u0026lt;Array\u0026gt;\u003c/code\u003e\nCast row based on field types and formats.\n\n**Returns**: \u003ccode\u003eArray.\u0026lt;Array\u0026gt;\u003c/code\u003e - cast data row\n\n| Param | Type | Description |\n| --- | --- | --- |\n| row | \u003ccode\u003eArray.\u0026lt;Array\u0026gt;\u003c/code\u003e | data row as an array of values |\n| failFalst | \u003ccode\u003eboolean\u003c/code\u003e |  |\n\n\n#### schema.infer(rows, headers) ⇒ \u003ccode\u003eObject\u003c/code\u003e\nInfer and set `schema.descriptor` based on data sample.\n\n**Returns**: \u003ccode\u003eObject\u003c/code\u003e - Table Schema descriptor\n\n| Param | Type | Description |\n| --- | --- | --- |\n| rows | \u003ccode\u003eArray.\u0026lt;Array\u0026gt;\u003c/code\u003e | array of arrays representing rows |\n| headers | \u003ccode\u003einteger\u003c/code\u003e \\| \u003ccode\u003eArray.\u0026lt;string\u0026gt;\u003c/code\u003e | data sample headers (one of):   - row number containing headers (`rows` should contain headers rows)   - array of headers (`rows` should NOT contain headers rows)   - defaults to 1 |\n\n\n#### schema.commit(strict) ⇒ \u003ccode\u003eBoolean\u003c/code\u003e\nUpdate schema instance if there are in-place changes in the descriptor.\n\n**Returns**: \u003ccode\u003eBoolean\u003c/code\u003e - returns true on success and false if not modified\n**Throws**:\n\n- \u003ccode\u003eTableSchemaError\u003c/code\u003e raises any error occurred in the process\n\n\n| Param | Type | Description |\n| --- | --- | --- |\n| strict | \u003ccode\u003eboolean\u003c/code\u003e | alter `strict` mode for further work |\n\n**Example**\n```javascript\nconst descriptor = {fields: [{name: 'field', type: 'string'}]}\nconst schema = await Schema.load(descriptor)\n\nschema.getField('name').type // string\nschema.descriptor.fields[0].type = 'number'\nschema.getField('name').type // string\nschema.commit()\nschema.getField('name').type // number\n```\n\n#### schema.save(target) ⇒ \u003ccode\u003eboolean\u003c/code\u003e\nSave schema descriptor to target destination.\n\n**Returns**: \u003ccode\u003eboolean\u003c/code\u003e - returns true on success\n**Throws**:\n\n- \u003ccode\u003eTableSchemaError\u003c/code\u003e raises any error occurred in the process\n\n\n| Param | Type | Description |\n| --- | --- | --- |\n| target | \u003ccode\u003estring\u003c/code\u003e | path where to save a descriptor |\n\n\n#### Schema.load(descriptor, strict) ⇒ [\u003ccode\u003eSchema\u003c/code\u003e](#Schema)\nFactory method to instantiate `Schema` class.\n\nThis method is async and it should be used with await keyword or as a `Promise`.\n\n**Returns**: [\u003ccode\u003eSchema\u003c/code\u003e](#Schema) - returns schema class instance\n**Throws**:\n\n- \u003ccode\u003eTableSchemaError\u003c/code\u003e raises any error occurred in the process\n\n\n| Param | Type | Description |\n| --- | --- | --- |\n| descriptor | \u003ccode\u003estring\u003c/code\u003e \\| \u003ccode\u003eObject\u003c/code\u003e | schema descriptor:   - local path   - remote url   - object |\n| strict | \u003ccode\u003eboolean\u003c/code\u003e | flag to alter validation behaviour:   - if false error will not be raised and all error will be collected in `schema.errors`   - if strict is true any validation error will be raised immediately |\n\n\n### Field\nField representation\n\n\n* [Field](#Field)\n    * [new Field(descriptor, missingValues)](#new_Field_new)\n    * [.name](#Field+name) ⇒ \u003ccode\u003estring\u003c/code\u003e\n    * [.type](#Field+type) ⇒ \u003ccode\u003estring\u003c/code\u003e\n    * [.format](#Field+format) ⇒ \u003ccode\u003estring\u003c/code\u003e\n    * [.required](#Field+required) ⇒ \u003ccode\u003eboolean\u003c/code\u003e\n    * [.constraints](#Field+constraints) ⇒ \u003ccode\u003eObject\u003c/code\u003e\n    * [.descriptor](#Field+descriptor) ⇒ \u003ccode\u003eObject\u003c/code\u003e\n    * [.castValue(value, constraints)](#Field+castValue) ⇒ \u003ccode\u003eany\u003c/code\u003e\n    * [.testValue(value, constraints)](#Field+testValue) ⇒ \u003ccode\u003eboolean\u003c/code\u003e\n\n\n#### new Field(descriptor, missingValues)\nConstructor to instantiate `Field` class.\n\n**Returns**: [\u003ccode\u003eField\u003c/code\u003e](#Field) - returns field class instance\n**Throws**:\n\n- \u003ccode\u003eTableSchemaError\u003c/code\u003e raises any error occured in the process\n\n\n| Param | Type | Description |\n| --- | --- | --- |\n| descriptor | \u003ccode\u003eObject\u003c/code\u003e | schema field descriptor |\n| missingValues | \u003ccode\u003eArray.\u0026lt;string\u0026gt;\u003c/code\u003e | an array with string representing missing values |\n\n\n#### field.name ⇒ \u003ccode\u003estring\u003c/code\u003e\nField name\n\n\n#### field.type ⇒ \u003ccode\u003estring\u003c/code\u003e\nField type\n\n\n#### field.format ⇒ \u003ccode\u003estring\u003c/code\u003e\nField format\n\n\n#### field.required ⇒ \u003ccode\u003eboolean\u003c/code\u003e\nReturn true if field is required\n\n\n#### field.constraints ⇒ \u003ccode\u003eObject\u003c/code\u003e\nField constraints\n\n\n#### field.descriptor ⇒ \u003ccode\u003eObject\u003c/code\u003e\nField descriptor\n\n\n#### field.castValue(value, constraints) ⇒ \u003ccode\u003eany\u003c/code\u003e\nCast value\n\n**Returns**: \u003ccode\u003eany\u003c/code\u003e - cast value\n\n| Param | Type | Description |\n| --- | --- | --- |\n| value | \u003ccode\u003eany\u003c/code\u003e | value to cast |\n| constraints | \u003ccode\u003eObject\u003c/code\u003e \\| \u003ccode\u003efalse\u003c/code\u003e |  |\n\n\n#### field.testValue(value, constraints) ⇒ \u003ccode\u003eboolean\u003c/code\u003e\nCheck if value can be cast\n\n\n| Param | Type | Description |\n| --- | --- | --- |\n| value | \u003ccode\u003eany\u003c/code\u003e | value to test |\n| constraints | \u003ccode\u003eObject\u003c/code\u003e \\| \u003ccode\u003efalse\u003c/code\u003e |  |\n\n\n### validate(descriptor) ⇒ \u003ccode\u003eObject\u003c/code\u003e\nThis function is async so it has to be used with `await` keyword or as a `Promise`.\n\n**Returns**: \u003ccode\u003eObject\u003c/code\u003e - returns `{valid, errors}` object\n\n| Param | Type | Description |\n| --- | --- | --- |\n| descriptor | \u003ccode\u003estring\u003c/code\u003e \\| \u003ccode\u003eObject\u003c/code\u003e | schema descriptor (one of):   - local path   - remote url   - object |\n\n\n### infer(source, headers, options) ⇒ \u003ccode\u003eObject\u003c/code\u003e\nThis function is async so it has to be used with `await` keyword or as a `Promise`.\n\n**Returns**: \u003ccode\u003eObject\u003c/code\u003e - returns schema descriptor\n**Throws**:\n\n- \u003ccode\u003eTableSchemaError\u003c/code\u003e raises any error occured in the process\n\n\n| Param | Type | Description |\n| --- | --- | --- |\n| source | \u003ccode\u003estring\u003c/code\u003e \\| \u003ccode\u003eArray.\u0026lt;Array\u0026gt;\u003c/code\u003e \\| \u003ccode\u003eStream\u003c/code\u003e \\| \u003ccode\u003efunction\u003c/code\u003e | data source (one of):   - local CSV file (path)   - remote CSV file (url)   - array of arrays representing the rows   - readable stream with CSV file contents   - function returning readable stream with CSV file contents |\n| headers | \u003ccode\u003eArray.\u0026lt;string\u0026gt;\u003c/code\u003e | array of headers |\n| options | \u003ccode\u003eObject\u003c/code\u003e | any `Table.load` options |\n\n\n### DataPackageError\nBase class for the all DataPackage/TableSchema errors.\n\nIf there are more than one error you could get an additional information\nfrom the error object:\n\n```javascript\ntry {\n  // some lib action\n} catch (error) {\n  console.log(error) // you have N cast errors (see error.errors)\n  if (error.multiple) {\n    for (const error of error.errors) {\n        console.log(error) // cast error M is ...\n    }\n  }\n}\n```\n\n\n* [DataPackageError](#DataPackageError)\n    * [new DataPackageError(message, errors)](#new_DataPackageError_new)\n    * [.multiple](#DataPackageError+multiple) ⇒ \u003ccode\u003eboolean\u003c/code\u003e\n    * [.errors](#DataPackageError+errors) ⇒ \u003ccode\u003eArray.\u0026lt;Error\u0026gt;\u003c/code\u003e\n\n\n#### new DataPackageError(message, errors)\nCreate an error\n\n\n| Param | Type | Description |\n| --- | --- | --- |\n| message | \u003ccode\u003estring\u003c/code\u003e |  |\n| errors | \u003ccode\u003eArray.\u0026lt;Error\u0026gt;\u003c/code\u003e | nested errors |\n\n\n#### dataPackageError.multiple ⇒ \u003ccode\u003eboolean\u003c/code\u003e\nWhether it's nested\n\n\n#### dataPackageError.errors ⇒ \u003ccode\u003eArray.\u0026lt;Error\u0026gt;\u003c/code\u003e\nList of errors\n\n\n### TableSchemaError\nBase class for the all TableSchema errors.\n\n\n## Contributing\n\n\u003e The project follows the [Open Knowledge International coding standards](https://github.com/okfn/coding-standards). There are common commands to work with the project:\n\n```bash\n$ npm install\n$ npm run test\n$ npm run build\n```\n\n## Changelog\n\nHere described only breaking and the most important changes. The full changelog and documentation for all released versions could be found in nicely formatted [commit history](https://github.com/frictionlessdata/tableschema-js/commits/master).\n\n#### v1.12\n\n- Added support for infinite numbers: NaN, INF, -INF\n\n#### v1.11\n\n- Improved data/time validation using a conversion table and moment.js (#170)\n\n#### v1.10\n\n- Rebased on csv-parse@4\n\n#### v1.9\n\nFix bug:\n\n- URI format must have the scheme protocol to be valid ([#135](https://github.com/frictionlessdata/tableschema-js/issues/135))\n\n#### v1.8\n\nImproved behaviour:\n\n- Automatically detect the CSV delimiter if one isn't explicit set\n\n#### v1.7\n\nNew API added:\n\n- added `forceCast` flag to the the `table.iter/read` methods\n\n#### v1.6\n\nImproved behaviour:\n\n- improved validation of `string` and `geojson` types\n- added heuristics to the `infer` function\n\n#### v1.5\n\nNew API added:\n\n- added `format` option to the `Table` constructor\n- added `encoding` option to the `Table` constructor\n\n#### v1.4\n\nImproved behaviour:\n\n- Now the `infer` functions support formats inferring\n\n#### v1.3\n\nNew API added:\n\n- `error.rowNumber` if available\n- `error.columnNumber` if available\n\n#### v1.2\n\nNew API added:\n\n- `Table.load` and `infer` now accept Node Stream as a `source` argument\n\n#### v1.1\n\nNew API added:\n\n- `Table.load` and `infer` now accepts `parserOptions`\n\n#### v1.0\n\nThis version includes various big changes, including a move to asynchronous inference.\n\n#### v0.2\n\nFirst stable version of the library.\n","funding_links":[],"categories":["data validation"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrictionlessdata%2Ftableschema-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffrictionlessdata%2Ftableschema-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrictionlessdata%2Ftableschema-js/lists"}