Skip to content

Commit 48255a3

Browse files
feat: remove resolving refs before getting instance due to forked draft-04 version
1 parent ebb004d commit 48255a3

File tree

1 file changed

+16
-46
lines changed

1 file changed

+16
-46
lines changed

packages/core/src/rules/ajv.ts

Lines changed: 16 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function getAjv(resolve: ResolveFn, allowAdditionalProperties: boolean) {
4040
return ajvInstance;
4141
}
4242

43-
function getAjvDraft04() {
43+
function getAjvDraft04(resolve: ResolveFn, allowAdditionalProperties: boolean) {
4444
if (!ajvDraft04Instance) {
4545
ajvDraft04Instance = new AjvDraft04({
4646
schemaId: 'id',
@@ -53,8 +53,14 @@ function getAjvDraft04() {
5353
allowUnionTypes: true,
5454
validateFormats: true,
5555
logger: false,
56+
defaultAdditionalProperties: allowAdditionalProperties,
57+
loadSchemaSync(base, $ref, id) {
58+
const resolvedRef = resolve({ $ref }, base.split('#')[0]);
59+
if (!resolvedRef || !resolvedRef.location) return false;
60+
return { $id: resolvedRef.location.source.absoluteRef + '#' + id, ...resolvedRef.node };
61+
},
5662
});
57-
addFormats(ajvDraft04Instance);
63+
addFormats(ajvDraft04Instance as any);
5864
}
5965

6066
return ajvDraft04Instance;
@@ -78,13 +84,16 @@ function getAjvValidator(
7884
function getAjvDraft04Validator(
7985
schema: any,
8086
loc: Location,
81-
resolve: ResolveFn
87+
resolve: ResolveFn,
88+
allowAdditionalProperties: boolean
8289
): ValidateFunction | undefined {
83-
const ajvDraft04 = getAjvDraft04();
90+
const ajv = getAjvDraft04(resolve, allowAdditionalProperties);
8491

85-
const dereferencedSchema = dereferenceSchema(schema, loc.source.absoluteRef, resolve);
92+
if (!ajv.getSchema(loc.absolutePointer)) {
93+
ajv.addSchema({ $id: loc.absolutePointer, ...schema }, loc.absolutePointer);
94+
}
8695

87-
return ajvDraft04.compile(dereferencedSchema);
96+
return ajv.getSchema(loc.absolutePointer);
8897
}
8998

9099
export function validateJsonSchema(
@@ -98,7 +107,7 @@ export function validateJsonSchema(
98107
): { valid: boolean; errors: (ErrorObject & { suggest?: string[] })[] } {
99108
const validate =
100109
specVersion === 'oas3_0' || specVersion === 'oas2'
101-
? getAjvDraft04Validator(schema, schemaLoc, resolve)
110+
? getAjvDraft04Validator(schema, schemaLoc, resolve, allowAdditionalProperties)
102111
: getAjvValidator(schema, schemaLoc, resolve, allowAdditionalProperties);
103112

104113
if (!validate) return { valid: true, errors: [] }; // unresolved refs are reported
@@ -145,42 +154,3 @@ export function validateJsonSchema(
145154
};
146155
}
147156
}
148-
149-
function dereferenceSchema(
150-
schema: any,
151-
baseRef: string,
152-
resolve: ResolveFn,
153-
visited: WeakSet<any> = new WeakSet()
154-
): any {
155-
if (!schema || typeof schema !== 'object') {
156-
return schema;
157-
}
158-
159-
if (visited.has(schema)) {
160-
return schema;
161-
}
162-
visited.add(schema);
163-
164-
if (Array.isArray(schema)) {
165-
return schema.map((item) => dereferenceSchema(item, baseRef, resolve, visited));
166-
}
167-
168-
if (schema.$ref) {
169-
const resolved = resolve({ $ref: schema.$ref });
170-
if (resolved && resolved.node) {
171-
return dereferenceSchema(
172-
resolved.node,
173-
resolved.location.source.absoluteRef,
174-
resolve,
175-
visited
176-
);
177-
}
178-
return schema;
179-
}
180-
181-
const result: any = {};
182-
for (const [key, value] of Object.entries(schema)) {
183-
result[key] = dereferenceSchema(value, baseRef, resolve, visited);
184-
}
185-
return result;
186-
}

0 commit comments

Comments
 (0)