Skip to content

Commit 97511ba

Browse files
feat: add hasSchema method
1 parent 550faca commit 97511ba

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ class RefResolver {
4141
return getDataByJSONPointer(schema.schema, jsonPointer)
4242
}
4343

44+
hasSchema (schemaId) {
45+
return this.#schemas[schemaId] !== undefined
46+
}
47+
4448
getSchemaRefs (schemaId) {
4549
const schema = this.#schemas[schemaId]
4650
if (schema === undefined) {

test/has-schema.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict'
2+
3+
const assert = require('node:assert/strict')
4+
const { test } = require('node:test')
5+
const { RefResolver } = require('../index.js')
6+
7+
test('should return true if schema exists', () => {
8+
const refResolver = new RefResolver()
9+
10+
const schemaId = 'schemaId'
11+
const schema = {
12+
$id: 'schemaId',
13+
type: 'object',
14+
properties: {
15+
foo: { type: 'string' }
16+
}
17+
}
18+
refResolver.addSchema(schema)
19+
20+
const hasSchema = refResolver.hasSchema(schemaId)
21+
assert.strictEqual(hasSchema, true)
22+
})
23+
24+
test('should return false if schema does not exist', () => {
25+
const refResolver = new RefResolver()
26+
const hasSchema = refResolver.hasSchema('schemaId')
27+
assert.strictEqual(hasSchema, false)
28+
})

types/index.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ declare class RefResolver {
2727
*/
2828
getSchema(schemaId: string, jsonPointer?: string): any | null;
2929

30+
/**
31+
* Returns true if the schema by the given schema id is added to the resolver.
32+
* @param {string} schemaId - The schema id of the schema to be checked.
33+
* @returns {boolean} True if the schema by the given schema id is added to the resolver.
34+
*/
35+
hasSchema(schemaId: string): boolean;
36+
3037
/**
3138
* Returns the schema references of the schema by the given schema id.
3239
* @param {string} schemaId - The schema id of the schema whose references are to be returned.

types/index.test-d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ expectType<void>(resolver.addSchema({}, 'schemaId'))
1111
expectType<any | null>(resolver.getSchema('schemaId'))
1212
expectType<any | null>(resolver.getSchema('schemaId', 'jsonPointer'))
1313

14+
expectType<boolean>(resolver.hasSchema('schemaId'))
15+
1416
expectType<{ schemaId: string; jsonPointer: string }[]>(resolver.getSchemaRefs('schemaId'))
1517

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

0 commit comments

Comments
 (0)