Skip to content

Commit

Permalink
feat(jex): replace any by unknown (#431)
Browse files Browse the repository at this point in the history
  • Loading branch information
franklevasseur authored Oct 16, 2024
1 parent ab418c7 commit a60a924
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 25 deletions.
2 changes: 1 addition & 1 deletion jex/src/builders/jexir-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function _literal(value: string | number | boolean) {

export type jexirBuilder = typeof jexirBuilder
export const jexirBuilder = {
any: () => ({ type: 'any' }),
unknown: () => ({ type: 'unknown' }),
string: () => ({ type: 'string' }),
number: () => ({ type: 'number' }),
boolean: () => ({ type: 'boolean' }),
Expand Down
4 changes: 2 additions & 2 deletions jex/src/builders/json-schema-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const INTEGER = { type: 'integer' } satisfies JSONSchema7
const BOOLEAN = { type: 'boolean' } satisfies JSONSchema7
const NULL = { type: 'null' } satisfies JSONSchema7
const UNDEFINED = { not: {} } satisfies JSONSchema7
const ANY = {} satisfies JSONSchema7
const UNKNOWN = {} satisfies JSONSchema7

export type JsonSchemaBuilder = typeof jsonSchemaBuilder
export const jsonSchemaBuilder = {
Expand All @@ -28,7 +28,7 @@ export const jsonSchemaBuilder = {
boolean: () => BOOLEAN,
null: () => NULL,
undefined: () => UNDEFINED,
any: () => ANY,
unknown: () => UNKNOWN,
object: <K extends string>(
properties: Record<K, JSONSchema7>,
required: NoInfer<K>[] = Object.keys(properties) as K[]
Expand Down
14 changes: 9 additions & 5 deletions jex/src/jex-extends.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ const expectJex = (typeA: jexir.JexIR) => ({
}
})

// any extends string, string extends any, any extends any
test('jex-extends should be true if child or parent is any', () => {
expectJex($.any()).toExtend($.any())
expectJex($.string()).toExtend($.any())
expectJex($.any()).toExtend($.string())
// string extends unknown, unknown extends unknown
test('jex-extends should be true if child or parent is unknown', () => {
expectJex($.unknown()).toExtend($.unknown())
expectJex($.string()).toExtend($.unknown())
})

// unknown does not extend string
test('jex-extends should be false if child is unknown and parent is not', () => {
expectJex($.unknown()).not.toExtend($.string())
})

// string extends string, { a: string } extends { a: string }, etc..
Expand Down
9 changes: 7 additions & 2 deletions jex/src/jex-extends.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,13 @@ const _primitiveExtends = <T extends jexir.JexIRPrimitive>(
}

const _jexExtends = (path: PropertyPath, typeA: jexir.JexIR, typeB: jexir.JexIR): _JexExtensionResult => {
if (typeB.type === 'any' || typeA.type === 'any') {
return { result: true }
if (typeB.type === 'unknown') {
return { result: true } // everything extends unknown
}

if (typeA.type === 'unknown') {
// nothing extends unknown except unknown itself
return { result: false, reasons: [{ path, typeA, typeB }] }
}

if (typeA.type === 'union') {
Expand Down
4 changes: 2 additions & 2 deletions jex/src/jexir/from-json-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ test('JexIR should model map types', async () => {
})
})

test('JexIR should model any type', async () => {
await expectZod($.any()).toEqualJex({ type: 'any' })
test('JexIR should model unknown type', async () => {
await expectZod($.unknown()).toEqualJex({ type: 'unknown' })
})

test('JexIR should model tuple types', async () => {
Expand Down
10 changes: 5 additions & 5 deletions jex/src/jexir/from-json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const _toInternalRep = (schema: JSONSchema7): types.JexIR => {
if (schema.not !== undefined) {
if (schema.not === true) {
return {
type: 'any'
type: 'unknown'
}
}

Expand All @@ -82,7 +82,7 @@ const _toInternalRep = (schema: JSONSchema7): types.JexIR => {
}

const not = _toInternalRep(schema.not)
if (not.type === 'any') {
if (not.type === 'unknown') {
return {
type: 'undefined'
}
Expand Down Expand Up @@ -119,7 +119,7 @@ const _toInternalRep = (schema: JSONSchema7): types.JexIR => {
if (schema.items === undefined) {
return {
type: 'array',
items: { type: 'any' }
items: { type: 'unknown' }
}
}

Expand Down Expand Up @@ -149,7 +149,7 @@ const _toInternalRep = (schema: JSONSchema7): types.JexIR => {
if (schema.additionalProperties === true) {
return {
type: 'map',
items: { type: 'any' }
items: { type: 'unknown' }
}
}
if (schema.additionalProperties === false) {
Expand Down Expand Up @@ -208,7 +208,7 @@ const _toInternalRep = (schema: JSONSchema7): types.JexIR => {
}
}

return { type: 'any' }
return { type: 'unknown' }
}

export const fromJsonSchema = async (schema: JSONSchema7): Promise<types.JexIR> => {
Expand Down
6 changes: 3 additions & 3 deletions jex/src/jexir/to-json-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ test('JexIR of map types should map to json-schema', () => {
})
})

// JexIR of any type should map to json-schema
// JexIR of unknown type should map to json-schema

test('JexIR of any type should map to json-schema', () => {
expectJex($.any()).toEqualJsonSchema({})
test('JexIR of unknown type should map to json-schema', () => {
expectJex($.unknown()).toEqualJsonSchema({})
})

// JexIR of tuple types should map to json-schema
Expand Down
2 changes: 1 addition & 1 deletion jex/src/jexir/to-string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const expectJex = (jex: JexIR) => ({
})

test('jexir toString should correctly convert jexir schema to string representation', () => {
expectJex($.any()).toStringifyAs('any')
expectJex($.unknown()).toStringifyAs('unknown')
expectJex($.undefined()).toStringifyAs('undefined')
expectJex($.null()).toStringifyAs('null')
expectJex($.string()).toStringifyAs('string')
Expand Down
2 changes: 1 addition & 1 deletion jex/src/jexir/to-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ export const toString = (jexirSchema: types.JexIR): string => {
.join(', ')} }`
}

return 'any'
return 'unknown'
}
6 changes: 3 additions & 3 deletions jex/src/jexir/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ export type JexIRMap = {
items: JexIR
}

export type JexIRAny = {
type: 'any'
export type JexIRUnknown = {
type: 'unknown'
}

export type JexIRTuple = {
Expand All @@ -85,5 +85,5 @@ export type JexIR =
| JexIRObject
| JexIRArray
| JexIRMap
| JexIRAny
| JexIRUnknown
| JexIRTuple

0 comments on commit a60a924

Please sign in to comment.