├── index.js ├── package.json ├── filter-with-schema.js └── flatten-json-schema.js /index.js: -------------------------------------------------------------------------------- 1 | var flattenJsonSchema = require('./flatten-json-schema') 2 | var filterWithSchema = require('./filter-with-schema') 3 | 4 | module.exports = { 5 | flattenJsonSchema: flattenJsonSchema, 6 | filterWithSchema: filterWithSchema, 7 | } 8 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jsonschema-utils", 3 | "version": "2.1.5", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/poetic/jsonschema-utils.git" 12 | }, 13 | "keywords": [ 14 | "jsonschema", 15 | "utils" 16 | ], 17 | "author": "Chun Yang", 18 | "license": "MIT", 19 | "bugs": { 20 | "url": "https://github.com/poetic/jsonschema-utils/issues" 21 | }, 22 | "homepage": "https://github.com/poetic/jsonschema-utils#readme" 23 | } 24 | -------------------------------------------------------------------------------- /filter-with-schema.js: -------------------------------------------------------------------------------- 1 | function filterWithSchema (origin, jsonSchema) { 2 | if (origin == null) { 3 | return origin 4 | } 5 | 6 | var type = jsonSchema.type 7 | 8 | if (type === 'object') { 9 | var properties = jsonSchema.properties 10 | return Object.keys(origin).reduce(function(obj, key) { 11 | if (key in properties) { 12 | var newObj = {} 13 | newObj[key] = filterWithSchema(origin[key], properties[key]) 14 | Object.assign(obj, newObj) 15 | } 16 | return obj 17 | }, {}) 18 | } else if (type === 'array') { 19 | var subJsonSchema = jsonSchema.items 20 | if (!Array.isArray(origin)) { 21 | throw { 22 | name: 'not-array', 23 | message: JSON.stringify(jsonSchema) 24 | } 25 | } 26 | return origin.map(function(item) { 27 | return filterWithSchema(item, subJsonSchema) 28 | }) 29 | } else { 30 | return origin 31 | } 32 | } 33 | 34 | module.exports = filterWithSchema 35 | -------------------------------------------------------------------------------- /flatten-json-schema.js: -------------------------------------------------------------------------------- 1 | function combineProperties (properties, parentPath) { 2 | var flattenedJsonSchemas = Object 3 | .keys(properties) 4 | .map(function (propertyKey) { 5 | return flattenJsonSchema( 6 | properties[propertyKey], 7 | parentPath ? [parentPath, propertyKey].join('.'): propertyKey 8 | ); 9 | }); 10 | 11 | return Array.prototype.concat.apply( 12 | Array.prototype, 13 | flattenedJsonSchemas 14 | ); 15 | } 16 | 17 | function flattenJsonSchema(jsonSchema, parentPath) { 18 | var type = jsonSchema.type; 19 | 20 | switch (type) { 21 | case 'object': 22 | return combineProperties( 23 | jsonSchema.properties, 24 | parentPath 25 | ); 26 | 27 | case 'array': 28 | return flattenJsonSchema( 29 | jsonSchema.items, 30 | parentPath ? [parentPath, '[]'].join('.'): '[]' 31 | ); 32 | 33 | default: 34 | return [{ path: parentPath, type: type }]; 35 | } 36 | } 37 | 38 | module.exports = flattenJsonSchema 39 | --------------------------------------------------------------------------------