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(jex): ignore additionalProperties false + intersection of union should extend union #449

Merged
merged 4 commits into from
Oct 23, 2024
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
12 changes: 12 additions & 0 deletions jex/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Debug TS File",
"type": "node",
"request": "launch",
"runtimeExecutable": "node",
"runtimeArgs": ["--nolazy", "-r", "ts-node/register/transpile-only"],
"args": ["${file}"],
"cwd": "${workspaceRoot}",
"console": "integratedTerminal",
"skipFiles": ["<node_internals>/**", "node_modules/**"],
"envFile": "${workspaceFolder}/.env"
},
{
"type": "node",
"request": "launch",
Expand Down
2 changes: 1 addition & 1 deletion jex/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bpinternal/jex",
"version": "1.1.0",
"version": "1.1.1",
"description": "JSON-Extends; JSON Schema type checking library",
"main": "dist/index.cjs",
"types": "dist/src/index.d.ts",
Expand Down
8 changes: 8 additions & 0 deletions jex/src/jex-extends.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,11 @@ test('jex-extends should be true when child implements all parent intersection',
const parent = $.intersection([foo, bar])
expectJex(child).toExtend(parent)
})

test('jex-extends should allow intersection of union to extend union', () => {
const typeA = $.string()
const typeB = $.union([$.literal('a'), $.literal('b')])
const child = $.intersection([typeA, typeB])
const parent = typeB
expectJex(child).toExtend(parent)
})
24 changes: 12 additions & 12 deletions jex/src/jex-extends.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,31 +66,31 @@ const _jexExtends = (path: PropertyPath, typeA: jexir.JexIR, typeB: jexir.JexIR)
return { result: false, reasons: [{ path, typeA, typeB }] }
}

if (typeA.type === 'union') {
const extensions = typeA.anyOf.map((c) => _jexExtends(path, c, typeB))
if (typeB.type === 'intersection') {
const extensions = typeB.allOf.map((c) => _jexExtends(path, typeA, c))
const [_, failures] = _splitSuccessFailure(extensions)
if (failures.length === 0) return { result: true } // A union all extends B
if (failures.length === 0) return { result: true } // A extends all of the B intersection
return { result: false, reasons: failures.flatMap((f) => f.reasons) }
}

if (typeB.type === 'union') {
const extensions = typeB.anyOf.map((c) => _jexExtends(path, typeA, c))
if (typeA.type === 'intersection') {
const extensions = typeA.allOf.map((c) => _jexExtends(path, c, typeB))
const [success, failures] = _splitSuccessFailure(extensions)
if (success.length > 0) return { result: true } // A extends at least one of the B union
if (success.length > 0) return { result: true } // At least one of A extends B
return { result: false, reasons: failures.flatMap((f) => f.reasons) }
}

if (typeB.type === 'intersection') {
const extensions = typeB.allOf.map((c) => _jexExtends(path, typeA, c))
if (typeA.type === 'union') {
const extensions = typeA.anyOf.map((c) => _jexExtends(path, c, typeB))
const [_, failures] = _splitSuccessFailure(extensions)
if (failures.length === 0) return { result: true } // A extends all of the B intersection
if (failures.length === 0) return { result: true } // A union all extends B
return { result: false, reasons: failures.flatMap((f) => f.reasons) }
}

if (typeA.type === 'intersection') {
const extensions = typeA.allOf.map((c) => _jexExtends(path, c, typeB))
if (typeB.type === 'union') {
const extensions = typeB.anyOf.map((c) => _jexExtends(path, typeA, c))
const [success, failures] = _splitSuccessFailure(extensions)
if (success.length > 0) return { result: true } // At least one of A extends B
if (success.length > 0) return { result: true } // A extends at least one of the B union
return { result: false, reasons: failures.flatMap((f) => f.reasons) }
}

Expand Down
24 changes: 21 additions & 3 deletions jex/src/jexir/from-json-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ test('JexIR should should throw an error when the JSON schema is unsuported', ()
]
expectJsonSchema(foo({ not: { type: 'number' } })).toFailAt(path)
expectJsonSchema(foo({ oneOf: [] })).toFailAt(path)
expectJsonSchema(foo({ additionalItems: {} })).toFailAt(path)
expectJsonSchema(foo({ patternProperties: {} })).toFailAt(path)
expectJsonSchema(foo({ propertyNames: {} })).toFailAt(path)
expectJsonSchema(foo({ if: {} })).toFailAt(path)
Expand Down Expand Up @@ -200,6 +199,25 @@ test('JexIR should model a object type with both properties and additionalProper
})
})

test('JexIR should not treat additionalProperties false as an intersection with a record', () => {
// { name: string; age: number } ⊈ { name: string; age: number } & Record<string, never>
expectJsonSchema({
type: 'object',
properties: {
name: $.string(),
age: $.number()
},
required: ['name', 'age'],
additionalProperties: false
}).toEqualJex({
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'number' }
}
})
})

