1.0.7 • Published 6 years ago

jsonschema-decorator v1.0.7

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

jsonSchema-decorator

a decorator for jsonSchema

Build Status Coverage Status

Usage

Install code:

npm install jsonschema-decorator

You need to establish a folder in your project root dir:

$/projectRootDir mkdir mock-decorators
chmod -R 777 mock-decorators

or you can use package.json to config a folder that jsonSchema will read:

// package.json
{
    "jsonSchema": {
        "path": "The schema path you want"
    }
}

Then, you can create js schema roles in dirctory mock-decorator,eg:

mock-decorator/----common/(write common rules in here)
                |
                |--schema.js(write unique rules in js files)
                |--folders/(you can also create more folders to save js files)
                   |
                   |--otherschema.js(other unique rules) 

There is and example for jsonschema-decorator:

//schema.js
'use strict';
require('./common/person');//This is a common schema

exports = module.exports = {//This is the unique schema
    "id": "/jsontest",
    "type": "object",
    "properties": {
        "test": {
            "type": "boolean",
            "required": "true"
        },
        "name": {
            "type": "string",
            "required": "true"
        },
        "params": {
            "$ref": "/jsonTestParam"
        }
    }
}

//index.js
const jd = require('jsonschema-decorator');
@jd.jsontest
class jsonTest {
    constructor(){
        this.val = {
            test:true,
            name:'xtx',
            params:{
                sex:'male',
                age:25
            }
        }
    }
}
router.get('/jsontest',async (ctx,next) => {
    ctx.set('Content-Type','application/json');
    let jsonT = new jsonTest();//new Object in here
    let json = jsonT.getVal();//get the schema value,if passed on ,it will return the mock json,else it will return a json contains the mistake
    ctx.body = jsonp(json);//use the json
});

for more example,please see the test case

LICENSE

MIT

1.0.7

6 years ago

1.0.5

7 years ago

1.0.3

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago