Skip to content

Commit

Permalink
test: schema format
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 committed Aug 21, 2024
1 parent e00e659 commit 6e66f1b
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 0 deletions.
169 changes: 169 additions & 0 deletions cypress/e2e/demo-schema/demo.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,32 @@ describe('`getTypesForSchema`', function () {
).to.throw();
});

it("errs with duplicate properties' properties", function () {
const schema = /** @type {import('zodex').SzIntersection} */ ({
type: 'intersection',
left: {
type: 'object',
properties: {
a: {
type: 'string'
}
}
},
right: {
type: 'object',
properties: {
a: {
type: 'number'
}
}
}
});

expect(
() => getTypesForSchema(schema, schema)
).to.throw();
});

it('copies properties from right', function () {
const schema = /** @type {import('zodex').SzIntersection} */ ({
type: 'intersection',
Expand All @@ -122,4 +148,147 @@ describe('`getTypesForSchema`', function () {
unknownKeys: 'strict'
}]);
});

it('copies `defaultValue` to group items', function () {
const schema =
/**
* @type {import('zodex').SzUnion<[
* import('zodex').SzType,
* ...import('zodex').SzType[]
* ]>}
*/ ({
type: 'union',
defaultValue: {},
options: [
{
type: 'object',
properties: {}
},
{
type: 'object',
properties: {},
unknownKeys: 'strict'
}
]
});

expect([...getTypesForSchema(schema, schema)]).to.deep.equal([{
type: 'object',
properties: {},
$defaultValue: {},
$unionGroupID: 1
}, {
type: 'object',
properties: {},
unknownKeys: 'strict',
$defaultValue: {},
$unionGroupID: 1
}]);
});

it('copies `readonly` to group items', function () {
const schema =
/**
* @type {import('zodex').SzUnion<[
* import('zodex').SzType,
* ...import('zodex').SzType[]
* ]>}
*/ ({
type: 'union',
readonly: true,
options: [
{
type: 'object',
properties: {}
},
{
type: 'object',
properties: {},
unknownKeys: 'strict'
}
]
});

expect([...getTypesForSchema(schema, schema)]).to.deep.equal([{
type: 'object',
properties: {},
$readonlyParent: true
}, {
type: 'object',
properties: {},
unknownKeys: 'strict',
$readonlyParent: true
}]);
});

it('copies `description` to group items', function () {
const schema =
/**
* @type {import('zodex').SzUnion<[
* import('zodex').SzType,
* ...import('zodex').SzType[]
* ]>}
*/ ({
type: 'union',
description: 'Union',
options: [
{
description: 'inner1',
type: 'object',
properties: {}
},
{
type: 'object',
properties: {},
unknownKeys: 'strict'
}
]
});

expect([...getTypesForSchema(schema, schema)]).to.deep.equal([{
type: 'object',
properties: {},
description: 'inner1 and Union'
}, {
type: 'object',
properties: {},
unknownKeys: 'strict',
description: 'Union'
}]);
});

it('adds `null` type for `isNullable` group items', function () {
const schema =
/**
* @type {import('zodex').SzUnion<[
* import('zodex').SzType,
* ...import('zodex').SzType[]
* ]>}
*/ ({
type: 'union',
isNullable: true,
options: [
{
type: 'object',
properties: {}
},
{
type: 'object',
properties: {},
unknownKeys: 'strict'
}
]
});

expect([...getTypesForSchema(schema, schema)]).to.deep.equal([{
type: 'object',
properties: {}
}, {
type: 'object',
properties: {},
unknownKeys: 'strict'
}, {
type: 'null'
}]);
});
});
2 changes: 2 additions & 0 deletions src/formats/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ function mergeSchema (leftItem, rightItem) {

newLeftObj[prop] = val && typeof val === 'object'
? copyObject(val)
/* istanbul ignore next -- Guard */
: val;
}
}
Expand All @@ -119,6 +120,7 @@ function mergeSchema (leftItem, rightItem) {
newLeftObj.properties
)[prop] = val && typeof val === 'object'
? copyObject(val)
/* istanbul ignore next -- Guard */
: val;
}

Expand Down

0 comments on commit 6e66f1b

Please sign in to comment.