Skip to content

Commit effd3a6

Browse files
authored
add comments, add setSchemaDefinitionsObject (#752)
1 parent a1513ef commit effd3a6

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

packages/_json-validator/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@quarto/_json-validator",
33
"private": true,
4-
"version": "0.1.7",
4+
"version": "0.1.8",
55
"description": "A validation library for JSON objects with an emphasis on good error messages.",
66
"author": {
77
"name": "Posit PBC"

packages/_json-validator/src/schema.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,33 @@ export function schemaExhaustiveCompletions(schema: Schema): boolean {
7171

7272
const definitionsObject: Record<string, ConcreteSchema> = {};
7373

74+
/**
75+
* Check if a schema definition exists in the current definitions object.
76+
* @param key - The key of the schema definition to check.
77+
* @returns True if the schema definition exists, false otherwise.
78+
*/
7479
export function hasSchemaDefinition(key: string): boolean {
7580
return definitionsObject[key] !== undefined;
7681
}
7782

83+
/**
84+
* Get a schema definition by its key.
85+
* @param key - The key of the schema definition to retrieve.
86+
* @returns The schema definition corresponding to the key.
87+
* @throws Error if the schema definition does not exist.
88+
*/
7889
export function getSchemaDefinition(key: string): ConcreteSchema {
7990
if (definitionsObject[key] === undefined) {
8091
throw new Error(`Schema ${key} not found.`);
8192
}
8293
return definitionsObject[key];
8394
}
8495

96+
/**
97+
* Set a schema definition in the current definitions object.
98+
* @param schema - The schema definition to set.
99+
* @throws Error if the schema does not have a $id property.
100+
*/
85101
export function setSchemaDefinition(schema: ConcreteSchema) {
86102
if (schema.$id === undefined) {
87103
throw new Error(
@@ -95,13 +111,27 @@ export function setSchemaDefinition(schema: ConcreteSchema) {
95111
}
96112
}
97113

114+
/**
115+
* Get the schema definitions object. This is a shallow copy of the current definitions.
116+
* @returns A copy of the schema definitions object.
117+
*/
98118
export function getSchemaDefinitionsObject(): Record<
99119
string,
100120
ConcreteSchema
101121
> {
102122
return Object.assign({}, definitionsObject);
103123
}
104124

125+
/**
126+
* Set the schema definitions object. This will replace the existing definitions.
127+
* @param newDefinitions - The new definitions to set.
128+
*/
129+
export function setSchemaDefinitionsObject(
130+
newDefinitions: Record<string, ConcreteSchema>
131+
): void {
132+
Object.assign(definitionsObject, newDefinitions);
133+
}
134+
105135
export function expandAliasesFrom(
106136
lst: string[],
107137
defs: Record<string, string[]>,

packages/json-validator/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@quarto/json-validator",
3-
"version": "0.1.7",
3+
"version": "0.1.8",
44
"description": "A validation library for JSON objects with an emphasis on good error messages.",
55
"author": {
66
"name": "Posit PBC"

packages/json-validator/src/schema.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,33 @@ export function schemaExhaustiveCompletions(schema: Schema): boolean {
7171

7272
const definitionsObject: Record<string, ConcreteSchema> = {};
7373

74+
/**
75+
* Check if a schema definition exists in the current definitions object.
76+
* @param key - The key of the schema definition to check.
77+
* @returns True if the schema definition exists, false otherwise.
78+
*/
7479
export function hasSchemaDefinition(key: string): boolean {
7580
return definitionsObject[key] !== undefined;
7681
}
7782

83+
/**
84+
* Get a schema definition by its key.
85+
* @param key - The key of the schema definition to retrieve.
86+
* @returns The schema definition corresponding to the key.
87+
* @throws Error if the schema definition does not exist.
88+
*/
7889
export function getSchemaDefinition(key: string): ConcreteSchema {
7990
if (definitionsObject[key] === undefined) {
8091
throw new Error(`Schema ${key} not found.`);
8192
}
8293
return definitionsObject[key];
8394
}
8495

96+
/**
97+
* Set a schema definition in the current definitions object.
98+
* @param schema - The schema definition to set.
99+
* @throws Error if the schema does not have a $id property.
100+
*/
85101
export function setSchemaDefinition(schema: ConcreteSchema) {
86102
if (schema.$id === undefined) {
87103
throw new Error(
@@ -95,13 +111,27 @@ export function setSchemaDefinition(schema: ConcreteSchema) {
95111
}
96112
}
97113

114+
/**
115+
* Get the schema definitions object. This is a shallow copy of the current definitions.
116+
* @returns A copy of the schema definitions object.
117+
*/
98118
export function getSchemaDefinitionsObject(): Record<
99119
string,
100120
ConcreteSchema
101121
> {
102122
return Object.assign({}, definitionsObject);
103123
}
104124

125+
/**
126+
* Set the schema definitions object. This will replace the existing definitions.
127+
* @param newDefinitions - The new definitions to set.
128+
*/
129+
export function setSchemaDefinitionsObject(
130+
newDefinitions: Record<string, ConcreteSchema>
131+
): void {
132+
Object.assign(definitionsObject, newDefinitions);
133+
}
134+
105135
export function expandAliasesFrom(
106136
lst: string[],
107137
defs: Record<string, string[]>,

0 commit comments

Comments
 (0)