axway-schema v4.3.1
axway-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://myproject/bar')
.add('./schemas/swagger-v2.0.json')
.loadSync();
schemas.get('schema:///foo');
schemas.validate('schema:///foo', foo);
schemas.get('schema:///foo/myproject/bar');
schemas.validate('schema:///foo/myproject/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
- .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://project/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', 'project');
.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 identified by id
.
Kind: instance method of AxwaySchema
Returns: string - Returns an error string when not valid, undefined otherwise.
Access: public
Param | Type | Description |
---|---|---|
id | string | The schema identifier. |
obj | object | The object to validate. |
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 support@axway.com https://axway.com
License
This code is closed source and Confidential and Proprietary to Axway, Inc. All Rights Reserved. This code MUST not be modified, copied or otherwise redistributed without express written permission of Axway. This file is licensed as part of the Axway Platform and governed under the terms of the Axway license agreement. Your right to use this software terminates when you terminate your Axway subscription.
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago