Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getJsonSchemaBaseUri #16

Merged
merged 2 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { afterAll, beforeAll, describe, expect, it, jest } from '@jest/globals';

jest.mock('@cuaklabs/uri');

import { JsonSchemaObject } from '@cuaklabs/json-schema-types/2020-12';
import { getBaseUri, GetBaseUriOptions } from '@cuaklabs/uri';

import { JsonRootSchemaFixtures } from '../fixtures/JsonRootSchemaFixtures';
import { getJsonSchemaBaseUri } from './getJsonSchemaBaseUri';

describe(getJsonSchemaBaseUri.name, () => {
describe('having a JsonSchema with $id', () => {
let jsonSchemaFixture: JsonSchemaObject;

beforeAll(() => {
jsonSchemaFixture = JsonRootSchemaFixtures.withId;
});

describe('when called', () => {
let uriFixture: string;

let result: unknown;

beforeAll(() => {
uriFixture = 'uri:fixture';

(getBaseUri as jest.Mock<typeof getBaseUri>).mockReturnValueOnce(
uriFixture,
);

result = getJsonSchemaBaseUri(jsonSchemaFixture);
});

afterAll(() => {
jest.clearAllMocks();
});

it('should call getBaseUri()', () => {
const expected: GetBaseUriOptions = {
documentBaseUri: jsonSchemaFixture.$id,
};

expect(getBaseUri).toHaveBeenCalledTimes(1);
expect(getBaseUri).toHaveBeenCalledWith(expected);
});

it('should return an Uri()', () => {
expect(result).toBe(uriFixture);
});
});
});

describe('having a JsonSchema with no id', () => {
let jsonSchemaFixture: JsonSchemaObject;

beforeAll(() => {
jsonSchemaFixture = JsonRootSchemaFixtures.withNoId;
});

describe('when called', () => {
let uriFixture: string;

let result: unknown;

beforeAll(() => {
uriFixture = 'uri:fixture';

(getBaseUri as jest.Mock<typeof getBaseUri>).mockReturnValueOnce(
uriFixture,
);

result = getJsonSchemaBaseUri(jsonSchemaFixture);
});

afterAll(() => {
jest.clearAllMocks();
});

it('should call getBaseUri()', () => {
const expected: GetBaseUriOptions = {
documentBaseUri: undefined,
};

expect(getBaseUri).toHaveBeenCalledTimes(1);
expect(getBaseUri).toHaveBeenCalledWith(expected);
});

it('should return an Uri()', () => {
expect(result).toBe(uriFixture);
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { JsonSchema } from '@cuaklabs/json-schema-types/2020-12';
import { getBaseUri } from '@cuaklabs/uri';

export interface GetJsonSchemaBaseUriOptions {
encapsulatingDocumentBaseUri?: string | undefined;
retrievalUri?: string | undefined;
}

export function getJsonSchemaBaseUri(
schema: JsonSchema,
options?: GetJsonSchemaBaseUriOptions,
): string {
const documentBaseUri: string | undefined =
typeof schema === 'boolean' ? undefined : schema.$id;

return getBaseUri({
...(options ?? {}),
documentBaseUri,
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
import {
JsonRootSchemaObject,
jsonSchemaTypes,
} from '@cuaklabs/json-schema-types/2020-12';

export class JsonRootSchemaFixtures {
public static get any(): JsonRootSchemaObject {
return {
$schema: 'https://json-schema.org/draft/2020-12/schema',
};
}

public static get withId(): JsonRootSchemaObject {
return {
...JsonRootSchemaFixtures.any,
$id: 'https://schema.id',
};
}

public static get withNoId(): JsonRootSchemaObject {
const fixture: JsonRootSchemaObject = JsonRootSchemaFixtures.any;

delete fixture.$id;

return fixture;
}

public static get with$DefsOne(): JsonRootSchemaObject {
return {
...JsonRootSchemaFixtures.any,
$defs: {
enabledToggle: {
default: null,
description: `Whether the feature is enabled (true),
disabled (false), or under
automatic control (null)`,
title: 'Enabled',
type: [jsonSchemaTypes.boolean, jsonSchemaTypes.null],
},
},
};
}

public static get withAdditionalProperties(): JsonRootSchemaObject {
return {
...JsonRootSchemaFixtures.any,
additionalProperties: { $ref: 'other.json' },
};
}

public static get withAllOfTwo(): JsonRootSchemaObject {
return {
...JsonRootSchemaFixtures.any,
allOf: [{ maximum: 30 }, { minimum: 20 }],
};
}

public static get withAnyOfTwo(): JsonRootSchemaObject {
return {
...JsonRootSchemaFixtures.any,
anyOf: [{ maximum: 30 }, { minimum: 20 }],
};
}

public static get withContains(): JsonRootSchemaObject {
return {
...JsonRootSchemaFixtures.any,
contains: {
enabledToggle: {
default: null,
description: `Whether the feature is enabled (true),
disabled (false), or under
automatic control (null)`,
title: 'Enabled',
type: [jsonSchemaTypes.boolean, jsonSchemaTypes.null],
},
},
};
}

public static get withDependentSchemasOne(): JsonRootSchemaObject {
return {
...JsonRootSchemaFixtures.any,
dependentSchemas: {
enabledToggle: {
default: null,
description: `Whether the feature is enabled (true),
disabled (false), or under
automatic control (null)`,
title: 'Enabled',
type: [jsonSchemaTypes.boolean, jsonSchemaTypes.null],
},
},
};
}

public static get withElse(): JsonRootSchemaObject {
return {
...JsonRootSchemaFixtures.any,
else: {
enabledToggle: {
default: null,
description: `Whether the feature is enabled (true),
disabled (false), or under
automatic control (null)`,
title: 'Enabled',
type: [jsonSchemaTypes.boolean, jsonSchemaTypes.null],
},
},
};
}

public static get withIf(): JsonRootSchemaObject {
return {
...JsonRootSchemaFixtures.any,
if: {
enabledToggle: {
default: null,
description: `Whether the feature is enabled (true),
disabled (false), or under
automatic control (null)`,
title: 'Enabled',
type: [jsonSchemaTypes.boolean, jsonSchemaTypes.null],
},
},
};
}

public static get withItems(): JsonRootSchemaObject {
return {
...JsonRootSchemaFixtures.any,
items: {
enabledToggle: {
default: null,
description: `Whether the feature is enabled (true),
disabled (false), or under
automatic control (null)`,
title: 'Enabled',
type: [jsonSchemaTypes.boolean, jsonSchemaTypes.null],
},
},
};
}

public static get withNot(): JsonRootSchemaObject {
return {
...JsonRootSchemaFixtures.any,
not: {
enabledToggle: {
default: null,
description: `Whether the feature is enabled (true),
disabled (false), or under
automatic control (null)`,
title: 'Enabled',
type: [jsonSchemaTypes.boolean, jsonSchemaTypes.null],
},
},
};
}

public static get withOneOfTwo(): JsonRootSchemaObject {
return {
...JsonRootSchemaFixtures.any,
oneOf: [{ maximum: 20 }, { minimum: 30 }],
};
}

public static get withPatternProperiesOne(): JsonRootSchemaObject {
return {
...JsonRootSchemaFixtures.any,
patternProperties: {
'^[a-z0-9]+$': {
type: [jsonSchemaTypes.boolean, jsonSchemaTypes.null],
},
},
};
}

public static get withPrefixItemsOne(): JsonRootSchemaObject {
return {
...JsonRootSchemaFixtures.any,
prefixItems: [
{
enabledToggle: {
default: null,
description: `Whether the feature is enabled (true),
disabled (false), or under
automatic control (null)`,
title: 'Enabled',
type: [jsonSchemaTypes.boolean, jsonSchemaTypes.null],
},
},
],
};
}

public static get withProperiesOne(): JsonRootSchemaObject {
return {
...JsonRootSchemaFixtures.any,
properties: {
z39: {
type: [jsonSchemaTypes.boolean, jsonSchemaTypes.null],
},
},
};
}

public static get withProperyNames(): JsonRootSchemaObject {
return {
...JsonRootSchemaFixtures.any,
propertyNames: { maxLength: 3 },
};
}

public static get withThen(): JsonRootSchemaObject {
return {
...JsonRootSchemaFixtures.any,
then: {
enabledToggle: {
default: null,
description: `Whether the feature is enabled (true),
disabled (false), or under
automatic control (null)`,
title: 'Enabled',
type: [jsonSchemaTypes.boolean, jsonSchemaTypes.null],
},
},
};
}

public static get withUnevaluatedItems(): JsonRootSchemaObject {
return {
...JsonRootSchemaFixtures.any,
unevaluatedItems: { maxLength: 3 },
};
}

public static get withUnevaluatedProperties(): JsonRootSchemaObject {
return {
...JsonRootSchemaFixtures.any,
unevaluatedProperties: { maxLength: 3 },
};
}
}