Skip to content

Commit

Permalink
fix: type resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
zekth committed Dec 20, 2021
1 parent 349fb2f commit 0a76537
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
12 changes: 7 additions & 5 deletions lib/compile/resolve.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {AnySchema, AnySchemaObject} from "../types"
import type {AnySchema, AnySchemaObject, UriResolver} from "../types"
import type Ajv from "../ajv"
import {eachItem} from "./util"
import * as equal from "fast-deep-equal"
Expand Down Expand Up @@ -67,7 +67,7 @@ function countKeys(schema: AnySchemaObject): number {
return count
}

export function getFullPath(id = "", normalize?: boolean, resolver?: typeof URI): string {
export function getFullPath(id = "", normalize?: boolean, resolver?: UriResolver): string {
if (normalize !== false) id = normalizeId(id)
let p: URI.URIComponents
if (resolver !== undefined) {
Expand All @@ -78,7 +78,7 @@ export function getFullPath(id = "", normalize?: boolean, resolver?: typeof URI)
return _getFullPath(p, resolver)
}

export function _getFullPath(p: URI.URIComponents, resolver?: typeof URI): string {
export function _getFullPath(p: URI.URIComponents, resolver?: UriResolver): string {
let serialized: string
if (resolver !== undefined) {
serialized = resolver.serialize(p)
Expand Down Expand Up @@ -119,8 +119,10 @@ export function getSchemaRefs(this: Ajv, schema: AnySchema, baseId: string): Loc
baseIds[jsonPtr] = baseId

function addRef(this: Ajv, ref: string): string {
const resolve = this.opts.uriResolver ? this.opts.uriResolver.resolve : URI.resolve
ref = normalizeId(baseId ? resolve(baseId, ref) : ref)
const _resolve =
// eslint-disable-next-line @typescript-eslint/unbound-method
this.opts.uriResolver !== undefined ? this.opts.uriResolver?.resolve : URI.resolve
ref = normalizeId(baseId ? _resolve(baseId, ref) : ref)
if (schemaRefs.has(ref)) throw ambiguos(ref)
schemaRefs.add(ref)
let schOrRef = this.refs[ref]
Expand Down
5 changes: 2 additions & 3 deletions lib/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import type {
Format,
AddedFormat,
RegExpEngine,
UriResolver,
} from "./types"
import type {JSONSchemaType} from "./types/json-schema"
import type {JTDSchemaType, SomeJTDSchemaType, JTDDataType} from "./types/jtd-schema"
Expand All @@ -60,8 +61,6 @@ import {Code, ValueScope} from "./compile/codegen"
import {normalizeId, getSchemaRefs} from "./compile/resolve"
import {getJSONTypes} from "./compile/validate/dataType"
import {eachItem} from "./compile/util"
import * as URI from "uri-js"

import * as $dataRefSchema from "./refs/data.json"

const defaultRegExp: RegExpEngine = (str, flags) => new RegExp(str, flags)
Expand Down Expand Up @@ -137,7 +136,7 @@ export interface CurrentOptions {
int32range?: boolean // JTD only
messages?: boolean
code?: CodeOptions // NEW
uriResolver?: typeof URI
uriResolver?: UriResolver
}

export interface CodeOptions {
Expand Down
7 changes: 7 additions & 0 deletions lib/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as URI from "uri-js"
import type {CodeGen, Code, Name, ScopeValueSets, ValueScopeName} from "../compile/codegen"
import type {SchemaEnv, SchemaCxt, SchemaObjCxt} from "../compile"
import type {JSONType} from "../compile/rules"
Expand Down Expand Up @@ -231,3 +232,9 @@ export interface RegExpEngine {
export interface RegExpLike {
test: (s: string) => boolean
}

export interface UriResolver {
parse(uri: string): URI.URIComponents
resolve(base: string, path: string): string
serialize(): string
}

0 comments on commit 0a76537

Please sign in to comment.