Skip to content

Commit 2e19fec

Browse files
committed
Make explicitly loaded dialects persistent
1 parent 84ce853 commit 2e19fec

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

lib/keywords.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export const defineVocabulary = (id, keywords) => {
3636

3737
const _dialects = {};
3838
const _allowUnknownKeywords = {};
39+
const _persistentDialects = {};
3940

4041
export const getKeywordId = (keyword, dialectId) => getDialect(dialectId)?.[keyword]
4142
|| (_allowUnknownKeywords[dialectId] || keyword.startsWith("x-"))
@@ -60,8 +61,9 @@ const getDialect = (dialectId) => {
6061

6162
export const hasDialect = (dialectId) => dialectId in _dialects;
6263

63-
export const loadDialect = (dialectId, dialect, allowUnknownKeywords = false) => {
64+
export const loadDialect = (dialectId, dialect, allowUnknownKeywords = false, isPersistent = true) => {
6465
_allowUnknownKeywords[dialectId] = allowUnknownKeywords;
66+
_persistentDialects[dialectId] = _persistentDialects[dialectId] || isPersistent;
6567

6668
_dialects[dialectId] = {};
6769
Object.entries(dialect)
@@ -77,12 +79,16 @@ export const loadDialect = (dialectId, dialect, allowUnknownKeywords = false) =>
7779
});
7880
} else if (!allowUnknownKeywords || isRequired) {
7981
delete _dialects[dialectId];
82+
delete _allowUnknownKeywords[dialectId];
83+
delete _persistentDialects[dialectId];
8084
throw Error(`Unrecognized vocabulary: ${vocabularyId}. You can define this vocabulary with the 'defineVocabulary' function.`);
8185
}
8286
});
8387
};
8488

8589
export const unloadDialect = (dialectId) => {
86-
delete _allowUnknownKeywords[dialectId];
87-
delete _dialects[dialectId];
90+
if (!_persistentDialects[dialectId]) {
91+
delete _allowUnknownKeywords[dialectId];
92+
delete _dialects[dialectId];
93+
}
8894
};

lib/schema.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export const buildSchemaDocument = (schema, id, dialectId, embedded = {}) => {
9393
const allowUnknownKeywords = schema[vocabularyToken]["https://json-schema.org/draft/2019-09/vocab/core"]
9494
|| schema[vocabularyToken]["https://json-schema.org/draft/2020-12/vocab/core"];
9595

96-
loadDialect(id, schema[vocabularyToken], allowUnknownKeywords);
96+
loadDialect(id, schema[vocabularyToken], allowUnknownKeywords, false);
9797
delete schema[vocabularyToken];
9898
}
9999

0 commit comments

Comments
 (0)