{"id":16584903,"url":"https://github.com/mikaelvesavuori/mikrovalid","last_synced_at":"2025-03-21T12:33:30.884Z","repository":{"id":224900804,"uuid":"764535687","full_name":"mikaelvesavuori/mikrovalid","owner":"mikaelvesavuori","description":"MikroValid is the JSON validator that cuts out all the bullshit.","archived":false,"fork":false,"pushed_at":"2024-04-27T15:10:17.000Z","size":463,"stargazers_count":5,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-05-01T16:13:17.927Z","etag":null,"topics":["json","object-validator","schema","schema-validation","validation","validator"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/mikaelvesavuori.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"mikaelvesavuori"}},"created_at":"2024-02-28T08:58:36.000Z","updated_at":"2024-06-10T12:00:22.162Z","dependencies_parsed_at":"2024-03-05T17:47:07.830Z","dependency_job_id":"ab11cb7e-e8b1-4d5c-a5fa-f97e15f022af","html_url":"https://github.com/mikaelvesavuori/mikrovalid","commit_stats":null,"previous_names":["mikaelvesavuori/mikromatch"],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikaelvesavuori%2Fmikrovalid","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikaelvesavuori%2Fmikrovalid/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikaelvesavuori%2Fmikrovalid/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikaelvesavuori%2Fmikrovalid/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mikaelvesavuori","download_url":"https://codeload.github.com/mikaelvesavuori/mikrovalid/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221815388,"owners_count":16885166,"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","object-validator","schema","schema-validation","validation","validator"],"created_at":"2024-10-11T22:46:10.541Z","updated_at":"2024-10-28T10:10:20.634Z","avatar_url":"https://github.com/mikaelvesavuori.png","language":"TypeScript","funding_links":["https://github.com/sponsors/mikaelvesavuori"],"categories":[],"sub_categories":[],"readme":"# mikrovalid\n\n**MikroValid is the minimalist, smart, and easy way to validate objects on both the client and server-side.**\n\n![Build Status](https://github.com/mikaelvesavuori/mikrovalid/workflows/main/badge.svg)\n\n[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=mikaelvesavuori_mikrovalid\u0026metric=alert_status)](https://sonarcloud.io/dashboard?id=mikaelvesavuori_mikrovalid)\n\n[![codecov](https://codecov.io/gh/mikaelvesavuori/mikrovalid/graph/badge.svg?token=2P5YYO89J2)](https://codecov.io/gh/mikaelvesavuori/mikrovalid)\n\n[![Maintainability](https://api.codeclimate.com/v1/badges/d42f8b255d893431050f/maintainability)](https://codeclimate.com/github/mikaelvesavuori/mikrovalid/maintainability)\n\n---\n\nMikroValid is the JSON validator that cuts out all the bullshit:\n\n- Dead easy, no proprietary stuff — uses simple JSON objects for schemas and input\n- Doesn't pollute your code with \"convenient\" APIs\n- Minimalist approach that will work for the majority of conventional-type objects\n- Meant to work effortlessly in both client- and server-side environments\n- Tiny (~2.2 KB gzipped), which is ~7-80x smaller than common, popular options\n- Zero dependencies\n- Has 100% test coverage\n\n## Usage\n\n### Basic importing and usage\n\n```typescript\n// ES5 format\nconst { MikroValid } = require('mikrovalid');\n// ES6 format\nimport { MikroValid } from 'mikrovalid';\n\nconst mikrovalid = new MikroValid();\n\nconst schema = {\n  properties: {\n    personal: {\n      name: {\n        type: 'string'\n      },\n      required: ['name']\n    },\n    work: {\n      office: {\n        type: 'string'\n      },\n      currency: {\n        type: 'string'\n      },\n      salary: {\n        type: 'number'\n      },\n      required: ['office']\n    },\n    required: ['personal', 'work']\n  },\n};\n\nconst input = {\n  personal: {\n    name: 'Sam Person'\n  },\n  work: {\n    office: 'London',\n    currency: 'GBP',\n    salary: 10000\n  }\n};\n\nconst generatedSchema = mikrovalid.schemaFrom(input); // \u003c-- OPTIONAL: You can also generate a schema directly from your input!\n\nconst { success, errors } = mikrovalid.test(schema, input);\n\nconsole.log('Was the test successful?', success);\n```\n\n### Warnings and silent mode\n\nBy default you will get warnings and non-critical message output. If you want to silence these message, you can instantiate MikroValid by passing `true` for the `isSilent` option, like so:\n\n```ts\nconst mikrovalid = new MikroValid(true);\n```\n\n### Errors\n\nThe `errors` object includes an aggregation of any errors, both those relating to field-level validation and for inline failures emitted when not having required keys or having excess keys.\n\nSince version `1.0.3` both error formats have the same shape:\n\n```json\n[{ \"key\": \"blip\", \"value\": 123, \"success\": false, \"error\": \"Invalid type\" }]\n```\n\n### Using schemas\n\nThe format is inspired by (but is not the same as, nor compliant with) [JSON Schema](https://json-schema.org).\n\nThe general shape it uses is:\n\n```json\n{\n  \"properties\": {\n    \"username\": {\n      \"type\": \"string\"\n    },\n    \"required\": [\"username\"]\n  }\n}\n```\n\nA valid input for this particular schema is:\n\n```json\n{\n  \"username\": \"Sam Person\"\n}\n```\n\nUsing the `schemaFrom()` method, you can easily generate schemas for your input. This is especially useful in a programmatic environment in which you can't decide or know before-hand what schema to create. Note that the generated schemas should work for the majority of cases, but you should try this functionality out before relying fully on it.\n\n#### Properties\n\n`properties` is the only **required** root-level object. Each key describes a property of the expected input. In the example, `name` is of the type `string`. Note that you never repeat the `properties` keyword—it's used only in the root.\n\n#### Allowing or disallowing additional properties\n\nBy default, unknown properties will be allowed and valid. Setting `additionalProperties` to `false` enables you to disallow any unlisted properties.\n\nSince version `1.0.10` it _only_ works in the direct scope of its location, as per below:\n\n```json\n{\n  \"properties\": {\n    \"first\": {\n      \"type\": \"string\"\n    },\n    \"second\": {\n      \"type\": \"string\"\n    },\n    \"third\": {\n      \"type\": \"string\"\n    },\n    \"additionalProperties\": false\n  }\n}\n```\n\nA payload like this...\n\n```json\n{\n  \"first\": \"the first\",\n  \"second\": \"the second\",\n  \"third\": \"the third\",\n  \"fourth\": \"the fourth\"\n}\n```\n\n...would therefore break the validation.\n\nThe same can be done with nested objects, by setting `additionalProperties` in the scope of the object:\n\n```json\n{\n  \"properties\": {\n    \"blip\": {\n      \"type\": \"string\"\n    },\n    \"inside\": {\n      \"type\": \"object\",\n      \"thing\": {\n        \"type\": \"string\"\n      },\n      \"additionalProperties\": false\n    }\n  }\n}\n```\n\nSo this would not work:\n\n```json\n{\n  \"blip\": \"beep bloop\",\n  \"inside\": {\n    \"thing\": \"scary monster\",\n    \"somethingThatIsNotAllowed\": \"...?\"\n  }\n}\n```\n\n#### Required\n\nFor each level of nesting, including within objects, a `required` key with an array of strings _may_ be used to describe properties that must exist at that location.\n\nEven the lowest-level `required` will be _within_ the `properties` key after version `1.0.10`.\n\nThis example requires both the `personal_data` object, as well as the inner `name` string:\n\n```json\n{\n  \"properties\": {\n    \"personal_data\": {\n      \"type\": \"object\",\n      \"name\": { \"type\": \"string\" },\n      \"required\": [\"name\"]\n    },\n    \"required\": [\"personal_data\"]\n  }\n}\n```\n\n#### Types\n\nThe `type` is the only **required** item-level object. Allowed types are:\n\n- `string`\n- `number`\n- `boolean`\n- `object`\n- `array`\n\nYou can require basic validation of `array` items by setting the expected type in `items.type`:\n\n```json\n{\n  \"properties\": {\n    \"books\": {\n      \"type\": \"array\",\n      \"items\": {\n        \"type\": \"object\"\n      }\n    }\n  }\n}\n```\n\nFor this schema, a valid input could for example be something like:\n\n```json\n{\n  \"books\": [{ \"author\": \"Cormac McCarthy\" }, { \"author\": \"William Blake\" }]\n}\n```\n\nNote that this will not work for mixed arrays or for any deeper inspection of object properties.\n\n#### Multiple types\n\nYou can also pass in an array of types if you want to verify that the input corresponds to at least one valid type.\n\n```json\n{\n  \"properties\": {\n    \"field\": {\n      \"type\": [\"string\", \"boolean\", \"number\"]\n    }\n  }\n}\n```\n\n#### Formats\n\nYou can use a number of special keywords to specify expectations on the input. These are:\n\n- `alphanumeric`\n- `date` (YYYY-MM-DD)\n- `email`\n- `hexColor`\n- `numeric`\n- `url`\n\nUsage is as simple as:\n\n```json\n{\n  \"properties\": {\n    \"username\": {\n      \"type\": \"string\",\n      \"format\": \"email\"\n    }\n  }\n}\n```\n\n#### Deeply nested objects\n\nThis example shows 3 levels of nesting with objects.\n\n```json\n{\n  \"properties\": {\n    \"things\": {\n      \"type\": \"object\",\n      \"required\": [\"nestedThings\"],\n      \"nestedThings\": {\n        \"type\": \"object\",\n        \"required\": [\"deeperThings\"],\n        \"deeperThings\": {\n          \"type\": \"object\",\n          \"required\": [\"something\"],\n          \"something\": \"number\"\n        }\n      }\n    },\n    \"required\": [\"things\"]\n  },\n}\n```\n\n#### Minimum length\n\n```json\n{\n  \"properties\": {\n    \"username\": {\n      \"type\": \"string\",\n      \"minLength\": 20\n    }\n  }\n}\n```\n\n#### Maximum length\n\n```json\n{\n  \"properties\": {\n    \"username\": {\n      \"type\": \"string\",\n      \"maxLength\": 2\n    }\n  }\n}\n```\n\n#### Minimum value\n\n```json\n{\n  \"properties\": {\n    \"phone\": {\n      \"type\": \"number\",\n      \"minValue\": 1000\n    }\n  }\n}\n```\n\n#### Maximum value\n\n```json\n{\n  \"properties\": {\n    \"phone\": {\n      \"type\": \"number\",\n      \"minValue\": 1000\n    }\n  }\n}\n```\n\n#### Matches regular expression pattern\n\nYou can provide your own regular expressions to match for.\n\n```json\n{\n  \"properties\": {\n    \"runtime\": {\n      \"type\": \"string\",\n      \"matchesPattern\": /^(nodejs20|python3.7)$/\n    }\n  }\n}\n```\n\n## License\n\nMIT. See `LICENSE` file.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmikaelvesavuori%2Fmikrovalid","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmikaelvesavuori%2Fmikrovalid","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmikaelvesavuori%2Fmikrovalid/lists"}