fork of hey-api/openapi-ts because I need some additional things
1import type { SchemaWithType } from '@hey-api/shared';
2
3// import { $ } from '../../../../py-dsl';
4import type { Ast, IrSchemaToAstOptions } from '../../shared/types';
5
6export function stringToNode({
7 schema,
8}: IrSchemaToAstOptions & {
9 schema: SchemaWithType<'string'>;
10}): Ast {
11 const constraints: Record<string, unknown> = {};
12
13 if (schema.minLength !== undefined) {
14 constraints.min_length = schema.minLength;
15 }
16
17 if (schema.maxLength !== undefined) {
18 constraints.max_length = schema.maxLength;
19 }
20
21 if (schema.pattern !== undefined) {
22 constraints.pattern = schema.pattern;
23 }
24
25 if (schema.description !== undefined) {
26 constraints.description = schema.description;
27 }
28
29 if (typeof schema.const === 'string') {
30 return {
31 // expression: $.expr(`Literal["${schema.const}"]`),
32 fieldConstraints: constraints,
33 hasLazyExpression: false,
34 models: [],
35 // pipes: [],
36 typeAnnotation: `Literal["${schema.const}"]`,
37 };
38 }
39
40 return {
41 // expression: $.expr('str'),
42 fieldConstraints: constraints,
43 hasLazyExpression: false,
44 models: [],
45 // pipes: [],
46 typeAnnotation: 'str',
47 };
48}