-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[PoC] feat(validator): add in a JSON Schema validator decorator #1294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| package-lock=false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| Copyright (c) IBM Corp. 2018. All Rights Reserved. | ||
| Node module: @loopback/validator | ||
| This project is licensed under the MIT License, full text below. | ||
|
|
||
| -------- | ||
|
|
||
| MIT license | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in | ||
| all copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # @loopback/validator | ||
|
|
||
|
|
||
| ## Overview | ||
|
|
||
|
|
||
|
|
||
|
|
||
| ## Installation | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| ## Basic use | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| ## Contributions | ||
|
|
||
| - [Guidelines](https://github.com/strongloop/loopback-next/blob/master/docs/CONTRIBUTING.md) | ||
| - [Join the team](https://github.com/strongloop/loopback-next/issues/110) | ||
|
|
||
| ## Tests | ||
|
|
||
| Run `npm test` from the root folder. | ||
|
|
||
| ## Contributors | ||
|
|
||
| See | ||
| [all contributors](https://github.com/strongloop/loopback-next/graphs/contributors). | ||
|
|
||
| ## License | ||
|
|
||
| MIT |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "content": [ | ||
| "index.ts", | ||
| "src/index.ts", | ||
| "src/build-schema.ts" | ||
| ], | ||
| "codeSectionDepth": 4 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| // Copyright IBM Corp. 2018. All Rights Reserved. | ||
| // Node module: @loopback/validator | ||
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| export * from './dist'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| // Copyright IBM Corp. 2018. All Rights Reserved. | ||
| // Node module: @loopback/validator | ||
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| module.exports = require('./dist'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| // Copyright IBM Corp. 2018. All Rights Reserved. | ||
| // Node module: @loopback/validator | ||
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| // DO NOT EDIT THIS FILE | ||
| // Add any additional (re)exports to src/index.ts instead. | ||
| export * from './src'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| { | ||
| "name": "@loopback/validator", | ||
| "version": "0.1.0", | ||
| "description": "", | ||
| "engines": { | ||
| "node": ">=8" | ||
| }, | ||
| "scripts": { | ||
| "build": "lb-tsc es2017", | ||
| "build:apidocs": "lb-apidocs", | ||
| "clean": "lb-clean loopback-validator*.tgz dist package api-docs", | ||
| "pretest": "npm run build", | ||
| "test": "lb-mocha \"DIST/test/unit/**/*.js\" \"DIST/test/integration/**/*.js\" \"DIST/test/acceptance/**/*.js\"", | ||
| "verify": "npm pack && tar xf validator*.tgz && tree package && npm run clean" | ||
| }, | ||
| "author": "IBM", | ||
| "license": "MIT", | ||
| "keywords": [ | ||
| "LoopBack", | ||
| "TypeScript", | ||
| "JSON Schema" | ||
| ], | ||
| "publishConfig": { | ||
| "access": "public" | ||
| }, | ||
| "dependencies": { | ||
| "@loopback/metadata": "^0.6.0", | ||
| "@types/http-errors": "^1.6.1", | ||
| "@types/json-schema": "^6.0.1", | ||
| "ajv": "^6.4.0", | ||
| "debug": "^3.1.0", | ||
| "http-errors": "^1.6.3" | ||
| }, | ||
| "devDependencies": { | ||
| "@loopback/build": "^0.6.0", | ||
| "@loopback/repository": "^0.8.0", | ||
| "@loopback/repository-json-schema": "^0.6.0", | ||
| "@loopback/rest": "^0.8.0", | ||
| "@loopback/testlab": "^0.8.0", | ||
| "@types/debug": "0.0.30", | ||
| "@types/node": "^8.10.4" | ||
| }, | ||
| "files": [ | ||
| "README.md", | ||
| "index.js", | ||
| "index.d.ts", | ||
| "dist/src", | ||
| "src" | ||
| ], | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/strongloop/loopback-next.git" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| // Copyright IBM Corp. 2018. All Rights Reserved. | ||
| // Node module: @loopback/validator | ||
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| export * from './validate.decorator'; | ||
|
|
||
| import * as HttpErrors from 'http-errors'; | ||
| export {HttpErrors}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| // Copyright IBM Corp. 2018. All Rights Reserved. | ||
| // Node module: @loopback/validator | ||
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| import {JSONSchema6} from 'json-schema'; | ||
| import * as AJV from 'ajv'; | ||
| import { | ||
| MetadataAccessor, | ||
| MetadataInspector, | ||
| ParameterDecoratorFactory, | ||
| } from '@loopback/metadata'; | ||
| import * as HttpErrors from 'http-errors'; | ||
|
|
||
| const debug = require('debug')('loopback:validator'); | ||
|
|
||
| // tslint:disable-next-line:no-any | ||
| export type Validator = (arg: any) => boolean | Promise<boolean>; | ||
|
|
||
| export const VALIDATION_KEY = MetadataAccessor.create<JSONSchema6 | Validator>( | ||
| 'validation.parameter', | ||
| ); | ||
|
|
||
| export function validatable() { | ||
| return function( | ||
| target: Object, | ||
| member: string, | ||
| // tslint:disable-next-line:no-any | ||
| descriptor: TypedPropertyDescriptor<(...args: any[]) => any>, | ||
| ) { | ||
| const originalMethod = descriptor.value; | ||
| if (!originalMethod) { | ||
| throw new Error( | ||
| 'No method found for some reason, this should not happen', | ||
| ); | ||
| } | ||
| // tslint:disable-next-line:no-any | ||
| descriptor.value = async function(...args: any[]) { | ||
| const ajv = new AJV(); | ||
| const schemas = MetadataInspector.getAllParameterMetadata( | ||
| VALIDATION_KEY, | ||
| target, | ||
| member, | ||
| )!; | ||
| for (let i = 0; i < args.length; i++) { | ||
| const schema = schemas[i]; | ||
| if (typeof schema === 'object') { | ||
| // is a JSON Schema | ||
| debug('validating %s against %o', args[i], schema); | ||
| const isValid = ajv.validate(schema, args[i]); | ||
| if (!isValid) { | ||
| throw new HttpErrors.UnprocessableEntity( | ||
| ajv.errorsText(ajv.errors, {dataVar: args[i]}), | ||
| ); | ||
| } | ||
| } else if (typeof schema === 'function') { | ||
| // is a validator function | ||
| debug('validating %s against %o', args[i], schema); | ||
| const isValid = await schema(args[i]); | ||
| if (!isValid) { | ||
| throw new HttpErrors.UnprocessableEntity( | ||
| `${args[i]} is not a valid argument`, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| // tslint:disable-next-line:no-invalid-this | ||
| return originalMethod.apply(this, args); | ||
| }; | ||
| // hacky way of bypassing rest parameter length issue; | ||
| // see DecoratorFactory.getNumberOfParameters in @loopback/context | ||
| Object.defineProperty(descriptor.value, 'length', { | ||
| value: originalMethod.length, | ||
| }); | ||
| return descriptor; | ||
| }; | ||
| } | ||
|
|
||
| export function validate(schemaOrValidator: JSONSchema6 | Validator) { | ||
| return ParameterDecoratorFactory.createDecorator( | ||
| VALIDATION_KEY, | ||
| schemaOrValidator, | ||
| ); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Creating a method proxy via
@validatableprobably has its constraints - for example, it won't have access to theContextfor some of the validators to be instantiated and executed. Only isolated validations can be performed by this fashion.