Skip to content

Commit

Permalink
feat: add hasSchema method
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-tymoshenko committed Oct 20, 2023
1 parent 550faca commit 97511ba
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class RefResolver {
return getDataByJSONPointer(schema.schema, jsonPointer)
}

hasSchema (schemaId) {
return this.#schemas[schemaId] !== undefined
}

getSchemaRefs (schemaId) {
const schema = this.#schemas[schemaId]
if (schema === undefined) {
Expand Down
28 changes: 28 additions & 0 deletions test/has-schema.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict'

const assert = require('node:assert/strict')
const { test } = require('node:test')
const { RefResolver } = require('../index.js')

test('should return true if schema exists', () => {
const refResolver = new RefResolver()

const schemaId = 'schemaId'
const schema = {
$id: 'schemaId',
type: 'object',
properties: {
foo: { type: 'string' }
}
}
refResolver.addSchema(schema)

const hasSchema = refResolver.hasSchema(schemaId)
assert.strictEqual(hasSchema, true)
})

test('should return false if schema does not exist', () => {
const refResolver = new RefResolver()
const hasSchema = refResolver.hasSchema('schemaId')
assert.strictEqual(hasSchema, false)
})
7 changes: 7 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ declare class RefResolver {
*/
getSchema(schemaId: string, jsonPointer?: string): any | null;

/**
* Returns true if the schema by the given schema id is added to the resolver.
* @param {string} schemaId - The schema id of the schema to be checked.
* @returns {boolean} True if the schema by the given schema id is added to the resolver.
*/
hasSchema(schemaId: string): boolean;

/**
* Returns the schema references of the schema by the given schema id.
* @param {string} schemaId - The schema id of the schema whose references are to be returned.
Expand Down
2 changes: 2 additions & 0 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ expectType<void>(resolver.addSchema({}, 'schemaId'))
expectType<any | null>(resolver.getSchema('schemaId'))
expectType<any | null>(resolver.getSchema('schemaId', 'jsonPointer'))

expectType<boolean>(resolver.hasSchema('schemaId'))

expectType<{ schemaId: string; jsonPointer: string }[]>(resolver.getSchemaRefs('schemaId'))

expectType<{ [key: string]: any }>(resolver.getSchemaDependencies('schemaId'))
Expand Down

0 comments on commit 97511ba

Please sign in to comment.