Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
23 changes: 23 additions & 0 deletions .github/workflows/extract-schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Extract Schema

on:
push:
branches: [main]
pull_request:

jobs:
extract-schema:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: oven-sh/setup-bun@v2
with:
bun-version: latest

- run: bun install

- run: bun run bin/cli.js extract-schema --folder /tmp/extracted-schemas

- name: Compare extracted schemas with core-spec/v1/schemas
run: diff -r core-spec/v1/schemas /tmp/extracted-schemas
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
name: Schema
name: Validate Schema

on:
push:
branches: [main]
pull_request:

jobs:
schema-test:
validate-schema:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ npx @metabase/representations validate-schema --folder ./my-export

Omit `--folder` to validate the current directory.

### Extracting schemas

Copy the bundled schemas (preserving folder structure) into a target directory:

```sh
npx @metabase/representations extract-schema --folder ./schemas
```

Omit `--folder` to extract into the current directory.

### Programmatic

```js
Expand Down
52 changes: 30 additions & 22 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { parseArgs } from "node:util";
import { relative } from "path";
import { validateSchema } from "../src/validate-schema.js";
import { extractSchema } from "../src/extract-schema.js";

const { values, positionals } = parseArgs({
allowPositionals: true,
Expand All @@ -19,37 +20,44 @@ if (values.help || !command) {

Commands:
validate-schema Validate YAML files against Metabase representation schemas
extract-schema Copy bundled schemas into a target folder

Options:
--folder <path> Folder to validate (default: current directory)
--folder <path> Folder to validate or extract into (default: cwd)
-h, --help Show this help message`);
process.exit(command ? 0 : 1);
}

if (command !== "validate-schema") {
console.error(`Unknown command: ${command}`);
process.exit(1);
}

const folder = values.folder ?? process.cwd();
const { results, passed, failed } = validateSchema({ folder });
if (command === "validate-schema") {
const folder = values.folder ?? process.cwd();
const { results, passed, failed } = validateSchema({ folder });

if (results.length === 0) {
console.error(`No YAML files found in ${folder}`);
process.exit(1);
}
if (results.length === 0) {
console.error(`No YAML files found in ${folder}`);
process.exit(1);
}

for (const result of results) {
const path = relative(process.cwd(), `${folder}/${result.file}`);
if (result.status === "ok") {
console.log(`OK ${path} (${result.model})`);
} else {
console.error(`FAIL ${path}${result.model ? ` (${result.model})` : ""}`);
for (const error of result.errors) {
console.error(` ${error.path} ${error.message}`);
for (const result of results) {
const path = relative(process.cwd(), `${folder}/${result.file}`);
if (result.status === "ok") {
console.log(`OK ${path} (${result.model})`);
} else {
console.error(`FAIL ${path}${result.model ? ` (${result.model})` : ""}`);
for (const error of result.errors) {
console.error(` ${error.path} ${error.message}`);
}
}
}

console.log(`\n${passed} passed, ${failed} failed`);
process.exit(failed > 0 ? 1 : 0);
}

if (command === "extract-schema") {
const { target } = extractSchema({ folder: values.folder ?? process.cwd() });
console.log(`Schemas extracted to ${target}`);
process.exit(0);
}

console.log(`\n${passed} passed, ${failed} failed`);
process.exit(failed > 0 ? 1 : 0);
console.error(`Unknown command: ${command}`);
process.exit(1);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@metabase/representations",
"version": "1.1.3",
"version": "1.1.4",
"description": "Metabase representation format specification and schema validator",
"license": "SEE LICENSE IN LICENSE.txt",
"repository": {
Expand Down
12 changes: 12 additions & 0 deletions src/extract-schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { cpSync, mkdirSync } from "fs";
import { resolve } from "path";

const PACKAGE_ROOT = resolve(import.meta.dirname, "..");

export function extractSchema({ folder }) {
const schemasDir = resolve(PACKAGE_ROOT, "core-spec/v1/schemas");
const target = resolve(folder);
mkdirSync(target, { recursive: true });
cpSync(schemasDir, target, { recursive: true });
return { source: schemasDir, target };
}
Loading