Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
json-schema-resolver
Advanced tools
Readme
Resolve all $refs
in your JSON schema!
This module will resolve the $ref
keyword against the externalSchemas
you will provide.
By resolving the $ref
keyword, means that you get back a single BIG inlined JSON schema that does not rely on any external schema.
If a reference is missing, it will not throw any error.
npm install json-schema-resolver
This plugin support Node.js >= 10
The $ref
string is going to be modified to point to a local reference URI: #/definitions/<generated key>
.
Moreover, the definitions
keyword will be decorated with the external schemas to get only one JSON schema resolved as output.
By default the <generated key>
has the def-${index}
format.
You can customize it by passing a buildLocalReference
function as follows:
const RefResolver = require('json-schema-resolver')
const ref = RefResolver({
clone: true, // Clone the input schema without changing it. Default: false,
buildLocalReference (json, baseUri, fragment, i) {
// the `json` that is being resolved
// the `baseUri` object of the schema. Its values is the parse result from https://www.npmjs.com/package/uri-js
// the `fragment` is the `$ref` string when the `$ref` is a relative reference
// the `i` is a local counter to generate a unique key
return `def-${i}` // default value
}
})
const inputSchema = {
$id: 'http://example.com/SimplePerson',
type: 'object',
properties: {
name: { type: 'string' },
address: { $ref: 'relativeAddress#' },
houses: { type: 'array', items: { $ref: 'relativeAddress#' } }
}
}
const addresSchema = {
$id: 'relativeAddress', // Note: prefer always absolute URI like: http://mysite.com
type: 'object',
properties: {
zip: { type: 'string' },
city: { type: 'string' }
}
}
const singleSchema = ref.resolve(inputSchema, { externalSchemas: [addresSchema] })
// inputSchema is untouched thanks to clone:true
singleSchema
will be like:
{
"$id": "http://example.com/SimplePerson",
"type": "object",
"properties": {
"name": {
"type": "string"
},
"address": {
"$ref": "#/definitions/def-0"
},
"houses": {
"type": "array",
"items": {
"$ref": "#/definitions/def-0"
}
}
},
"definitions": {
"def-0": {
"$id": "relativeAddress",
"type": "object",
"properties": {
"zip": {
"type": "string"
},
"city": {
"type": "string"
}
}
}
}
}
When you have multiple schemas to resolve against a collection of shared schema you need to use this module with little changes.
This is needed to have all the same definitions path (#/definitions/<generated key>
) across all the
root schemas
const ref = RefResolver({
clone: true, // Clone the input schema without changing it. Default: false
applicationUri: 'my-application.org', // You need to provide an unique URI to resolve relative `$id`s
externalSchemas: [addresSchema] // The schemas provided at the creation of the resolver, will be used evvery time `.resolve` will be called
})
const inputSchema = {
$id: 'http://example.com/SimplePerson',
type: 'object',
properties: {
name: { type: 'string' },
address: { $ref: 'relativeAddress#' },
houses: { type: 'array', items: { $ref: 'relativeAddress#' } }
}
}
// the resolved schema DOES NOT have definitions added
const singleSchema = ref.resolve(inputSchema)
const anotherResolvedSchema = ref.resolve(input_2_Schema) // resolve schemas within the same externalSchemas
// to get the definition you need only to call:
const sharedDefinitions = ref.definitions()
To debug this module, simply set:
export DEBUG=json-schema-resolver
Licensed under MIT.
FAQs
Resolve all your $refs
The npm package json-schema-resolver receives a total of 372,099 weekly downloads. As such, json-schema-resolver popularity was classified as popular.
We found that json-schema-resolver demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.