@axway/api-builder-schema
v5.0.0
Published
A singleton module for loading custom schemas and resolving them to json-schema docs
Downloads
416
Readme
@axway/api-builder-schema
A singleton module for loading custom schemas and resolving them to json-schema docs
API Reference
Example (Quick start)
const schemas = require('axway-schema');
schemas.add('./schemas/foo.json')
.add('./schemas/bar.json', 'schema://myservice/bar')
.add('./schemas/swagger-v2.0.json')
.loadSync();
schemas.get('schema:///foo');
schemas.validate('schema:///foo', foo);
schemas.get('schema:///foo/myservice/bar');
schemas.validate('schema:///foo/myservice/bar', bar);
schemas.get('http://swagger.io/v2/schema.json#');
schemas.validate('http://swagger.io/v2/schema.json#', swagger);
schemas.dereference(swagger);
schemas.dereference(swagger, { target: '#/definitions' });
schemas.dereference(swagger, { target: '#/definitions', rename: (id) => { return 'foo'; } });
axway-schema~AxwaySchema
A schema manager.
Kind: inner class of axway-schema
Access: public
- ~AxwaySchema
- new AxwaySchema()
- .setLogger(logger)
- .add(item, [defaultId]) ⇒ AxwaySchema
- .register(schema) ⇒ AxwaySchema
- .loadSync() ⇒ AxwaySchema
- .clear() ⇒ AxwaySchema
- .exists(id) ⇒ boolean
- .get([id]) ⇒ object
- .validate(id, obj) ⇒ string
- ._updateRef(doc, ref, refpath, encodedRef)
- .dereference(doc, [options]) ⇒ object
- .getErrorString(errors, objectType) ⇒ string
new AxwaySchema()
Constructs a schema manager. The instance is a singleton.
axwaySchema.setLogger(logger)
Sets the Logger
Kind: instance method of AxwaySchema
Access: public
| Param | Type | Description | | --- | --- | --- | | logger | Logger | A logger. |
axwaySchema.add(item, [defaultId]) ⇒ AxwaySchema
Adds an item to be loaded later by calling
loadSync.
The item
can be a string
, in which case it is a schema file, and an optional defaultId
will be used for the id
if the file does not have an id
. If the file does not have an
id
and no defaultId
is supplied, the function will throw.
If the item
is an object, it is a valid schema that has an id
defined so that
it can be referenced later. In this case, defaultId
is ignored.
Kind: instance method of AxwaySchema
Returns: AxwaySchema - The AxwaySchema object.
Access: public
| Param | Type | Description | | --- | --- | --- | | item | string | object | The item to add (a filename or schema). | | [defaultId] | string | The item id if one not supplied. |
Example (Add a schema)
const schemas = new AxwaySchema();
schemas.add('./schema1.json');
schemas.add('./schema2.json', 'schema://service/myapp/schema2.json');
schemas.add({id: 'http://axway.com/schemas/user.json'});
axwaySchema.register(schema) ⇒ AxwaySchema
Registers a global schema that will persist across calls to
clear operations. Schemas registered with
the same id
will generate a warning but will be ignored.
Kind: instance method of AxwaySchema
Returns: AxwaySchema - The AxwaySchema object.
| Param | Type | Description | | --- | --- | --- | | schema | object | The schema. |
axwaySchema.loadSync() ⇒ AxwaySchema
Loads all schemas previously added with calls to add.
Kind: instance method of AxwaySchema
Returns: AxwaySchema - The AxwaySchema object.
Access: public
Example (Load schemas)
const schemas = new AxwaySchema();
schemas
.add('./schema1.json');
.add('./schema2.json', 'service');
.loadSync());
axwaySchema.clear() ⇒ AxwaySchema
Clears all schemas and resets state. All registered schema will be automatically re-added on next load.
Kind: instance method of AxwaySchema
Returns: AxwaySchema - The AxwaySchema object.
Access: public
axwaySchema.exists(id) ⇒ boolean
Tests whether or not a schema is loaded, identified by id
.
Kind: instance method of AxwaySchema
Returns: boolean - True if the schema exists.
Access: public
| Param | Type | Description | | --- | --- | --- | | id | string | The schema identifier to test. |
axwaySchema.get([id]) ⇒ object
Gets a schema by id
. If id
is not supplied, all schemas are returned.
Kind: instance method of AxwaySchema
Returns: object - Returns a schema identified by id
.
Access: public
| Param | Type | Description | | --- | --- | --- | | [id] | string | The schema identifier. |
axwaySchema.validate(id, obj) ⇒ string
Validates an object obj
against a schema previously added with
add or
register
and identified by id
, or if id
is a JSON schema object, obj
is validated
against that.
Kind: instance method of AxwaySchema
Returns: string - Returns an error string when not valid, undefined otherwise.
Access: public
| Param | Type | Description | | --- | --- | --- | | id | string | object | The schema identifier or object. | | obj | * | The object to validate. |
axwaySchema._updateRef(doc, ref, refpath, encodedRef)
Updates the reference with its encoded equivalent.
Kind: instance method of AxwaySchema
| Param | Type | Description | | --- | --- | --- | | doc | Object | the doc to be updated | | ref | Object | reference details | | refpath | string | the path to be replaced | | encodedRef | string | the new path |
axwaySchema.dereference(doc, [options]) ⇒ object
Dereferences non-local references in the supplied document. The supplied
document is not modified. All non-local references (e.g schema://
) will
be replaced with the corresponding loaded schema. If schema are not found,
an error will be thrown.
If options.target
is not supplied, then references are expanded inline. If
options.target
is supplied, then references are expanded in the location specified
by options.target
and any existing reference updated to point to the new target.
For example, if the options.target
is #/definitions
, then the schema will be
expanded to $.definitions
with the name of the schema the trailing component
(basename) of its id
, or if options.rename
is supplied, the name
returned by that.
Kind: instance method of AxwaySchema
Returns: object - Returns a document with schema://
references expanded.
Access: public
| Param | Type | Description | | --- | --- | --- | | doc | object | A document | | [options] | object | Options | | [options.target] | string | A target | | [options.rename] | targetRenameCallback | A callback to rename schema |
axwaySchema.getErrorString(errors, objectType) ⇒ string
The ajv.errorsText is actually not very good.
Kind: instance method of AxwaySchema
Returns: string - A formatted error string.
Access: public
| Param | Type | Description | | --- | --- | --- | | errors | array | ajv errors object | | objectType | string | The name of the object type that was validated |
axway-schema~targetRenameCallback ⇒ string
Callback function to rename a target when used as a
dereference target.
For example, if your id is "schema://svc.foo/bar", then bar
will be written
to the dereference target as bar
. If you want to rename bar
, then use
this function.
Kind: inner typedef of axway-schema
Returns: string - Return the renamed target.
Access: public
| Param | Type | Description | | --- | --- | --- | | id | id | The schema id. |
Building
npm run build
Author
Axway [email protected] https://axway.com
License
This code is proprietary, closed source software licensed to you by Axway. All Rights Reserved. You may not modify Axway’s code without express written permission of Axway. You are licensed to use and distribute your services developed with the use of this software and dependencies, including distributing reasonable and appropriate portions of the Axway code and dependencies. Except as set forth above, this code MUST not be copied or otherwise redistributed without express written permission of Axway. This module is licensed as part of the Axway Platform and governed under the terms of the Axway license agreement (General Conditions) located here: https://support.axway.com/en/auth/general-conditions; EXCEPT THAT IF YOU RECEIVED A FREE SUBSCRIPTION, LICENSE, OR SUPPORT SUBSCRIPTION FOR THIS CODE, NOTWITHSTANDING THE LANGUAGE OF THE GENERAL CONDITIONS, AXWAY HEREBY DISCLAIMS ALL SUPPORT AND MAINTENANCE OBLIGATIONS, AS WELL AS ALL EXPRESS AND IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO IMPLIED INFRINGEMENT WARRANTIES, WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, AND YOU ACCEPT THE PRODUCT AS-IS AND WITH ALL FAULTS, SOLELY AT YOUR OWN RISK. Your right to use this software is strictly limited to the term (if any) of the license or subscription originally granted to you.