{"id":13608287,"url":"https://github.com/ruzicka/to-json-schema","last_synced_at":"2025-06-15T18:38:52.598Z","repository":{"id":19393592,"uuid":"86946877","full_name":"ruzicka/to-json-schema","owner":"ruzicka","description":"Converts JS objects to JSON Schema","archived":false,"fork":false,"pushed_at":"2023-01-05T16:13:42.000Z","size":1067,"stargazers_count":106,"open_issues_count":19,"forks_count":22,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-14T19:55:31.125Z","etag":null,"topics":["convert","converter","json","jsonschema","schema"],"latest_commit_sha":null,"homepage":null,"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/ruzicka.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":"2017-04-01T22:34:00.000Z","updated_at":"2024-04-07T01:52:04.000Z","dependencies_parsed_at":"2023-01-13T20:20:56.603Z","dependency_job_id":null,"html_url":"https://github.com/ruzicka/to-json-schema","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ruzicka%2Fto-json-schema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ruzicka%2Fto-json-schema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ruzicka%2Fto-json-schema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ruzicka%2Fto-json-schema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ruzicka","download_url":"https://codeload.github.com/ruzicka/to-json-schema/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247393567,"owners_count":20931812,"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":["convert","converter","json","jsonschema","schema"],"created_at":"2024-08-01T19:01:25.998Z","updated_at":"2025-04-05T20:05:13.293Z","avatar_url":"https://github.com/ruzicka.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"[![npm version](https://badge.fury.io/js/to-json-schema.svg)](https://badge.fury.io/js/to-json-schema)\n[![Build Status](https://travis-ci.org/ruzicka/to-json-schema.svg?branch=master)](https://travis-ci.org/ruzicka/to-json-schema)\n[![Coverage Status](https://coveralls.io/repos/github/ruzicka/to-json-schema/badge.svg?branch=master)](https://coveralls.io/github/ruzicka/to-json-schema?branch=master)\n\n# to-json-schema\n\nConverts javascript objects (and other types) to corresponding JSON schema\n\n## Install\n\n```\nnpm install to-json-schema\n```\n\n## Example usage\n```javascript\nconst toJsonSchema = require('to-json-schema');\n\nconst objToBeConverted = {\n  name: 'David',\n  rank: 7,\n  born: '1990-04-05T15:09:56.704Z',\n  luckyNumbers: [7, 77, 5]\n};\n\nconst schema = toJsonSchema(objToBeConverted);\n```\n\n`schema` generated from above code will look like this:\n\n```javascript\n{\n  \"type\": \"object\",\n  \"properties\": {\n    \"name\": {\n      \"type\": \"string\"\n    },\n    \"rank\": {\n      \"type\": \"integer\"\n    },\n    \"born\": {\n      \"type\": \"string\",\n      \"format\": \"date-time\"\n    },\n    \"luckyNumbers\": {\n      \"type\": \"array\",\n      \"items\": {\n        \"type\": \"integer\"\n      }\n    }\n  }\n}\n```\n\n## `toJsonSchema(value, options)`\n\n`to-json-schema` exports function that converts most javascript values to JSON schema. Such a schema can be used to\nfurther validation of similar objects/values\n\n* `value`: **required** Any javascript value\n* `options`: optional options object   \n\n## Options\n\n### Common options\n\nPossible option values\n\n**required**\n(`true|false` default is `false`)\nspecify `true` to make all properties required.\n\n```javascript\nconst schema = toJsonSchema(33, {required: false});\n/*\n{\n  \"type\": \"integer\"\n}\n*/\n```\n\n```javascript\nconst schema = toJsonSchema(33, {required: true});\n/*\n{\n  \"type\": \"integer\",\n  \"required\": true\n}\n*/\n```\n\n**postProcessFnc** (`function`)\n\nparameters:\n- type (string) - JSON schema type of the `value`\n- schema (object) - Generated JSON schema\n- value - (any) - input value \n- defaultFunc (function) - standard function that is used to post-process generated schema. Takes the `type`, `schema`, `value` params.\n\nBy providing `postProcessFnc`, you can modify or replace generated schema. This function\nwill be called recursively for all the properties and sub-properties and array items from leaves to the root.\nIf you want to preserve default functionality, don't forget to call defaultFunc which is currently responsible for setting\n`required` for the schema items if there is common option `required` set tu true.   \n\n\nFollowing example is showing configuration options leading to all integer values to be automatically required\n\n```javascript\nconst options = {\n  postProcessFnc: (type, schema, value, defaultFunc) =\u003e\n    (type === 'integer') ? {...schema, required: true} : defaultFunc(type, schema, value),\n}\n\nconst instance = {\n  a: 1,\n  b: 'str',\n}\n\nconst schema = toJsonSchema(instance, options);\n/*\n{\n  type: 'object',\n  properties: {\n    a: {type: 'integer', required: true},\n    b: {type: 'string'},\n  },\n}*/\n```\n\n \n### Arrays options\n\n**arrays.mode** (`all|first|uniform|tuple` default is `all`)\n  \n`all` option causes parser to go through all array items, finding the most compatible yet most descriptive schema possible. \n\nArray items are all of compatible type:\n\n```javascript\nconst arr = [33, 44, 55];\nconst schema = toJsonSchema(arr, {arrays: {mode: 'all'}});\n/*\n{\n  \"type\": \"array\",\n  \"items\": {\n    \"type\": \"integer\"\n  }\n}\n*/\n```\n\nItems' types are incompatible. Type is omitted in schema to be able to validate input object:\n\n```javascript\nconst arr = [33, 'str', 55];\nconst schema = toJsonSchema(arr, {arrays: {mode: 'all'}});\n/* \n{\n  \"type\": \"array\"\n}\n*/\n```\n\nIncompatible in sub-item. Schema still describes object properties\n\n```javascript\nconst arr = [\n  {name: 'john', grades: [1, 2, 3]},\n  {name: 'david', grades: ['a', 'b', 'c']}\n];\nconst schema = toJsonSchema(arr, {arrays: {mode: 'all'}});\n/*\n{\n  \"type\": \"array\",\n  \"items\": {\n    \"type\": \"object\",\n    \"properties\": {\n      \"name\": {\n        \"type\": \"string\"\n      },\n      \"grades\": {\n        \"type\": \"array\" // due to incompatible array items' types, `items` field is omitted\n      }\n    }\n  }\n}\n*/\n```\n\n`first` option takes only first item in the array into account. If performance\n is a concern, you may consider this option. \n\n```javascript\nconst arr = ['str', 11, 30];\nconst schema = toJsonSchema(arr, {arrays: {mode: 'first'}});\n/* Other than first array item is ignored\n{\n  \"type\": \"array\",\n  \"items\": {\n    \"type\": \"string\"\n  }\n}\n*/\n```\n\n`uniform` option requires all items in array to have same structure (to convert to the same schema).\nIf not, error is thrown. \n\n```javascript\nconst arr = ['str', 11, 30];\nconst schema = toJsonSchema(arr, {arrays: {mode: 'uniform'}});\n/*\n Above code will throw 'Error: Invalid schema, incompatible array items'\n*/\n```\n\n`tuple` option generates a [tuple array](https://json-schema.org/understanding-json-schema/reference/array.html#tuple-validation)\n (array of objects) from arrays.\n\n```javascript\nconst arr = ['str', 11, 30];\nconst schema = toJsonSchema(arr, {arrays: {mode: 'tuple'}});\n/*\n{\n  \"type\": \"array\",\n  \"items\": [\n    {\n      \"type\": \"string\"\n    },\n    {\n      \"type\": \"integer\"\n    },\n    {\n      \"type\": \"integer\"\n    }\n  ]\n}\n*/\n```\n### Objects options\n\n\n**objects.additionalProperties** (`boolean`, default `true`)\n\nif set to `false`, all object schemas will include JSON schema property `additionalProperties: false` which makes generated schema\nto perevent any extra properties.\n\n```javascript\nconst options = {\n  objects: {additionalProperties: false},\n}\nconst obj = {\n  a: {\n    c: 1,\n    d: 1,\n  },\n  b: 'str',\n}\nconst schema = toJsonSchema(obj, options);\n/*\n{\n  type: 'object',\n  properties: {\n    a: {\n      type: 'object',\n      properties: {\n        c: {type: 'integer'},\n        d: {type: 'integer'},\n      },\n      additionalProperties: false,\n    },\n    b: {type: 'string'},\n  },\n  additionalProperties: false,\n}\n*/\n```\n\n**objects.preProcessFnc** (`function`)\n\nparameters:\n- obj - (object) - input object value that is supposed to be converted into JSON schema\n- defaultFunc (function) - standard function that is used to generate schema from object. Takes just the `obj` param.\n\nBy providing custom function you will be able to modify any object value (including nested ones) and pre-process\nit before it gets converted into schema or modify generated schema or do the schema conversion entirely by yourself.\n\nCustom function from example bellow ignores all properties other than `a` and `b` from input object:\n```javascript\nconst options = {\n  objects: {\n    preProcessFnc: (obj, defaultFnc) =\u003e defaultFnc({a: obj.a, b: obj.b})\n  }\n};\nconst obj = {a: 1, b: 2, c: 3};\nconst schema = toJsonSchema(obj, options);\n/*\n{\n  \"type\": \"object\",\n  \"properties\": {\n    \"a\": {\n      \"type\": \"integer\"\n    },\n    \"b\": {\n      \"type\": \"integer\",\n    }\n  }\n}\n*/\n```\n\n\n**objects.postProcessFnc** (`function`)\n\nparameters:\n- schema (object) - Generated JSON schema\n- obj - (object) - input value \n- defaultFunc (function) - standard function that is used to post-process generated schema. Takes the `schema`, `obj` params.\n\nBy providing `postProcessFnc`, you can modify or replace generated schema. This function\nwill be called recursively for all the properties and sub-properties and array items from leaves to the root of the `obj` object. \n\nCustom objects.postProcessFnc makes properties required on parent type level:\n\n```javascript\nconst options = {\n  objects: {\n    postProcessFnc: (schema, obj, defaultFnc) =\u003e ({...defaultFnc(schema, obj), required: Object.getOwnPropertyNames(obj)})\n  }\n};\nconst obj = {a: 1, b: 'str'};\nconst schema = toJsonSchema(obj, options);\n/*\n{\n  type: 'object',\n  properties: {\n    a: {type: 'integer'},\n    b: {type: 'string'},\n  }\n  required: ['a', 'b']\n}\n*/\n```\n\n\n### strings options\n\n**strings.preProcessFnc** (`function`)\n\nBy providing custom function you will be able to modify any string value (including nested ones) and pre-process\nit before it gets converted to schema, modify generated schema or do the schema conversion entirely by yourself.\n\nProvided function will receive two parameters:\n- `string` to be converted into JSON schema\n- default `function` that normally generates the schema. This function receives only `string` to be converted to JSON schema \n\nCustom function from example bellow converts any string of object containing string to JSON schema and if string's content is 'date' than sets the format property to 'date':\n```javascript\nconst options = {\n  strings: {\n    preProcessFnc: (value, defaultFnc) =\u003e {\n      const schema = defaultFnc(value);\n      if (value === 'date') {\n        schema.format = 'date';\n      }\n      return schema;\n    },\n  },\n}\nconst schema = toJsonSchema('date', options);\n/*\n{\n  \"type\": \"string\",\n  \"format\": \"date\"\n}\n*/\n```\n**strings.detectFormat** (`true|false` default id `true`)\n\nWhen set to true format of the strings values may be detected based on it's content.\n\nThese JSON schema string formats can be detected:\n* date-time\n* date\n* time\n* utc-millisec\n* color\n* style\n* phone\n* uri\n* email\n* ip-address\n* ipv6\n\n```javascript\nconst obj = {\n  a: '2012-07-08T16:41:41.532Z',\n  b: '+31 42 123 4567',\n  c: 'http://www.google.com/',\n  d: 'obama@whitehouse.gov'\n};\nconst schema = toJsonSchema(obj, {strings: {detectFormat: true}});\n/*\n{\n  \"type\": \"object\",\n  \"properties\": {\n    \"a\": {\n      \"type\": \"string\",\n      \"format\": \"date-time\"\n    },\n    \"b\": {\n      \"type\": \"string\",\n      \"format\": \"phone\"\n    },\n    \"c\": {\n      \"type\": \"string\",\n      \"format\": \"uri\"\n    },\n    \"d\": {\n      \"type\": \"string\",\n      \"format\": \"email\"\n    }\n  }\n}\n*/\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fruzicka%2Fto-json-schema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fruzicka%2Fto-json-schema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fruzicka%2Fto-json-schema/lists"}