Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

Commit

Permalink
Lint & format files
Browse files Browse the repository at this point in the history
  • Loading branch information
gcornut committed Jan 25, 2024
1 parent 16f5ddb commit 04a8adf
Show file tree
Hide file tree
Showing 19 changed files with 877 additions and 750 deletions.
146 changes: 73 additions & 73 deletions cli/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,96 +1,96 @@
import { expect, test, describe } from 'vitest';
import { runCLI, readFile } from './test/utils';
import { describe, expect, test } from 'vitest';
import { readFile, runCLI } from './test/utils';

function testCase({ run, expectedOutput }: { run: string; expectedOutput: string | RegExp }) {
return async () => {
const actual = await runCLI(run);
if (expectedOutput instanceof RegExp)
expect(actual).toMatch(expectedOutput);
if (expectedOutput instanceof RegExp) expect(actual).toMatch(expectedOutput);
else expect(actual).toEqual(expectedOutput);
};
}

describe('module formats', () => {
test('Convert named export', testCase({
run: 'to-json-schema ./single-type-export.valibot.ts',
expectedOutput: readFile('./single-type-export.schema.json'),
}));
test(
'Convert named export',
testCase({
run: 'to-json-schema ./single-type-export.valibot.ts',
expectedOutput: readFile('./single-type-export.schema.json'),
}),
);

test(
'Convert default export',
testCase({
run: 'to-json-schema ./single-type-export-default.valibot.ts',
expectedOutput: readFile('./single-type-export-default.schema.json'),
}),
);
test(
'Convert default export',
testCase({
run: 'to-json-schema ./single-type-export-default.valibot.ts',
expectedOutput: readFile('./single-type-export-default.schema.json'),
}),
);

test(
'Convert both default export and named export',
testCase({
run: 'to-json-schema ./single-type-multi-exports.valibot.ts',
expectedOutput: readFile('./single-type-multi-exports.schema.json'),
}),
);
test(
'Convert both default export and named export',
testCase({
run: 'to-json-schema ./single-type-multi-exports.valibot.ts',
expectedOutput: readFile('./single-type-multi-exports.schema.json'),
}),
);

test(
'Convert from CJS module',
testCase({
run: 'to-json-schema ./complex-type.valibot.cjs',
expectedOutput: readFile('./complex-type.schema.json'),
}),
);
test(
'Convert from CJS module',
testCase({
run: 'to-json-schema ./complex-type.valibot.cjs',
expectedOutput: readFile('./complex-type.schema.json'),
}),
);
});

describe('main schema and definitions', () => {
test(
'Choose main type',
testCase({
run: 'to-json-schema ./complex-type.valibot.ts -t ListElement',
expectedOutput: readFile('./complex-type-root-list-element.schema.json'),
}),
);
test(
'Choose main type',
testCase({
run: 'to-json-schema ./complex-type.valibot.ts -t ListElement',
expectedOutput: readFile('./complex-type-root-list-element.schema.json'),
}),
);

test(
'Convert nested main type',
testCase({
run: 'to-json-schema ./single-type-nested.valibot.ts -t schemas.NumberSchema',
expectedOutput: readFile('./single-type-nested-with-main.schema.json'),
}),
);
test(
'Convert nested main type',
testCase({
run: 'to-json-schema ./single-type-nested.valibot.ts -t schemas.NumberSchema',
expectedOutput: readFile('./single-type-nested-with-main.schema.json'),
}),
);

test(
'Convert nested definitions type',
testCase({
run: 'to-json-schema ./single-type-nested.valibot.ts --definitions schemas',
expectedOutput: readFile(
'./single-type-nested-with-definitions.schema.json',
),
}),
);
test(
'Convert nested definitions type',
testCase({
run: 'to-json-schema ./single-type-nested.valibot.ts --definitions schemas',
expectedOutput: readFile('./single-type-nested-with-definitions.schema.json'),
}),
);
});

describe('date strategy', () => {
test(
'Convert date type without date strategy',
testCase({
run: 'to-json-schema ./date-type.valibot.ts',
expectedOutput: /Error: The 'dateStrategy' option must be set/i,
}),
);
test(
'Convert date type without date strategy',
testCase({
run: 'to-json-schema ./date-type.valibot.ts',
expectedOutput: /Error: The "dateStrategy" option must be set to handle/i,
}),
);

test(
'Convert date type with integer date strategy',
testCase({
run: 'to-json-schema --dateStrategy integer ./date-type.valibot.ts',
expectedOutput: readFile('./date-type-integer.schema.json'),
}),
);
test(
'Convert date type with integer date strategy',
testCase({
run: 'to-json-schema --dateStrategy integer ./date-type.valibot.ts',
expectedOutput: readFile('./date-type-integer.schema.json'),
}),
);

test(
'Convert date type with string date strategy',
testCase({
run: 'to-json-schema --dateStrategy string ./date-type.valibot.ts',
expectedOutput: readFile('./date-type-string.schema.json'),
}),
);
test(
'Convert date type with string date strategy',
testCase({
run: 'to-json-schema --dateStrategy string ./date-type.valibot.ts',
expectedOutput: readFile('./date-type-string.schema.json'),
}),
);
});
17 changes: 10 additions & 7 deletions cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import { Command, Option } from 'commander';
import stableStringify from 'safe-stable-stringify';
import path from 'path';
import fs from 'fs';
import path from 'path';
import { Command, Option } from 'commander';
import get from 'lodash/get';
import stableStringify from 'safe-stable-stringify';

