Skip to content

Commit

Permalink
Fix toTypescript output escaping, fix nullable typing
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastien-de-saint-florent committed Apr 26, 2024
1 parent 4d004bd commit 63f1de4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ npm i @serafin/schema-builder
## Why Schema Builder?

JSON schema is the base of Open API so it's really important for Serafin framework.
JSON Schema is powerfull but it is also verbose.
JSON Schema is powerful but it is also verbose.

On top of a JSON schema, you also have to create the Typescript interface that it represents. If you take in account other schemas and interfaces you have to define (one for the post body, one for the patch body, one for get query parameters, etc.), it starts to be problematic.

Schema builder is here to save you from all this tedious work!

To summarize, this library allows you to programatically create a JSON Schema and its associated typescript type **at the same time**.
To summarize, this library allows you to programmatically create a JSON Schema and its associated typescript type **at the same time**.

## A quick example

Expand Down
12 changes: 6 additions & 6 deletions src/SchemaBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export class SchemaBuilder<T> {
let s: JSONSchema = {
...cloneJSON(schema),
type: nullable ? ["object", "null"] : "object",
properties,
...(Object.keys(properties).length ? { properties } : {}),
...(required.length > 0 ? { required } : {}),
additionalProperties: false,
}
Expand Down Expand Up @@ -145,7 +145,7 @@ export class SchemaBuilder<T> {
*/
static integerSchema<N extends boolean = false>(
schema: Pick<JSONSchema, JSONSchemaNumberProperties> = {},
nullable?: boolean,
nullable?: N,
): N extends true ? SchemaBuilder<number | null> : SchemaBuilder<number> {
let s: JSONSchema = {
...cloneJSON(schema),
Expand All @@ -159,7 +159,7 @@ export class SchemaBuilder<T> {
*/
static booleanSchema<N extends boolean = false>(
schema: Pick<JSONSchema, JSONSchemaBooleanProperties> = {},
nullable?: boolean,
nullable?: N,
): N extends true ? SchemaBuilder<boolean | null> : SchemaBuilder<boolean> {
let s: JSONSchema = {
...cloneJSON(schema),
Expand Down Expand Up @@ -206,7 +206,7 @@ export class SchemaBuilder<T> {
static enumSchema<K extends string | number | boolean | null, N extends boolean = false>(
values: readonly K[],
schema: Pick<JSONSchema, JSONSchemaEnumProperties> = {},
nullable?: boolean,
nullable?: N,
): N extends true ? SchemaBuilder<K | null> : SchemaBuilder<K> {
const types = [] as JSONSchemaTypeName[]
for (let value of values) {
Expand Down Expand Up @@ -237,7 +237,7 @@ export class SchemaBuilder<T> {
static arraySchema<U, N extends boolean = false>(
items: SchemaBuilder<U>,
schema: Pick<JSONSchema, JSONSchemaArrayProperties> = {},
nullable?: boolean,
nullable?: N,
): N extends true ? SchemaBuilder<U[] | null> : SchemaBuilder<U[]> {
let s: JSONSchema = {
...cloneJSON(schema),
Expand Down Expand Up @@ -1103,7 +1103,7 @@ export class SchemaBuilder<T> {
function optionalStringify(obj: any, force = false, prefix = "") {
let result = force || (obj !== undefined && Object.keys(obj).length) ? JSON.stringify(obj) : undefined
result = result ? `${prefix}${result}` : ""
return result.replaceAll("\\\\", "\\") // unescape
return result
}
const o = customizeOutput ?? ((output: string, s: SchemaBuilder<any>) => output)
if (!processNamedSchema && this.schemaObject.title) {
Expand Down

0 comments on commit 63f1de4

Please sign in to comment.