datatype-expansion
Advanced tools
Comparing version 0.1.1 to 0.2.0
{ | ||
"name":"datatype-expansion", | ||
"version":"0.1.1", | ||
"main":"index", | ||
"name": "datatype-expansion", | ||
"version": "0.2.0", | ||
"description": "Utility tool to expand a given RAML type and create a canonical form", | ||
"main": "src/index.js", | ||
"scripts": { | ||
"lint": "standard", | ||
"test-cov": "istanbul --include-all-sources cover --root src node_modules/mocha/bin/_mocha --report lcovonly -- -R dot --recursive --bail test/", | ||
"test-spec": "_mocha -R spec --recursive --bail test/", | ||
"test": "npm run lint && npm run test-cov", | ||
"browserify": "browserify src/index.js -o datatype-expansion.js -s datexp" | ||
}, | ||
"keywords": [ | ||
"raml", | ||
"expansion", | ||
"json", | ||
"types" | ||
], | ||
"homepage": "https://github.com/raml-org/raml-parser-toolbelt/tree/master/tools/datatype-expansion", | ||
"author": "Mulesoft", | ||
"license": "Apache-2.0", | ||
"bugs": { | ||
"url": "https://github.com/raml-org/raml-parser-toolbelt/issues" | ||
} | ||
"devDependencies": { | ||
"browserify": "^14.4.0", | ||
"chai": "^4.0.2", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^3.4.2", | ||
"standard": "^10.0.2" | ||
}, | ||
"dependencies": { | ||
"lodash": "^4.17.4" | ||
}, | ||
"files": [ | ||
"src/**", | ||
"LICENSE", | ||
"test/**" | ||
] | ||
} |
191
README.md
@@ -12,192 +12,1 @@ # RAML Data Type Expansion | ||
``` | ||
### Developer Environment | ||
To run this project and build the different packages for Java or Javascript, you need to install `leiningen`. You can use [homebrew](http://brew.sh/) for that. | ||
``` | ||
brew install leiningen | ||
``` | ||
## RAML type expanded form | ||
The RAML expanded form for a RAML type, resolves references and fills missing information to compute a fully expanded representation of the type. | ||
The form and the algorithm to compute is [documented here](doc/algorithms.md). | ||
### Usage Clojure | ||
The Clojure interface for the library offers a function to compute the expanded form using the `datatype-expansion.expanded-form/expanded-form` function. | ||
The function accepts an in-memory representation of the RAML type (keywordize) and a map from type names to types with the same representation. | ||
It returns the expanded form. | ||
The following example shows a usage example: | ||
``` clojure | ||
;; A type context with some types definitions | ||
(def types-context {"Songs.Song" {:properties {:title "string" | ||
:length "number"}} | ||
"Songs.Album" {:properties {:title "string" | ||
:songs "Songs.Song[]"}}}) | ||
(is (= (datatype-expansion.expanded-form/expanded-form (get types-context "Songs.Album") types-context) | ||
{:type "object" | ||
:properties | ||
{"title" {:type "string" :required true} | ||
"songs" {:type "array" | ||
:items {:type "object" | ||
:properties | ||
{"title" {:type "string" :required true} | ||
"length" {:type "number" :required true}} | ||
:additionalProperties true | ||
:required true} | ||
:required true}} | ||
:additionalProperties true | ||
:required true})) | ||
``` | ||
### Usage Node.js | ||
The Node.js interface for the library offers the `expandedForm` function to compute the expanded form. | ||
It accepts an in-memory JSON representation of the type, the types mapping and a callback function. | ||
If the invocation succeeds, it will return the expanded form as an argument to the provided callback function. | ||
``` javascript | ||
var tools = require("datatype-expansion"); | ||
var typesContext = {"Songs.Song": {"properties": {"title": "string", | ||
"length": "number"}}, | ||
"Songs.Album": {"properties": {"title": "string", | ||
"songs": "Songs.Song[]"}}}; | ||
tools.expandedForm(typesContext["Songs.Album"], typesContext, function(err, expanded) { | ||
// expanded contains the computed expanded form | ||
//{ | ||
// "properties": { | ||
// "title": { | ||
// "type": "string", | ||
// "required": true | ||
// }, | ||
// "songs": { | ||
// "type": "array", | ||
// "items": { | ||
// "properties": { | ||
// "title": { | ||
// "type": "string", | ||
// "required": true | ||
// }, | ||
// "length": { | ||
// "type": "number", | ||
// "required": true | ||
// } | ||
// }, | ||
// "additionalProperties": true, | ||
// "type": "object", | ||
// "required": true | ||
// }, | ||
// "required": true | ||
// } | ||
// }, | ||
// "additionalProperties": true, | ||
// "type": "object", | ||
// "required": true | ||
//} | ||
console.log(JSON.stringify(expanded,null,2)); | ||
}); | ||
``` | ||
## RAML type canonical form | ||
The canonical form computes inheritance and pushes unions to the top level of the type structure of an expanded RAML type. It is described in the [documentation section](doc/algorithms.md) of this repository. | ||
### Usage Clojure | ||
The Clojure interface to compute the canonical form is implemented in the `datatype-expansion.canonical-form/canonical-form` function. | ||
The function accepts and expanded RAML type and returns the canonical form for the same type. | ||
``` clojure | ||
;; A type context with some types definitions | ||
(def types-context {"Songs.Song" {:properties {:title "string" | ||
:length "number"}} | ||
"Songs.Album" {:properties {:title "string" | ||
:songs "Songs.Song[]"}}}) | ||
(-> types-context | ||
(get "Songs.Song") | ||
(datatype-expansion.expanded-form/expanded-form types-context) | ||
datatype-expansion.canonical-form/canonical-form) | ||
``` | ||
### Usage Node.js | ||
The Node.js version of the canonical form function is defined in the `canonicalForm` function of the library module. | ||
It accepts a JSON in-memory representation of an expanded RAML type and a callback function. It returns the canonical form or an exception: | ||
``` javascript | ||
var tools = require("datatype-expansion"); | ||
var typesContext = {"Songs.Song": {"properties": {"title": "string", | ||
"length": "number"}}, | ||
"Songs.Album": {"properties": {"title": "string", | ||
"songs": "Songs.Song[]"}}}; | ||
tools.expandedForm(typesContext["Songs.Album"], typesContext, function(err, expanded) { | ||
tools.canonicalForm(expanded, function(err, canonical) { | ||
// canonical contains the computed canonical form | ||
console.log(canonical); | ||
}); | ||
}); | ||
``` | ||
### Usage browser | ||
Require the browser build of the library, located in the `browser` directory. | ||
``` html | ||
<html> | ||
<head> | ||
<script type="text/javascript" src="./browser/index.js"></script> | ||
</head> | ||
<body> | ||
<script type='text/javascript'> | ||
var compose = function(g,f) { | ||
return (function(x) { return g(f(x)); }); | ||
}; | ||
console.log("Starting..."); | ||
var result = compose( | ||
datatype_expansion.js.canonicalForm, | ||
datatype_expansion.js.expandedForm | ||
)("string"); | ||
console.log(result); // you should get {"type": "string"} | ||
</script> | ||
</body> | ||
</html> | ||
``` | ||
## Running tests | ||
Tests for the library can be run for the clojure version using leiningen: | ||
``` shell | ||
$ lein test | ||
``` | ||
The JavaScript tests can be run with the following leiningen invocation: | ||
``` shell | ||
$ lein doo node test | ||
``` | ||
## Building the NPM package | ||
The ClojureScript code of the library can be packaged as a NPM library using the following leiningen comand: | ||
``` shell | ||
$ lein cljsbuild once default | ||
``` | ||
This command will compile the ClojureScript code into JavaScript code that can be published as a NPM package using the provided `packages.json` file. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
Found 1 instance in 1 package
1
95980
1
5
14
3180
12
2
+ Addedlodash@^4.17.4
+ Addedlodash@4.17.21(transitive)