{"id":16916291,"url":"https://github.com/joshuawise/tiny-schema","last_synced_at":"2025-04-11T11:31:25.980Z","repository":{"id":57377258,"uuid":"145746859","full_name":"JoshuaWise/tiny-schema","owner":"JoshuaWise","description":"A minimalistic JSON validator 💍","archived":false,"fork":false,"pushed_at":"2018-09-04T04:04:10.000Z","size":47,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-18T14:57:45.840Z","etag":null,"topics":["json","schema","validator"],"latest_commit_sha":null,"homepage":"","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/JoshuaWise.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-08-22T18:23:09.000Z","updated_at":"2024-09-25T08:40:06.000Z","dependencies_parsed_at":"2022-09-26T16:41:45.138Z","dependency_job_id":null,"html_url":"https://github.com/JoshuaWise/tiny-schema","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JoshuaWise%2Ftiny-schema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JoshuaWise%2Ftiny-schema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JoshuaWise%2Ftiny-schema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JoshuaWise%2Ftiny-schema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JoshuaWise","download_url":"https://codeload.github.com/JoshuaWise/tiny-schema/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248383946,"owners_count":21094639,"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":["json","schema","validator"],"created_at":"2024-10-13T19:26:12.973Z","updated_at":"2025-04-11T11:31:25.660Z","avatar_url":"https://github.com/JoshuaWise.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# tiny-schema [![Build Status](https://travis-ci.org/JoshuaWise/tiny-schema.svg?branch=master)](https://travis-ci.org/JoshuaWise/tiny-schema)\n\nIt's like `typeof`, but it's a function.\n\n```js\nconst is = require('tiny-schema');\n\nis('number')(5); // =\u003e true\nis('number')('hello'); // =\u003e false\nis('string')('hello'); // =\u003e true\nis('boolean')(false); // =\u003e true\n```\n\nBut it understands the difference between `object` and `null`.\n\n```js\nis('object')({}); // =\u003e true\nis('object')(null); // =\u003e false\nis('null')(null); // =\u003e true\nis('null')({}); // =\u003e false\n```\n\nAnd it understands the difference between `object` and `array`.\n\n```js\nis('array')([]); // =\u003e true\nis('array')({}); // =\u003e false\nis('object')([]); // =\u003e false\n```\n\nInspired by [JSON Schema](http://json-schema.org/), it can recognize integers.\n\n```js\nis('integer')(5); // =\u003e true\nis('integer')(5.5); // =\u003e false\n```\n\nAnd since it knows integers, why not go further?\n\n```js\n// Positive\nis('+integer')(-5); // =\u003e false\nis('+integer')(5); // =\u003e true\nis('+integer')(0); // =\u003e true\n\n// Negative\nis('-number')(-5.5); // =\u003e true\nis('-number')(5.5); // =\u003e false\nis('-number')(0); // =\u003e true\n```\n\nAnd sometimes you might want to exclude those pesky zeros...\n\n```js\n// Positive\nis('++integer')(5); // =\u003e true\nis('++integer')(0); // =\u003e false\n\n// Negative\nis('--integer')(-5); // =\u003e true\nis('--integer')(0); // =\u003e false\n```\n\nValidating deep structures could be useful...\n\n```js\nis('[integer]')([-5, 0, 5]); // =\u003e true\nis('{string}')({ a: 'hello', b: 'world' }); // =\u003e true\n```\n\nNullable types? Might need those too.\n\n```js\nis('[string?]?')(['hello']); // =\u003e true\nis('[string?]?')([null]); // =\u003e true\nis('[string?]?')(null); // =\u003e true\n```\n\nAlgebraic data types are *so in* these days.\n\n```js\nis('number|string')('hello'); // =\u003e true\nis('number|string')(-123); // =\u003e true\n```\n\nNested algebraic data types within a nullable object? Oh I can validate that in one line of code.\n\n```js\nis('{integer|boolean}?')({ a: -123, b: true }); // =\u003e true\nis('{integer|boolean}?')(null); // =\u003e true\n```\n\nWhat if I need to get specific about which strings I accept?\n\n```js\nis('/^\\\\w+$/i')('not a word'); // =\u003e false\nis('/^\\\\w+$/i')('word'); // =\u003e true\n```\n\nWhat if I need to get specific about which numbers I accept?\n\n```js\nis('100-200')(150); // =\u003e true\nis('100-200')(200); // =\u003e true\nis('100-200')(201); // =\u003e false\nis('100-200')(150.5); // =\u003e false (no fractions in an integer range)\nis('100.0-200.0')(150.5); // =\u003e true (now it accepts fractions)\n```\n\nEnums are nice. Let's use those.\n\n```js\nis('\"hello\"|\"world\"|500|false')('hello'); // =\u003e true\nis('\"hello\"|\"world\"|500|false')('world'); // =\u003e true\nis('\"hello\"|\"world\"|500|false')(500); // =\u003e true\nis('\"hello\"|\"world\"|500|false')(false); // =\u003e true\n```\n\nCan every single feature mentioned thus far be nested and used in every context? Yes? Oh, great.\n\n```js\nis('true|{boolean|\"hello\"|[/^\\w+$/i?]}|500.2-600.8|[[--integer]?]');\n```\n\nJesus, okay I'll stop now.\n\n### Escaping string literals\n\nString literals can be created by using `\"\"`, `''`, or ` `` `, and each form is practically equivalent. The only difference is that each form's respective quote character must be escaped when referred to literally. Escaping is done by using two quotes in direct succession.\n\n```js\nis('\"The cow said \"\"moo\"\".\"')('The cow said \"moo\".'); // =\u003e true\nis('`The cow said \"moo\".`')('The cow said \"moo\".'); // =\u003e true\n```\n\n### Regular expressions\n\nRegular expressions are passed directly to the `RegExp` constructor. To include a literal `/` character in a regular expression, use two forward slashes in direct succession.\n\n```js\nis('/^application//json$/i')('application/json'); // =\u003e true\n```\n\n### Exclusionary ranges\n\nWhen validating a range of numbers, sometimes it's useful to exclude one or both boundaries. This can be done by using the `\u003c` or `\u003e` character on the boundary that should be excluded.\n\n```js\nis('0.0-\u003e1.0')(0); // =\u003e true\nis('0.0-\u003e1.0')(0.9999); // =\u003e true\nis('0.0-\u003e1.0')(1); // =\u003e false\n\nis('0.0\u003c-1.0')(0); // =\u003e false\nis('0.0\u003c-1.0')(0.9999); // =\u003e true\nis('0.0\u003c-1.0')(1); // =\u003e true\n\nis('0.0\u003c-\u003e1.0')(0); // =\u003e false\nis('0.0\u003c-\u003e1.0')(0.9999); // =\u003e true\nis('0.0\u003c-\u003e1.0')(1); // =\u003e false\n```\n\n\u003e The `\u003c` and `\u003e` characters don't correspond to \"less than\" and \"greater than\". Rather, they depict an arrow, and should be read as \"approaches\".\n\n### Accepting anything\n\nThe `any` type can be used to accept any value.\n\n```js\nis('any')(5); // =\u003e true\nis('any')({}); // =\u003e true\nis('any')(null); // =\u003e true\n```\n\n### Reusing validators\n\nThe functions spawned by `tiny-schema` can be reused without needing to parse the schema string more than once.\n\n```js\nconst isValid = is('{string}');\n\nisValid({ hello: 'world' }); // =\u003e true\nisValid({ hello: 5 }); // =\u003e false\n```\n\n## Limitations\n\nThe purpose of `tiny-schema` is to provide powerful JSON validation with minimal code use. Because of its terse syntax, typical validations can be done in less than one line of code. However, `tiny-schema` is not a replacement for [JSON Schema](http://json-schema.org/).\n\nUnlike JSON Schema, `tiny-schema` cannot...\n\n- validate each of an object's properties by name\n- validate that an object has certain property names\n- validate that an object *doesn't* have certain property names\n- validate that an object or array has a minimum or maximum number of properties\n- use [JSON Pointers](http://json-schema.org/latest/relative-json-pointer.html) to implement [dependent types](https://en.wikipedia.org/wiki/Dependent_type)\n\n`tiny-schema` should be seen as an easy-to-use JSON type checker on steroids—not an all-purpose, fully-featured JSON typing system.\n\n## Installation\n\n```\nnpm install --save tiny-schema\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoshuawise%2Ftiny-schema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjoshuawise%2Ftiny-schema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoshuawise%2Ftiny-schema/lists"}