fork of hey-api/openapi-ts because I need some additional things
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at feat/use-query-options 70 lines 1.8 kB view raw
1import { $ } from '../../../py-dsl'; 2import type { PydanticPlugin } from '../types'; 3import type { FieldConstraints } from '../v2/constants'; 4 5type FieldArg = ReturnType<typeof $.expr | typeof $.kwarg | typeof $.literal>; 6 7export function createFieldCall( 8 constraints: FieldConstraints, 9 plugin: PydanticPlugin['Instance'], 10 options?: { 11 /** If true, the field is required. */ 12 required?: boolean; 13 }, 14): ReturnType<typeof $.call> { 15 const field = plugin.external('pydantic.Field'); 16 const args: Array<FieldArg> = []; 17 18 const isRequired = options?.required !== false && constraints.default === undefined; 19 20 // For required fields with no default, use ... as first arg 21 if (isRequired && constraints.default === undefined) { 22 args.push($('...')); 23 } 24 25 // TODO: move to DSL 26 // Add constraint arguments in a consistent order 27 const orderedKeys: Array<keyof FieldConstraints> = [ 28 'default', 29 'default_factory', 30 'alias', 31 'title', 32 'description', 33 'gt', 34 'ge', 35 'lt', 36 'le', 37 'multiple_of', 38 'min_length', 39 'max_length', 40 'pattern', 41 ]; 42 43 for (const key of orderedKeys) { 44 const value = constraints[key]; 45 if (value === undefined) continue; 46 47 // Skip default if we already added ... for required fields 48 if (key === 'default' && isRequired) continue; 49 50 args.push($.kwarg(key, toKwargValue(value))); 51 } 52 53 return $(field).call(...(args as Array<Parameters<typeof $.call>[1]>)); 54} 55 56/** 57 * Converts a constraint value to a kwarg-compatible value. 58 */ 59function toKwargValue(value: unknown): string | number | boolean | null { 60 if ( 61 value === null || 62 typeof value === 'string' || 63 typeof value === 'number' || 64 typeof value === 'boolean' 65 ) { 66 return value; 67 } 68 // For complex types, stringify 69 return String(value); 70}