{"id":16326036,"url":"https://github.com/boizz/json-safeguard","last_synced_at":"2026-04-15T09:31:42.900Z","repository":{"id":143933398,"uuid":"121346760","full_name":"boizz/json-safeguard","owner":"boizz","description":"A lib that effectively safeguards data types, and automatic completion of the required words.","archived":false,"fork":false,"pushed_at":"2018-04-15T10:46:11.000Z","size":170,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-03-03T20:58:00.216Z","etag":null,"topics":["json","json-api","json-schema"],"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/boizz.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-02-13T06:22:22.000Z","updated_at":"2018-04-15T10:46:12.000Z","dependencies_parsed_at":null,"dependency_job_id":"e2c2161a-efcd-4dcc-ab3f-feceb6b6ba5f","html_url":"https://github.com/boizz/json-safeguard","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/boizz/json-safeguard","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boizz%2Fjson-safeguard","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boizz%2Fjson-safeguard/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boizz%2Fjson-safeguard/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boizz%2Fjson-safeguard/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/boizz","download_url":"https://codeload.github.com/boizz/json-safeguard/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/boizz%2Fjson-safeguard/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31834523,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-15T07:17:56.427Z","status":"ssl_error","status_checked_at":"2026-04-15T07:17:30.007Z","response_time":63,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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","json-api","json-schema"],"created_at":"2024-10-10T23:07:16.891Z","updated_at":"2026-04-15T09:31:42.891Z","avatar_url":"https://github.com/boizz.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# json-safeguard\n\n[![Build Status](https://travis-ci.org/BoizZ/json-safeguard.svg?branch=master)](https://travis-ci.org/BoizZ/json-safeguard)\n[![Coverage Status](https://coveralls.io/repos/github/BoizZ/json-safeguard/badge.svg)](https://coveralls.io/github/BoizZ/json-safeguard)\n[![NPM Package](https://img.shields.io/npm/v/json-safeguard.svg?style=flat-square)](https://www.npmjs.org/package/json-safeguard)\n[![NPM Downloads](https://img.shields.io/npm/dm/json-safeguard.svg?style=flat-square)](https://npmjs.org/package/json-safeguard)\n[![Dependency Status](https://david-dm.org/BoizZ/json-safeguard.svg?style=flat-square)](https://david-dm.org/BoizZ/json-safeguard)\n[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2FBoizZ%2Fjson-safeguard.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2FBoizZ%2Fjson-safeguard?ref=badge_shield)\n\nA lib that effectively safeguards data types, and automatic completion of the required words.\n\n## Quick Start\n\n```bash\n$ npm i --save json-safeguard\n```\n\n```js\nconst safeguard = require('json-safeguard')\n\nconst schema = {\n  name: 'string'\n}\nconst data = {\n  name: 'Heleth'\n}\n\nsafeguard(schema, data) /** =\u003e\n  {\n    state: true,\n    message: 'Verification passed',\n    complated_data: {\n      name: 'Heleth'\n    }\n  }\n*/\n```\n\n## More complicated verification\n\n```js\nconst schema = {\n  name: 'string*',\n  age: 'number',\n  excellent: (value, data) =\u003e {\n    const subjects = data.subjects || []\n    const isExcellent = subjects.every(item =\u003e item.achievement \u003e= 90)\n    return isExcellent === value ? true : {\n      state: false,\n      messages: [\n        `${data.name} is ${isExcellent ? '' : 'n\\'t'} excellent`\n      ]\n    }\n  },\n  subjects: [\n    {\n      name: 'string*',\n      achievement: 'number*'\n    }\n  ],\n  hobby: 'string[]*'\n}\n\nconst data = {\n  name: 'Heleth',\n  age: 23,\n  excellent: false,\n  subjects: [\n    {\n      name: 'Javascript',\n      achievement: 95.5\n    },\n    {\n      name: 'Python',\n      achievement: 90\n    },\n    {\n      name: 'Swift',\n      achievement: 93\n    }\n  ]\n}\n\nsafeguard(schema, data) /** =\u003e\n  {\n    state: false,\n    message: 'Heleth is excellent. | hobby: Expected a value is required.',\n    completed_data: {\n      name: 'Heleth',\n      age: 23,\n      excellent: false,\n      subjects: [\n        {\n          name: 'Javascript',\n          achievement: 95.5\n        },\n        {\n          name: 'Python',\n          achievement: 90\n        },\n        {\n          name: 'Swift',\n          achievement: 93\n        }\n      ]\n    },\n    hobby: []\n  }\n*/\n\n```\n\n## Schema\n\n### Basic Types\n\nDeclare the type of field in the Schema to verify the data.\n\n``` js\nconst schema = {\n  _string: 'string'\n}\n```\n\n| Types | Value | Description |\n| :-: | :-: | :-: |\n| Boolean | `boolean` | True or false. |\n| Number | `number` | Integer or float. |\n| String | `string` |  |\n| Array | `[]` | Array of any. |\n| Boolean-Array | `boolean[]` | Array of boolean only. |\n| Number-Array | `number[]` | Array of number only. |\n| String-Array | `string[]` | Array of string only. |\n\n### Required and Automatic Completion\n\nYou can specify some required fields with `*`, such as `number[]*`. When the field is empty, it will automatically completion.\n\n| Types | Value | Description |\n| :-: | :-: | :-: |\n| Required | `*` | Check if this field exists. |\n\nFor example:\n\n``` js\nconst safeguard = require('json-safeguard')\n\nconst schema = {\n  _string_array: 'string[]*'\n}\nconst data = {}\n\nsafeguard(schema, data) /** =\u003e\n  {\n    state: false,\n    message: '_string_array: Expected a value is required.',\n    completed_data: {\n      _string_array: []\n    }\n  }\n*/\n```\n\n### Custom Verify\n\n| Types | Value | Description |\n| :-: | :-: | :-: |\n| Custom Verify | (value, data) =\u003e any | Use a custom function to check if this field meets the requirements. Return a boolean to tell the result of the check directly. You can also return an object and set the custom message and default value, please read below for details. |\n\n``` js\nconst safeguard = require('json-safeguard')\n\nconst schema = {\n  _number: (value, data) =\u003e value \u003c 20\n}\nconst data = {\n  _number: 15\n}\n\nsafeguard(schema, data) /** =\u003e\n  {\n    state: true,\n    message: '',\n    completed_data: {\n      _string_array: 15\n    }\n  }\n*/\n```\n\n## Result\n\n| Property | Description | Type | Default |\n| :-: | :-: | :-: | :-: |\n| state | Verification state | boolean | - |\n| message | Error message | string | '' |\n| completed_data | Safe data containing required arrays and objects | object | {} |\n\n## License\n\nMIT\n\n\n[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2FBoizZ%2Fjson-safeguard.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2FBoizZ%2Fjson-safeguard?ref=badge_large)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fboizz%2Fjson-safeguard","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fboizz%2Fjson-safeguard","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fboizz%2Fjson-safeguard/lists"}