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

feat: defineUntypedSchema and SchemaDefinition #46

Merged
merged 3 commits into from
Aug 26, 2022
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
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