RAML Data Type Expansion
Often, tools need the full expansion of RAML data types where there are no references. This module gives you a utility tool to expand a given type and create a canonical form.
Installation
NPM
npm install datatype-expansion
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.
Usage
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 an optional callback function or options object.
It returns the canonical form or an exception. The following options are supported:
callback
: Provides a callback function via the options object.topLevel
(default:any
): The default RAML type to use when base type is not explicit and cannot be inferred. It can beany
orstring
, depending on whether the type comes from thebody
of a RAML service.trackOriginalType
(default:false
): Controls whether expansion should track the original type in a property calledoriginalType
when expanding a type reference.
Sync API
const tools = ; const typesContext = Song: properties: title: 'string' length: 'number' Album: properties: title: 'string' songs: 'Songs.Song[]' ;const expanded = toolsconsole;
Callback API
const tools = ; const typesContext = Song: properties: title: 'string' length: 'number' Album: properties: title: 'string' songs: 'Songs.Song[]' ;tools;
Result
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 of this repository.
Usage
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 an optional callback function or options object. It returns the canonical form or an exception. The following options are supported:
callback
: Provides a callback function via the options object.hoistUnions
(default:true
): Controls whether canonicalization should hoist unions to the top level of the type form. When enabled,union
can only appear as the top-level type, and the associated type alternatives are the permutation of any nested unions in the input type. When disabled, unions remain nested.
Sync API
const tools = ; const typesContext = SimpleUnion: properties: a: 'string' b: 'number | string' ;const expanded = toolsconst canonical = toolsconsole;
Callback API
const tools = ; const typesContext = SimpleUnion: properties: a: 'string' b: 'number | string' ;tools;
Result
Browser usage
Include it via unpkg ( if you don't use a bundler like webpack )
It gets exported as expansion
expansion
Running tests
Tests for the library can be run using:
$ npm run test