{"id":20913809,"url":"https://github.com/vikcch/layer-one-validator","last_synced_at":"2026-02-13T10:03:17.761Z","repository":{"id":64181537,"uuid":"572029001","full_name":"vikcch/layer-one-validator","owner":"vikcch","description":"layer-one-validator is a set of express.js middlewares callback functions to validate data form requests.","archived":false,"fork":false,"pushed_at":"2025-02-19T21:34:48.000Z","size":77,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-29T12:23:44.391Z","etag":null,"topics":["business","database","express","expressjs","middleware","server-side","validation","validator"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/layer-one-validator","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/vikcch.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}},"created_at":"2022-11-29T12:06:55.000Z","updated_at":"2025-02-19T21:34:52.000Z","dependencies_parsed_at":"2024-02-02T23:25:58.088Z","dependency_job_id":null,"html_url":"https://github.com/vikcch/layer-one-validator","commit_stats":{"total_commits":4,"total_committers":2,"mean_commits":2.0,"dds":0.25,"last_synced_commit":"b21b6e298b5b2ac87e0d74b33b217941f0683c95"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vikcch%2Flayer-one-validator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vikcch%2Flayer-one-validator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vikcch%2Flayer-one-validator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vikcch%2Flayer-one-validator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vikcch","download_url":"https://codeload.github.com/vikcch/layer-one-validator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249785767,"owners_count":21325539,"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":["business","database","express","expressjs","middleware","server-side","validation","validator"],"created_at":"2024-11-18T15:08:03.654Z","updated_at":"2026-02-13T10:03:12.730Z","avatar_url":"https://github.com/vikcch.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# layer-one-validator\n\nSome _expressjs middleware_ callback functions to validate data from requests, checks the amount of properties, their types and the business logic.\n\n## Installation and Usage\n\n### Server-side usage\n\nInstall the library with: \n\n`npm install layer-one-validator`\n\nThe _validator_ should be invoked using the `bind()` method. The first and only _argument_ can be either an `object` or an `array`.\n\n\u003e NOTE:: If the value being tested is an _array_, the `type:function` must be evaluated for each element within that array. In the case of the `biz:function`, testing can be performed on the entire array or on each individual element.\n\n### Syntax\n\n`layerOneValidator.body.bind(object)`\n\n`layerOneValidator.body.bind(objectsArray)`\n\n### Object\n\n`{ prop:string [, type:function][, biz:function][, optional:boolean] }`\n\n`{ prop:string [, type:function][, biz:function][, optional:boolean] }[]`\n\n* prop \u003c_string_\u003e - The name of the property named on the request.\n\n* type \u003c_function_\u003e - Optional, a function to test the type of the property. Should return a `boolean`.\n\n* biz \u003c_function_\u003e - Optional, a function to test the business part of the property.\nShould return a `boolean`.\n\n* optional \u003c_boolean_\u003e - Optional, default: `false`. When `optional` is set to `true` and the property exists on the incoming request, it must pass validation if a `type` or `biz` rule is defined.\n\n### Body, Query or Params\n\nThe _layer-one-validator_ is an object with 3 properties, `'body'`,`'params'` and `'query'`, representing each _express request_.\n\n\u003e NOTE:: The `Content-Type` must be set to `application/json` when the data is sent in the body of the request.\n\n### Responses\n\nIf validation fails, the response will be a JSON object containing:\n\n* `{ success: false }` - Will throw an error on the server with additional information.\n\n* `{ /* ... */, message }` - Indicates the stage at which the failures occurred.\n\n* `{ /* ... */, fail }` - Displays the name of the failing property.\n\n* `{ /* ... */, layer }` - Displays `'body'`,`'params'` or `'query'`.\n\n* `{ /* ... */, source }` - Displays the source as `layer-one-validator`.\n\nIf successful, will go to the next _middleware_.\n\n### HTTP response status codes\n\nIf the bound _object_ or _objectsArray_ is incorrect, or if the `type` property function checks the entire array instead of individual items:\n\n* `500 Internal Server Error` \n\nWhen issues are found with prop, type, or there are discrepancies in the number of properties:\n\n* `400 Bad Request`\n\nWhen fails on _biz_:\n\n* `422 Unprocessable Content`\n\n### Route only\n\n```js\n// route\nconst layerOneValidator = require('layer-one-validator');\n\nrouter.post('/user',\n\n    layerOneValidator.body.bind([\n        { prop: 'weight', type: v =\u003e Number.isInteger(v), biz: v =\u003e v \u003e 0 },\n        { prop: 'username', biz: v =\u003e /^[a-z]{4,8}$/.test(v) }\n    ]),\n\n    (request, response, next) =\u003e {\n        /* ... */\n    }\n);\n\nmodule.exports = router;\n```\n\n### Route \u0026 Helpers \u0026 Controller\n\n```js\n// route\nconst userController = require(/* ... */);\n\nrouter.post('/user',\n    userController.user.validation.layerOne,\n    userController.user.execute\n);\n```\n```js\n// helpers - fns.js\nmodule.exports = {\n    isNumber: value =\u003e typeof value === 'number',\n    isString: value =\u003e typeof value === 'string'\n};\n\n// helpers - biz.js\nmodule.exports = {\n    isWeight: value =\u003e Number.isInteger(value) \u0026\u0026 value \u003e 0,\n    isUsername: value =\u003e /^[a-z]{4,8}$/.test(value)\n};\n```\n```js\n// controller\nconst layerOneValidator = require('layer-one-validator');\nconst fns = require('./path_to_helpers/fns');\nconst biz = require('./path_to_helpers/biz');\n\nmodule.exports = {\n\n    // * POST\n    user: {\n\n        validation: {\n            layerOne: layerOneValidator.body.bind([\n                { prop: 'weight', type: fns.isNumber, biz: biz.isWeight },\n                { prop: 'username', type: fns.isString, biz: biz.isUsername }\n            ])\n        },\n\n        execute(request, response, next) {\n            /* ... */\n        }\n    }\n};\n```\n\n## Tests\n\nTests are using _mocha_, to run the tests use:\n\n`$ npm test`\n\nConducts tests without displaying _layer-one-validator_ helper errors in the console.\n\n`$ npm run test-no-print-error`\n\n## License\n\nLicensed under the [MIT](./LICENSE)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvikcch%2Flayer-one-validator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvikcch%2Flayer-one-validator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvikcch%2Flayer-one-validator/lists"}