import { toJSONSchema } from '../src';
import { isSchema } from '../src/utils/valibot';
import { DateStrategy } from '../src/toJSONSchema/types';
import { isSchema } from '../src/utils/valibot';

const program = new Command();

program.command('to-json-schema <path>')
program
.command('to-json-schema <path>')
.description('Convert a Valibot schema exported from a JS or TS module.')
.option('-o, --out <file>', 'Set the output file (default: stdout)')
.option('-t, --type <type>', 'Path to the main type')
.option('-d, --definitions <object_path>', 'Path to the definitions')
.option('--strictObjectTypes', 'Make object strict object types (no unknown keys)')
.addOption(new Option('--dateStrategy <strategy>', 'Define how date validator should be converted').choices(Object.values(DateStrategy)))
.addOption(
new Option('--dateStrategy <strategy>', 'Define how date validator should be converted').choices(Object.values(DateStrategy)),
)
.action((sourcePath, { type, definitions: definitionsPath, out, strictObjectTypes, dateStrategy }) => {
try {
// Enable auto transpile of ESM & TS modules required
Expand Down Expand Up @@ -53,7 +56,7 @@ program.command('to-json-schema <path>')

// Convert
const jsonSchema = toJSONSchema({ schema, definitions, strictObjectTypes, dateStrategy });
const jsonSchemaString = stableStringify(jsonSchema, null, 2)!;
const jsonSchemaString = stableStringify(jsonSchema, null, 2);
if (out) {
// Output to file
fs.writeFileSync(out, jsonSchemaString);
Expand Down
10 changes: 2 additions & 8 deletions cli/test/complex-type-root-list-element.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
"const": "ul"
}
},
"required": [
"type",
"children"
],
"required": ["type", "children"],
"type": "object"
},
"ListItemElement": {
Expand All @@ -39,10 +36,7 @@
"const": "li"
}
},
"required": [
"type",
"children"
],
"required": ["type", "children"],
"type": "object"
}
}
Expand Down
12 changes: 3 additions & 9 deletions cli/test/complex-type.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
"const": "ul"
}
},
"required": [
"type",
"children"
],
"required": ["type", "children"],
"type": "object"
},
"ListItemElement": {
Expand All @@ -38,11 +35,8 @@
"const": "li"
}
},
"required": [
"type",
"children"
],
"required": ["type", "children"],
"type": "object"
}
}
}
}
1 change: 0 additions & 1 deletion cli/test/complex-type.valibot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ export const ListElement = v.object({
type: v.literal('ul'),
children: v.array(ListItemElement),
});

2 changes: 1 addition & 1 deletion cli/test/single-type-export.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
"type": "number"
}
}
}
}
2 changes: 1 addition & 1 deletion cli/test/single-type-multi-exports.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"type": "number"
}
}
}
}
6 changes: 3 additions & 3 deletions cli/test/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import util from 'util';
import childProcess from 'child_process';
import path from 'path';
import fs from 'fs';
import path from 'path';
import util from 'util';

const root = path.resolve(__dirname, '../../');
const packageJson = JSON.parse(fs.readFileSync(path.join(root, 'package.json')).toString());
Expand All @@ -12,7 +12,7 @@ let cliBuiltPromise: Promise<void> | undefined;
// Build it once
async function buildOnce() {
if (cliBuiltPromise) return cliBuiltPromise;
const command = `yarn build`;
const command = 'yarn build';
cliBuiltPromise = exec(command) as Promise<any>;
await cliBuiltPromise;
}
Expand Down
2 changes: 1 addition & 1 deletion src/extension/assignExtraJSONSchemaFeatures.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SupportedSchemas } from '../toJSONSchema/schemas';
import { JSONSchema7 } from 'json-schema';
import { SupportedSchemas } from '../toJSONSchema/schemas';
import { getJSONSchemaFeatures } from './withJSONSchemaFeatures';

export function assignExtraJSONSchemaFeatures(schema: SupportedSchemas, converted: JSONSchema7) {
Expand Down
2 changes: 1 addition & 1 deletion src/extension/withJSONSchemaFeatures.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { BaseSchema } from 'valibot';
import type { JSONSchema7 } from 'json-schema';
import type { BaseSchema } from 'valibot';

const JSON_SCHEMA_FEATURES_KEY = '__json_schema_features';

Expand Down
Loading

0 comments on commit 04a8adf

Please sign in to comment.