Skip to content

Commit

Permalink
test(json-schema-parser): add JsonRootSchemaFixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
notaphplover committed Sep 6, 2023
1 parent ebb0b66 commit d00d34b
Show file tree
Hide file tree
Showing 2 changed files with 247 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ jest.mock('@cuaklabs/uri');
import { JsonSchemaObject } from '@cuaklabs/json-schema-types/2020-12';
import { getBaseUri, GetBaseUriOptions } from '@cuaklabs/uri';

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

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

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

describe('when called', () => {
Expand Down Expand Up @@ -54,7 +54,7 @@ describe(getJsonSchemaBaseUri.name, () => {
let jsonSchemaFixture: JsonSchemaObject;

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

describe('when called', () => {
Expand Down
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 },
};
}
}

0 comments on commit d00d34b

Please sign in to comment.