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

[Major Refactor] Native typescript support, circular references fix, vitest #29

Merged
merged 7 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix tests and filter if/then
  • Loading branch information
jonluca committed Aug 1, 2022
commit e66790a885e7b88daa59c624d53fadafc151c7a4
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ function rewriteIfThenElse(schema: SchemaType) {
*/
if ('if' in schema && schema.if && schema.then) {
schema.oneOf = [
{ allOf: [schema.if, schema.then] },
{ allOf: [{ not: schema.if }, schema.else] },
{ allOf: [schema.if, schema.then].filter(Boolean) },
{ allOf: [{ not: schema.if }, schema.else].filter(Boolean) },
];
delete schema.if;
delete schema.then;
Expand Down
71 changes: 71 additions & 0 deletions test/if-then-else.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,74 @@ it('if-then-else', async ({ expect }) => {

expect(result).toEqual(expected);
});

it('if-then', async ({ expect }) => {
const schema = {
$schema: 'http:https://json-schema.org/draft-07/schema#',
type: 'object',
properties: {
type: {
type: 'string',
enum: ['css', 'js', 'i18n', 'json'],
},
locale: {
type: 'string',
},
},
if: {
properties: {
type: {
const: 'i18n',
},
},
},
then: {
required: ['locale'],
},
};
const result = await convert(schema);

const expected = {
type: 'object',
properties: {
type: {
type: 'string',
enum: ['css', 'js', 'i18n', 'json'],
},
locale: {
type: 'string',
},
},
oneOf: [
{
allOf: [
{
properties: {
type: {
enum: ['i18n'],
},
},
},
{
required: ['locale'],
},
],
},
{
allOf: [
{
not: {
properties: {
type: {
enum: ['i18n'],
},
},
},
},
],
},
],
};

expect(result).toEqual(expected);
});