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

fix: remove schema bundler from the validator #1090

Merged
merged 7 commits into from
Aug 4, 2022
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
2 changes: 2 additions & 0 deletions packages/js/manifests/polywrap/scripts/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ async function generateFormatTypes() {
os.writeFileSync(tsOutputPath, tsSrc);
};

const firstItem = <T>(arr: Array<T>) => arr[0];
const lastItem = <T>(arr: Array<T>) => arr[arr.length - 1];
const versionToTs = (version: string) =>
version.replace(/\./g, "_").replace(/\-/g, "_");
Expand Down Expand Up @@ -137,6 +138,7 @@ async function generateFormatTypes() {

const validateContext = {
formats: validateFormats,
first: firstItem(validateFormats),
latest: lastItem(validateFormats),
validators: [] as string[],
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from ".";
{{#validators.length}}import * as Validators from "../../validators";{{/validators.length}}
{{#formats}}
import schema_{{tsVersion}} from "@polywrap/polywrap-manifest-schemas/formats/{{dir}}/{{version}}.json";
import {{type}}Schema_{{tsVersion}} from "@polywrap/polywrap-manifest-schemas/formats/{{dir}}/{{version}}.json";
{{/formats}}

import {
Expand All @@ -26,9 +26,9 @@ type {{#latest}}{{type}}{{/latest}}Schemas = {

const schemas: {{#latest}}{{type}}{{/latest}}Schemas = {
// NOTE: Patch fix for backwards compatability
"0.1.0": schema_0_1,
"0.1.0": {{#first}}{{type}}{{/first}}Schema_0_1,
{{#formats}}
"{{version}}": schema_{{tsVersion}},
"{{version}}": {{type}}Schema_{{tsVersion}},
{{/formats}}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
AppManifestFormats
} from ".";

import schema_0_1 from "@polywrap/polywrap-manifest-schemas/formats/polywrap.app/0.1.json";
import AppManifestSchema_0_1 from "@polywrap/polywrap-manifest-schemas/formats/polywrap.app/0.1.json";

import {
Schema,
Expand All @@ -24,8 +24,8 @@ type AppManifestSchemas = {

const schemas: AppManifestSchemas = {
// NOTE: Patch fix for backwards compatability
"0.1.0": schema_0_1,
"0.1": schema_0_1,
"0.1.0": AppManifestSchema_0_1,
"0.1": AppManifestSchema_0_1,
};

const validator = new Validator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
BuildManifestFormats
} from ".";

import schema_0_1 from "@polywrap/polywrap-manifest-schemas/formats/polywrap.build/0.1.json";
import BuildManifestSchema_0_1 from "@polywrap/polywrap-manifest-schemas/formats/polywrap.build/0.1.json";

import {
Schema,
Expand All @@ -24,8 +24,8 @@ type BuildManifestSchemas = {

const schemas: BuildManifestSchemas = {
// NOTE: Patch fix for backwards compatability
"0.1.0": schema_0_1,
"0.1": schema_0_1,
"0.1.0": BuildManifestSchema_0_1,
"0.1": BuildManifestSchema_0_1,
};

const validator = new Validator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
DeployManifestFormats
} from ".";

import schema_0_1 from "@polywrap/polywrap-manifest-schemas/formats/polywrap.deploy/0.1.json";
import DeployManifestSchema_0_1 from "@polywrap/polywrap-manifest-schemas/formats/polywrap.deploy/0.1.json";

import {
Schema,
Expand All @@ -24,8 +24,8 @@ type DeployManifestSchemas = {

const schemas: DeployManifestSchemas = {
// NOTE: Patch fix for backwards compatability
"0.1.0": schema_0_1,
"0.1": schema_0_1,
"0.1.0": DeployManifestSchema_0_1,
"0.1": DeployManifestSchema_0_1,
};

const validator = new Validator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
InfraManifestFormats
} from ".";

import schema_0_1 from "@polywrap/polywrap-manifest-schemas/formats/polywrap.infra/0.1.json";
import InfraManifestSchema_0_1 from "@polywrap/polywrap-manifest-schemas/formats/polywrap.infra/0.1.json";

import {
Schema,
Expand All @@ -24,8 +24,8 @@ type InfraManifestSchemas = {

const schemas: InfraManifestSchemas = {
// NOTE: Patch fix for backwards compatability
"0.1.0": schema_0_1,
"0.1": schema_0_1,
"0.1.0": InfraManifestSchema_0_1,
"0.1": InfraManifestSchema_0_1,
};

const validator = new Validator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
MetaManifestFormats
} from ".";

import schema_0_1 from "@polywrap/polywrap-manifest-schemas/formats/polywrap.meta/0.1.json";
import MetaManifestSchema_0_1 from "@polywrap/polywrap-manifest-schemas/formats/polywrap.meta/0.1.json";

import {
Schema,
Expand All @@ -24,8 +24,8 @@ type MetaManifestSchemas = {

const schemas: MetaManifestSchemas = {
// NOTE: Patch fix for backwards compatability
"0.1.0": schema_0_1,
"0.1": schema_0_1,
"0.1.0": MetaManifestSchema_0_1,
"0.1": MetaManifestSchema_0_1,
};

const validator = new Validator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
PluginManifestFormats
} from ".";

import schema_0_1 from "@polywrap/polywrap-manifest-schemas/formats/polywrap.plugin/0.1.json";
import PluginManifestSchema_0_1 from "@polywrap/polywrap-manifest-schemas/formats/polywrap.plugin/0.1.json";

import {
Schema,
Expand All @@ -24,8 +24,8 @@ type PluginManifestSchemas = {

const schemas: PluginManifestSchemas = {
// NOTE: Patch fix for backwards compatability
"0.1.0": schema_0_1,
"0.1": schema_0_1,
"0.1.0": PluginManifestSchema_0_1,
"0.1": PluginManifestSchema_0_1,
};

const validator = new Validator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
PolywrapManifestFormats
} from ".";

import schema_0_1 from "@polywrap/polywrap-manifest-schemas/formats/polywrap/0.1.json";
import PolywrapManifestSchema_0_1 from "@polywrap/polywrap-manifest-schemas/formats/polywrap/0.1.json";

import {
Schema,
Expand All @@ -24,8 +24,8 @@ type PolywrapManifestSchemas = {

const schemas: PolywrapManifestSchemas = {
// NOTE: Patch fix for backwards compatability
"0.1.0": schema_0_1,
"0.1": schema_0_1,
"0.1.0": PolywrapManifestSchema_0_1,
"0.1": PolywrapManifestSchema_0_1,
};

const validator = new Validator();
Expand Down
2 changes: 1 addition & 1 deletion packages/js/manifests/wrap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
},
"dependencies": {
"@polywrap/msgpack-js": "0.3.0",
"@polywrap/wrap-manifest-schemas": "0.3.0",
"json-schema-ref-parser": "9.0.9",
"jsonschema": "1.4.0",
"semver": "7.3.5"
},
"devDependencies": {
"@polywrap/os-js": "0.3.0",
"@polywrap/wrap-manifest-schemas": "0.3.0",
"@types/jest": "26.0.8",
"@types/mustache": "4.0.1",
"@types/prettier": "2.6.0",
Expand Down
3 changes: 3 additions & 0 deletions packages/js/manifests/wrap/scripts/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ async function generateFormatTypes() {
`/* eslint-disable @typescript-eslint/naming-convention */\n${tsFile}`
);

const schemaOutputPath = path.join(wrapOutputDir, `${wrapVersion}.schema.json`);
os.writeFileSync(schemaOutputPath, JSON.stringify(bundledSchema, null ,2));

// Add metadata for the root index.ts file to use
wrapModules.push({
interface: wrapSchema.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ import {
} from "./{{version}}";
{{/formats}}

{{#formats}}
import WrapManifestSchema_{{tsVersion}} from "./{{version}}.schema.json";
{{/formats}}

export {
{{#formats}}
WrapManifest_{{tsVersion}},
WrapAbi_{{tsVersion}}_{{abiTsVersion}}
WrapAbi_{{tsVersion}}_{{abiTsVersion}},
WrapManifestSchema_{{tsVersion}}
{{/formats}}
};

Expand Down
58 changes: 11 additions & 47 deletions packages/js/manifests/wrap/scripts/templates/validate-ts.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,33 @@
*/
import {
AnyWrapManifest,
WrapManifestVersions
WrapManifestVersions,
{{#formats}}
WrapManifestSchema_{{tsVersion}}
{{/formats}}
} from ".";
{{#validators.length}}import * as Validators from "../validators";{{/validators.length}}
{{#formats}}
import schema_{{tsVersion}} from "@polywrap/wrap-manifest-schemas/formats/wrap.info/{{version}}.json";
import abi_schema_{{abiTsVersion}} from "@polywrap/wrap-manifest-schemas/formats/abi/{{abiVersion}}.json";
{{/formats}}

import {
Schema,
Validator,
ValidationError,
ValidatorResult
} from "jsonschema";
import { resolve, bundle, FileInfo, $Refs } from "json-schema-ref-parser";
import path from "path";
import { resolve, $Refs } from "json-schema-ref-parser";

type WrapManifestSchemas = {
[key in WrapManifestVersions]: Schema | undefined
};

type WrapAbiSchemas = {
[key in WrapManifestVersions]: Schema | undefined
}

const schemas: WrapManifestSchemas = {
// NOTE: Patch fix for backwards compatability
"0.1.0": schema_0_1,
"0.1.0": WrapManifestSchema_0_1,
{{#formats}}
"{{version}}": schema_{{tsVersion}},
"{{version}}": WrapManifestSchema_{{tsVersion}},
{{/formats}}
};

const abiSchemas: WrapAbiSchemas = {
// NOTE: Patch fix for backwards compatability
"0.1.0": abi_schema_0_1,
{{#formats}}
"{{version}}": abi_schema_{{abiTsVersion}},
{{/formats}}
}

{{#validators}}
Validator.prototype.{{.}} = Validators.{{.}};
{{/validators}}
Expand All @@ -65,37 +51,15 @@ export async function validateWrapManifest(
extSchema: Schema | undefined = undefined
): Promise<void> {
const schema = schemas[manifest.version as WrapManifestVersions];
const abiSchema = abiSchemas[manifest.version as WrapManifestVersions];

if (!schema || !abiSchema) {
if (!schema) {
throw new Error(`Unrecognized WrapManifest schema version "${manifest.version}"\nmanifest: ${JSON.stringify(manifest, null, 2)}`);
}

if (!schema.properties) {
// This should never happen
throw new Error(`WrapManifest schema doesn't contain any properties`)
}

const abiJsonSchemaRelPath = schema.properties.abi.$ref as string;

const finalSchema = await bundle(schema as any, {
resolve: {
file: {
read: (file: FileInfo) => {
// If both url is same
if (!path.relative(abiJsonSchemaRelPath, file.url)) {
return abiSchema as any;
}
return file.data;
},
},
},
});

const refs: $Refs = await resolve(finalSchema);
const refs: $Refs = await resolve(schema as any);

const validator = new Validator();
validator.addSchema(finalSchema as Schema);
validator.addSchema(schema);

const resolveRefs = () => {
const unresolvedRef = validator.unresolvedRefs.shift();
Expand All @@ -113,7 +77,7 @@ export async function validateWrapManifest(

resolveRefs();

throwIfErrors(validator.validate(manifest, finalSchema as Schema), manifest.version);
throwIfErrors(validator.validate(manifest, schema), manifest.version);

if (extSchema) {
throwIfErrors(validator.validate(manifest, extSchema), manifest.version);
Expand Down
Loading