test('JexIR should model unknown type', () => {
expectJsonSchema($.unknown()).toEqualJex({ type: 'unknown' })
})
Expand All @@ -211,8 +229,8 @@ test('JexIR should model tuple types', () => {
})
})

test('JexIR should model intersection types', async () => {
await expectJsonSchema($.intersection([$.string(), $.number()])).toEqualJex({
test('JexIR should model intersection types', () => {
expectJsonSchema($.intersection([$.string(), $.number()])).toEqualJex({
type: 'intersection',
allOf: [{ type: 'string' }, { type: 'number' }]
})
Expand Down
39 changes: 26 additions & 13 deletions jex/src/jexir/from-json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,6 @@ const _toInternalRep = (path: JSONSchemaPropertyPath, schema: JSONSchema7Definit
throw new errors.JexUnresolvedReferenceError(path.path)
}

if (schema.additionalItems !== undefined) {
throw new errors.JexUnsuportedJsonSchemaError(path.path, { additionalItems: schema.additionalItems })
}

if (schema.patternProperties !== undefined) {
throw new errors.JexUnsuportedJsonSchemaError(path.path, { patternProperties: schema.patternProperties })
}
Expand Down Expand Up @@ -146,28 +142,45 @@ const _toInternalRep = (path: JSONSchemaPropertyPath, schema: JSONSchema7Definit
}

if (schema.type === 'array') {
if (schema.items === undefined) {
if (schema.additionalItems && schema.items !== undefined) {
return {
type: 'intersection',
allOf: [
_toInternalRep(path, { ...schema, additionalItems: undefined }),
_toInternalRep(path, { ...schema, items: undefined })
]
}
}

if (schema.additionalItems) {
return {
type: 'array',
items: { type: 'unknown' }
items: _toInternalRep(path.key('additionalItems'), schema.additionalItems)
}
}

if (Array.isArray(schema.items)) {
if (schema.items !== undefined) {
if (Array.isArray(schema.items)) {
return {
type: 'tuple',
items: schema.items.map((s, i) => _toInternalRep(path.key('items').index(i), s))
}
}

return {
type: 'tuple',
items: schema.items.map((s, i) => _toInternalRep(path.key('items').index(i), s))
type: 'array',
items: _toInternalRep(path.key('items'), schema.items)
}
}

return {
type: 'array',
items: _toInternalRep(path.key('items'), schema.items)
type: 'tuple',
items: []
}
}

if (schema.type === 'object') {
if (schema.additionalProperties !== undefined && schema.properties !== undefined) {
if (schema.additionalProperties && schema.properties !== undefined) {
return {
type: 'intersection',
allOf: [
Expand All @@ -177,7 +190,7 @@ const _toInternalRep = (path: JSONSchemaPropertyPath, schema: JSONSchema7Definit
}
}

if (schema.additionalProperties !== undefined) {
if (schema.additionalProperties) {
return {
type: 'map',
items: _toInternalRep(path.key('additionalProperties'), schema.additionalProperties)
Expand Down