Skip to content

Commit

Permalink
feat: defineUntypedSchema and SchemaDefinition (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu authored Aug 26, 2022
1 parent 90e2eb8 commit 806a2a9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { resolveSchema, applyDefaults } from './schema'
export { generateTypes } from './generator/dts'
export { generateMarkdown } from './generator/md'
export { defineUntypedSchema } from './utils'

export * from './types'
5 changes: 4 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ export interface Schema extends TypeDescriptor {
}

export interface InputObject {
[key: string]: any
$schema?: Schema
$resolve?: ResolveFn
$default?: any
[key: string]: any
}

export type InputValue = InputObject | JSValue

export type SchemaDefinition = { [x:string]: JSValue | InputObject | SchemaDefinition }
6 changes: 5 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { pascalCase } from 'scule'
import type { Schema, JSType, TypeDescriptor } from './types'
import type { Schema, JSType, TypeDescriptor, SchemaDefinition } from './types'

export function defineUntypedSchema (options: SchemaDefinition) {
return options
}

export function escapeKey (val: string): string {
return /^\w+$/.test(val) ? val : `"${val}"`
Expand Down
35 changes: 35 additions & 0 deletions test/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,41 @@ describe('transform (jsdoc)', () => {
};"
`)
})

it('support define function', () => {
const result = transform(`
export default defineUntypedSchema({
/**
* @type {'src' | 'root'}
*/
srcDir: 'src',
multiline: {
$resolve(val) {
return val || false
}
}
})
`)
expect(result).toMatchInlineSnapshot(`
"export default defineUntypedSchema({
srcDir: {
$default: 'src',
$schema: {
title: \\"\\",
description: \\"\\",
tags: [],
tsType: \\"'src' | 'root'\\"
}
},
multiline: {
$resolve(val) {
return val || false;
}
}
});"
`)
})
})

function expectCodeToMatch (code: string, pattern: RegExp, expected: any) {
Expand Down

0 comments on commit 806a2a9

Please sign in to comment.