From 6aae40c06352f4037eb249cb701bd289144a8c4b Mon Sep 17 00:00:00 2001 From: cesarvspr Date: Thu, 27 Oct 2022 00:02:35 -0400 Subject: [PATCH 1/7] Fix: nodenext resolution #198 --- src/ObjectSchema.js | 5 +- src/ObjectSchema.test.js | 2 +- types/FluentJSONSchema.d.ts | 309 +++++++++++++++++++----------------- 3 files changed, 163 insertions(+), 153 deletions(-) diff --git a/src/ObjectSchema.js b/src/ObjectSchema.js index dc94852..a709361 100644 --- a/src/ObjectSchema.js +++ b/src/ObjectSchema.js @@ -45,10 +45,11 @@ const ObjectSchema = ({ schema = initialState, ...options } = {}) => { * @param {string} id - an #id **/ id: id => { - if (!id) + if (!id) { throw new FluentSchemaError( - `id should not be an empty fragment <#> or an empty string <> (e.g. #myId)` + 'id should not be an empty fragment <#> or an empty string <> (e.g. #myId)' ) + } return options.factory({ schema: { ...schema, $id: id }, ...options }) }, /** diff --git a/src/ObjectSchema.test.js b/src/ObjectSchema.test.js index 0d3e6bb..0140121 100644 --- a/src/ObjectSchema.test.js +++ b/src/ObjectSchema.test.js @@ -229,7 +229,7 @@ describe('ObjectSchema', () => { .valueOf() ).toEqual({ $id: id, - properties: {'prop': {}}, + properties: { prop: {} }, type: 'object' }) }) diff --git a/types/FluentJSONSchema.d.ts b/types/FluentJSONSchema.d.ts index fba1db0..97782df 100644 --- a/types/FluentJSONSchema.d.ts +++ b/types/FluentJSONSchema.d.ts @@ -1,59 +1,59 @@ -export interface BaseSchema { - id: (id: string) => T - title: (title: string) => T - description: (description: string) => T - examples: (examples: Array) => T - ref: (ref: string) => T - enum: (values: Array) => T - const: (value: any) => T - default: (value: any) => T - required: (fields?: string[]) => T - ifThen: (ifClause: JSONSchema, thenClause: JSONSchema) => T +interface BaseSchema { + id: (id: string) => T; + title: (title: string) => T; + description: (description: string) => T; + examples: (examples: Array) => T; + ref: (ref: string) => T; + enum: (values: Array) => T; + const: (value: any) => T; + default: (value: any) => T; + required: (fields?: string[]) => T; + ifThen: (ifClause: JSONSchema, thenClause: JSONSchema) => T; ifThenElse: ( ifClause: JSONSchema, thenClause: JSONSchema, elseClause: JSONSchema - ) => T - not: (schema: JSONSchema) => T - anyOf: (schema: Array) => T - allOf: (schema: Array) => T - oneOf: (schema: Array) => T - readOnly: (isReadOnly?: boolean) => T - writeOnly: (isWriteOnly?: boolean) => T - deprecated: (isDeprecated?: boolean) => T - isFluentSchema: boolean - isFluentJSONSchema: boolean - raw: (fragment: any) => T + ) => T; + not: (schema: JSONSchema) => T; + anyOf: (schema: Array) => T; + allOf: (schema: Array) => T; + oneOf: (schema: Array) => T; + readOnly: (isReadOnly?: boolean) => T; + writeOnly: (isWriteOnly?: boolean) => T; + deprecated: (isDeprecated?: boolean) => T; + isFluentSchema: boolean; + isFluentJSONSchema: boolean; + raw: (fragment: any) => T; } -export type TYPE = - | 'string' - | 'number' - | 'boolean' - | 'integer' - | 'object' - | 'array' - | 'null' +type TYPE = + | "string" + | "number" + | "boolean" + | "integer" + | "object" + | "array" + | "null"; type FORMATS = { - RELATIVE_JSON_POINTER: 'relative-json-pointer' - JSON_POINTER: 'json-pointer' - UUID: 'uuid' - REGEX: 'regex' - IPV6: 'ipv6' - IPV4: 'ipv4' - HOSTNAME: 'hostname' - EMAIL: 'email' - URL: 'url' - URI_TEMPLATE: 'uri-template' - URI_REFERENCE: 'uri-reference' - URI: 'uri' - TIME: 'time' - DATE: 'date' - DATE_TIME: 'date-time' -} + RELATIVE_JSON_POINTER: "relative-json-pointer"; + JSON_POINTER: "json-pointer"; + UUID: "uuid"; + REGEX: "regex"; + IPV6: "ipv6"; + IPV4: "ipv4"; + HOSTNAME: "hostname"; + EMAIL: "email"; + URL: "url"; + URI_TEMPLATE: "uri-template"; + URI_REFERENCE: "uri-reference"; + URI: "uri"; + TIME: "time"; + DATE: "date"; + DATE_TIME: "date-time"; +}; -export type JSONSchema = +type JSONSchema = | ObjectSchema | StringSchema | NumberSchema @@ -61,112 +61,109 @@ export type JSONSchema = | IntegerSchema | BooleanSchema | NullSchema - | ExtendedSchema - -export class FluentSchemaError extends Error { - name: string -} + | ExtendedSchema; -export interface SchemaOptions { - schema: object - generateIds: boolean +interface SchemaOptions { + schema: object; + generateIds: boolean; } -export interface StringSchema extends BaseSchema { - minLength: (min: number) => StringSchema - maxLength: (min: number) => StringSchema - format: (format: FORMATS[keyof FORMATS]) => StringSchema - pattern: (pattern: string | RegExp) => StringSchema - contentEncoding: (encoding: string) => StringSchema - contentMediaType: (mediaType: string) => StringSchema +interface StringSchema extends BaseSchema { + minLength: (min: number) => StringSchema; + maxLength: (min: number) => StringSchema; + format: (format: FORMATS[keyof FORMATS]) => StringSchema; + pattern: (pattern: string | RegExp) => StringSchema; + contentEncoding: (encoding: string) => StringSchema; + contentMediaType: (mediaType: string) => StringSchema; } -export interface NullSchema { - null: () => StringSchema +interface NullSchema { + null: () => StringSchema; } -export interface BooleanSchema extends BaseSchema { - boolean: () => BooleanSchema +interface BooleanSchema extends BaseSchema { + boolean: () => BooleanSchema; } -export interface NumberSchema extends BaseSchema { - minimum: (min: number) => NumberSchema - exclusiveMinimum: (min: number) => NumberSchema - maximum: (max: number) => NumberSchema - exclusiveMaximum: (max: number) => NumberSchema - multipleOf: (multiple: number) => NumberSchema +interface NumberSchema extends BaseSchema { + minimum: (min: number) => NumberSchema; + exclusiveMinimum: (min: number) => NumberSchema; + maximum: (max: number) => NumberSchema; + exclusiveMaximum: (max: number) => NumberSchema; + multipleOf: (multiple: number) => NumberSchema; } -export interface IntegerSchema extends BaseSchema { - minimum: (min: number) => IntegerSchema - exclusiveMinimum: (min: number) => IntegerSchema - maximum: (max: number) => IntegerSchema - exclusiveMaximum: (max: number) => IntegerSchema - multipleOf: (multiple: number) => IntegerSchema +interface IntegerSchema extends BaseSchema { + minimum: (min: number) => IntegerSchema; + exclusiveMinimum: (min: number) => IntegerSchema; + maximum: (max: number) => IntegerSchema; + exclusiveMaximum: (max: number) => IntegerSchema; + multipleOf: (multiple: number) => IntegerSchema; } -export interface ArraySchema extends BaseSchema { - items: (items: JSONSchema | Array) => ArraySchema - additionalItems: (items: Array | boolean) => ArraySchema - contains: (value: JSONSchema | boolean) => ArraySchema - uniqueItems: (boolean: boolean) => ArraySchema - minItems: (min: number) => ArraySchema - maxItems: (max: number) => ArraySchema +interface ArraySchema extends BaseSchema { + items: (items: JSONSchema | Array) => ArraySchema; + additionalItems: (items: Array | boolean) => ArraySchema; + contains: (value: JSONSchema | boolean) => ArraySchema; + uniqueItems: (boolean: boolean) => ArraySchema; + minItems: (min: number) => ArraySchema; + maxItems: (max: number) => ArraySchema; } -export interface ObjectSchema = Record> extends BaseSchema> { - definition: (name: Key, props?: JSONSchema) => ObjectSchema - prop: (name: Key, props?: JSONSchema) => ObjectSchema - additionalProperties: (value: JSONSchema | boolean) => ObjectSchema - maxProperties: (max: number) => ObjectSchema - minProperties: (min: number) => ObjectSchema - patternProperties: (options: PatternPropertiesOptions) => ObjectSchema - dependencies: (options: DependenciesOptions) => ObjectSchema - propertyNames: (value: JSONSchema) => ObjectSchema - extend: (schema: ObjectSchema | ExtendedSchema) => ExtendedSchema - only: (properties: string[]) => ObjectSchema - without: (properties: string[]) => ObjectSchema - dependentRequired: (options: DependentRequiredOptions) => ObjectSchema - dependentSchemas: (options: DependentSchemaOptions) => ObjectSchema +interface ObjectSchema = Record> + extends BaseSchema> { + definition: (name: Key, props?: JSONSchema) => ObjectSchema; + prop: (name: Key, props?: JSONSchema) => ObjectSchema; + additionalProperties: (value: JSONSchema | boolean) => ObjectSchema; + maxProperties: (max: number) => ObjectSchema; + minProperties: (min: number) => ObjectSchema; + patternProperties: (options: PatternPropertiesOptions) => ObjectSchema; + dependencies: (options: DependenciesOptions) => ObjectSchema; + propertyNames: (value: JSONSchema) => ObjectSchema; + extend: (schema: ObjectSchema | ExtendedSchema) => ExtendedSchema; + only: (properties: string[]) => ObjectSchema; + without: (properties: string[]) => ObjectSchema; + dependentRequired: (options: DependentRequiredOptions) => ObjectSchema; + dependentSchemas: (options: DependentSchemaOptions) => ObjectSchema; } -export type ExtendedSchema = Pick +type ExtendedSchema = Pick; type InferSchemaMap = { - string: StringSchema - number: NumberSchema - boolean: BooleanSchema - integer: IntegerSchema - object: ObjectSchema - array: ArraySchema - null: NullSchema -} + string: StringSchema; + number: NumberSchema; + boolean: BooleanSchema; + integer: IntegerSchema; + object: ObjectSchema; + array: ArraySchema; + null: NullSchema; +}; type MixedSchema1 = T extends [infer U] ? InferSchemaMap[U extends TYPE ? U : never] - : never + : never; type MixedSchema2 = T extends [infer U, infer V] ? InferSchemaMap[U extends TYPE ? U : never] & InferSchemaMap[V extends TYPE ? V : never] - : never + : never; type MixedSchema3 = T extends [infer U, infer V, infer W] ? InferSchemaMap[U extends TYPE ? U : never] & InferSchemaMap[V extends TYPE ? V : never] & InferSchemaMap[W extends TYPE ? W : never] - : never + : never; type MixedSchema4 = T extends [infer U, infer V, infer W, infer X] ? InferSchemaMap[U extends TYPE ? U : never] & InferSchemaMap[V extends TYPE ? V : never] & InferSchemaMap[W extends TYPE ? W : never] & InferSchemaMap[X extends TYPE ? X : never] - : never + : never; type MixedSchema5 = T extends [infer U, infer V, infer W, infer X, infer Y] ? InferSchemaMap[U extends TYPE ? U : never] & InferSchemaMap[V extends TYPE ? V : never] & InferSchemaMap[W extends TYPE ? W : never] & InferSchemaMap[X extends TYPE ? X : never] & InferSchemaMap[Y extends TYPE ? Y : never] - : never + : never; type MixedSchema6 = T extends [ infer U, infer V, @@ -181,7 +178,7 @@ type MixedSchema6 = T extends [ InferSchemaMap[X extends TYPE ? X : never] & InferSchemaMap[Y extends TYPE ? Y : never] & InferSchemaMap[Z extends TYPE ? Z : never] - : never + : never; type MixedSchema7 = T extends [ infer U, infer V, @@ -198,63 +195,75 @@ type MixedSchema7 = T extends [ InferSchemaMap[Y extends TYPE ? Y : never] & InferSchemaMap[Z extends TYPE ? Z : never] & InferSchemaMap[A extends TYPE ? A : never] - : never + : never; -export type MixedSchema = +type MixedSchema = | MixedSchema1 | MixedSchema2 | MixedSchema3 | MixedSchema4 | MixedSchema5 | MixedSchema6 - | MixedSchema7 + | MixedSchema7; interface SchemaOptions { - schema: object - generateIds: boolean + schema: object; + generateIds: boolean; } interface PatternPropertiesOptions { - [key: string]: JSONSchema + [key: string]: JSONSchema; } interface DependenciesOptions { - [key: string]: JSONSchema[] + [key: string]: JSONSchema[]; } -type Key = keyof T | (string & {}) +type Key = keyof T | (string & {}); -type DependentSchemaOptions>> = Partial> +type DependentSchemaOptions>> = + Partial>; -type DependentRequiredOptions>> = Partial> - -export function withOptions(options: SchemaOptions): T +type DependentRequiredOptions>> = + Partial>; type ObjectPlaceholder = Record; -export interface S extends BaseSchema { - string: () => StringSchema - number: () => NumberSchema - integer: () => IntegerSchema - boolean: () => BooleanSchema - array: () => ArraySchema - object: () => ObjectSchema - null: () => NullSchema - mixed: < - T extends - | [TYPE] - | [TYPE, TYPE] - | [TYPE, TYPE, TYPE] - | [TYPE, TYPE, TYPE, TYPE] - | [TYPE, TYPE, TYPE, TYPE, TYPE] - | [TYPE, TYPE, TYPE, TYPE, TYPE, TYPE] - | [TYPE, TYPE, TYPE, TYPE, TYPE, TYPE, TYPE] - >( - types: T - ) => MixedSchema - raw: (fragment: any) => S - FORMATS: FORMATS +declare namespace s { + export class FluentSchemaError extends Error { + name: string; + } + + interface S extends BaseSchema { + string: () => StringSchema; + number: () => NumberSchema; + integer: () => IntegerSchema; + boolean: () => BooleanSchema; + array: () => ArraySchema; + object: < + T extends ObjectPlaceholder = ObjectPlaceholder + >() => ObjectSchema; + null: () => NullSchema; + mixed: < + T extends + | [TYPE] + | [TYPE, TYPE] + | [TYPE, TYPE, TYPE] + | [TYPE, TYPE, TYPE, TYPE] + | [TYPE, TYPE, TYPE, TYPE, TYPE] + | [TYPE, TYPE, TYPE, TYPE, TYPE, TYPE] + | [TYPE, TYPE, TYPE, TYPE, TYPE, TYPE, TYPE] + >( + types: T + ) => MixedSchema; + raw: (fragment: any) => S; + FORMATS: FORMATS; + } + + export function withOptions(options: SchemaOptions): T; + + export const FluentSchema: S; + export { FluentSchema as default }; } -declare var s: S -export default s +export = s; From c8c50d6bd2f7ce7ac46f523e7e86b6db116dc383 Mon Sep 17 00:00:00 2001 From: cesarvspr Date: Thu, 27 Oct 2022 10:06:28 -0400 Subject: [PATCH 2/7] chore: review fixes --- types/FluentJSONSchema.d.ts | 68 ++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/types/FluentJSONSchema.d.ts b/types/FluentJSONSchema.d.ts index 97782df..cb7c6d2 100644 --- a/types/FluentJSONSchema.d.ts +++ b/types/FluentJSONSchema.d.ts @@ -1,31 +1,3 @@ -interface BaseSchema { - id: (id: string) => T; - title: (title: string) => T; - description: (description: string) => T; - examples: (examples: Array) => T; - ref: (ref: string) => T; - enum: (values: Array) => T; - const: (value: any) => T; - default: (value: any) => T; - required: (fields?: string[]) => T; - ifThen: (ifClause: JSONSchema, thenClause: JSONSchema) => T; - ifThenElse: ( - ifClause: JSONSchema, - thenClause: JSONSchema, - elseClause: JSONSchema - ) => T; - not: (schema: JSONSchema) => T; - anyOf: (schema: Array) => T; - allOf: (schema: Array) => T; - oneOf: (schema: Array) => T; - readOnly: (isReadOnly?: boolean) => T; - writeOnly: (isWriteOnly?: boolean) => T; - deprecated: (isDeprecated?: boolean) => T; - isFluentSchema: boolean; - isFluentJSONSchema: boolean; - raw: (fragment: any) => T; -} - type TYPE = | "string" | "number" @@ -68,7 +40,7 @@ interface SchemaOptions { generateIds: boolean; } -interface StringSchema extends BaseSchema { +interface StringSchema extends s.BaseSchema { minLength: (min: number) => StringSchema; maxLength: (min: number) => StringSchema; format: (format: FORMATS[keyof FORMATS]) => StringSchema; @@ -81,11 +53,11 @@ interface NullSchema { null: () => StringSchema; } -interface BooleanSchema extends BaseSchema { +interface BooleanSchema extends s.BaseSchema { boolean: () => BooleanSchema; } -interface NumberSchema extends BaseSchema { +interface NumberSchema extends s.BaseSchema { minimum: (min: number) => NumberSchema; exclusiveMinimum: (min: number) => NumberSchema; maximum: (max: number) => NumberSchema; @@ -93,7 +65,7 @@ interface NumberSchema extends BaseSchema { multipleOf: (multiple: number) => NumberSchema; } -interface IntegerSchema extends BaseSchema { +interface IntegerSchema extends s.BaseSchema { minimum: (min: number) => IntegerSchema; exclusiveMinimum: (min: number) => IntegerSchema; maximum: (max: number) => IntegerSchema; @@ -101,7 +73,7 @@ interface IntegerSchema extends BaseSchema { multipleOf: (multiple: number) => IntegerSchema; } -interface ArraySchema extends BaseSchema { +interface ArraySchema extends s.BaseSchema { items: (items: JSONSchema | Array) => ArraySchema; additionalItems: (items: Array | boolean) => ArraySchema; contains: (value: JSONSchema | boolean) => ArraySchema; @@ -111,7 +83,7 @@ interface ArraySchema extends BaseSchema { } interface ObjectSchema = Record> - extends BaseSchema> { + extends s.BaseSchema> { definition: (name: Key, props?: JSONSchema) => ObjectSchema; prop: (name: Key, props?: JSONSchema) => ObjectSchema; additionalProperties: (value: JSONSchema | boolean) => ObjectSchema; @@ -234,6 +206,34 @@ declare namespace s { name: string; } + export interface BaseSchema { + id: (id: string) => T; + title: (title: string) => T; + description: (description: string) => T; + examples: (examples: Array) => T; + ref: (ref: string) => T; + enum: (values: Array) => T; + const: (value: any) => T; + default: (value: any) => T; + required: (fields?: string[]) => T; + ifThen: (ifClause: JSONSchema, thenClause: JSONSchema) => T; + ifThenElse: ( + ifClause: JSONSchema, + thenClause: JSONSchema, + elseClause: JSONSchema + ) => T; + not: (schema: JSONSchema) => T; + anyOf: (schema: Array) => T; + allOf: (schema: Array) => T; + oneOf: (schema: Array) => T; + readOnly: (isReadOnly?: boolean) => T; + writeOnly: (isWriteOnly?: boolean) => T; + deprecated: (isDeprecated?: boolean) => T; + isFluentSchema: boolean; + isFluentJSONSchema: boolean; + raw: (fragment: any) => T; + } + interface S extends BaseSchema { string: () => StringSchema; number: () => NumberSchema; From d3b1511b600f4967428d70d32d8dd4709f354f48 Mon Sep 17 00:00:00 2001 From: cesarvspr Date: Thu, 27 Oct 2022 11:17:52 -0400 Subject: [PATCH 3/7] chore: add all interfaces inside namespace --- types/FluentJSONSchema.d.ts | 185 ++++++++++++++++++------------------ 1 file changed, 94 insertions(+), 91 deletions(-) diff --git a/types/FluentJSONSchema.d.ts b/types/FluentJSONSchema.d.ts index cb7c6d2..a93bef9 100644 --- a/types/FluentJSONSchema.d.ts +++ b/types/FluentJSONSchema.d.ts @@ -26,89 +26,27 @@ type FORMATS = { }; type JSONSchema = - | ObjectSchema - | StringSchema - | NumberSchema - | ArraySchema - | IntegerSchema - | BooleanSchema - | NullSchema + | s.ObjectSchema + | s.StringSchema + | s.NumberSchema + | s.ArraySchema + | s.IntegerSchema + | s.BooleanSchema + | s.NullSchema | ExtendedSchema; -interface SchemaOptions { - schema: object; - generateIds: boolean; -} - -interface StringSchema extends s.BaseSchema { - minLength: (min: number) => StringSchema; - maxLength: (min: number) => StringSchema; - format: (format: FORMATS[keyof FORMATS]) => StringSchema; - pattern: (pattern: string | RegExp) => StringSchema; - contentEncoding: (encoding: string) => StringSchema; - contentMediaType: (mediaType: string) => StringSchema; -} - -interface NullSchema { - null: () => StringSchema; -} - -interface BooleanSchema extends s.BaseSchema { - boolean: () => BooleanSchema; -} - -interface NumberSchema extends s.BaseSchema { - minimum: (min: number) => NumberSchema; - exclusiveMinimum: (min: number) => NumberSchema; - maximum: (max: number) => NumberSchema; - exclusiveMaximum: (max: number) => NumberSchema; - multipleOf: (multiple: number) => NumberSchema; -} - -interface IntegerSchema extends s.BaseSchema { - minimum: (min: number) => IntegerSchema; - exclusiveMinimum: (min: number) => IntegerSchema; - maximum: (max: number) => IntegerSchema; - exclusiveMaximum: (max: number) => IntegerSchema; - multipleOf: (multiple: number) => IntegerSchema; -} -interface ArraySchema extends s.BaseSchema { - items: (items: JSONSchema | Array) => ArraySchema; - additionalItems: (items: Array | boolean) => ArraySchema; - contains: (value: JSONSchema | boolean) => ArraySchema; - uniqueItems: (boolean: boolean) => ArraySchema; - minItems: (min: number) => ArraySchema; - maxItems: (max: number) => ArraySchema; -} - -interface ObjectSchema = Record> - extends s.BaseSchema> { - definition: (name: Key, props?: JSONSchema) => ObjectSchema; - prop: (name: Key, props?: JSONSchema) => ObjectSchema; - additionalProperties: (value: JSONSchema | boolean) => ObjectSchema; - maxProperties: (max: number) => ObjectSchema; - minProperties: (min: number) => ObjectSchema; - patternProperties: (options: PatternPropertiesOptions) => ObjectSchema; - dependencies: (options: DependenciesOptions) => ObjectSchema; - propertyNames: (value: JSONSchema) => ObjectSchema; - extend: (schema: ObjectSchema | ExtendedSchema) => ExtendedSchema; - only: (properties: string[]) => ObjectSchema; - without: (properties: string[]) => ObjectSchema; - dependentRequired: (options: DependentRequiredOptions) => ObjectSchema; - dependentSchemas: (options: DependentSchemaOptions) => ObjectSchema; -} -type ExtendedSchema = Pick; +type ExtendedSchema = Pick; type InferSchemaMap = { - string: StringSchema; - number: NumberSchema; - boolean: BooleanSchema; - integer: IntegerSchema; - object: ObjectSchema; - array: ArraySchema; - null: NullSchema; + string: s.StringSchema; + number: s.NumberSchema; + boolean: s.BooleanSchema; + integer: s.IntegerSchema; + object: s.ObjectSchema; + array: s.ArraySchema; + null: s.NullSchema; }; type MixedSchema1 = T extends [infer U] @@ -178,19 +116,6 @@ type MixedSchema = | MixedSchema6 | MixedSchema7; -interface SchemaOptions { - schema: object; - generateIds: boolean; -} - -interface PatternPropertiesOptions { - [key: string]: JSONSchema; -} - -interface DependenciesOptions { - [key: string]: JSONSchema[]; -} - type Key = keyof T | (string & {}); type DependentSchemaOptions>> = @@ -233,8 +158,86 @@ declare namespace s { isFluentJSONSchema: boolean; raw: (fragment: any) => T; } + + export interface SchemaOptions { + schema: object; + generateIds: boolean; + } + + export interface PatternPropertiesOptions { + [key: string]: JSONSchema; + } + + export interface DependenciesOptions { + [key: string]: JSONSchema[]; + } + + + export interface SchemaOptions { + schema: object; + generateIds: boolean; + } + + export interface StringSchema extends s.BaseSchema { + minLength: (min: number) => StringSchema; + maxLength: (min: number) => StringSchema; + format: (format: FORMATS[keyof FORMATS]) => StringSchema; + pattern: (pattern: string | RegExp) => StringSchema; + contentEncoding: (encoding: string) => StringSchema; + contentMediaType: (mediaType: string) => StringSchema; + } + + export interface NullSchema { + null: () => StringSchema; + } + + export interface BooleanSchema extends s.BaseSchema { + boolean: () => BooleanSchema; + } + + export interface NumberSchema extends s.BaseSchema { + minimum: (min: number) => NumberSchema; + exclusiveMinimum: (min: number) => NumberSchema; + maximum: (max: number) => NumberSchema; + exclusiveMaximum: (max: number) => NumberSchema; + multipleOf: (multiple: number) => NumberSchema; + } + + export interface IntegerSchema extends s.BaseSchema { + minimum: (min: number) => IntegerSchema; + exclusiveMinimum: (min: number) => IntegerSchema; + maximum: (max: number) => IntegerSchema; + exclusiveMaximum: (max: number) => IntegerSchema; + multipleOf: (multiple: number) => IntegerSchema; + } + + export interface ArraySchema extends s.BaseSchema { + items: (items: JSONSchema | Array) => ArraySchema; + additionalItems: (items: Array | boolean) => ArraySchema; + contains: (value: JSONSchema | boolean) => ArraySchema; + uniqueItems: (boolean: boolean) => ArraySchema; + minItems: (min: number) => ArraySchema; + maxItems: (max: number) => ArraySchema; + } + + export interface ObjectSchema = Record> + extends s.BaseSchema> { + definition: (name: Key, props?: JSONSchema) => ObjectSchema; + prop: (name: Key, props?: JSONSchema) => ObjectSchema; + additionalProperties: (value: JSONSchema | boolean) => ObjectSchema; + maxProperties: (max: number) => ObjectSchema; + minProperties: (min: number) => ObjectSchema; + patternProperties: (options: PatternPropertiesOptions) => ObjectSchema; + dependencies: (options: DependenciesOptions) => ObjectSchema; + propertyNames: (value: JSONSchema) => ObjectSchema; + extend: (schema: ObjectSchema | ExtendedSchema) => ExtendedSchema; + only: (properties: string[]) => ObjectSchema; + without: (properties: string[]) => ObjectSchema; + dependentRequired: (options: DependentRequiredOptions) => ObjectSchema; + dependentSchemas: (options: DependentSchemaOptions) => ObjectSchema; + } - interface S extends BaseSchema { + export interface S extends BaseSchema { string: () => StringSchema; number: () => NumberSchema; integer: () => IntegerSchema; From 65023bcab50efbba34471716cbc94597c91cba9e Mon Sep 17 00:00:00 2001 From: cesarvspr Date: Thu, 27 Oct 2022 20:22:40 -0400 Subject: [PATCH 4/7] fix: export type --- types/FluentJSONSchema.d.ts | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/types/FluentJSONSchema.d.ts b/types/FluentJSONSchema.d.ts index a93bef9..aea0c71 100644 --- a/types/FluentJSONSchema.d.ts +++ b/types/FluentJSONSchema.d.ts @@ -25,18 +25,6 @@ type FORMATS = { DATE_TIME: "date-time"; }; -type JSONSchema = - | s.ObjectSchema - | s.StringSchema - | s.NumberSchema - | s.ArraySchema - | s.IntegerSchema - | s.BooleanSchema - | s.NullSchema - | ExtendedSchema; - - - type ExtendedSchema = Pick; type InferSchemaMap = { @@ -118,8 +106,8 @@ type MixedSchema = type Key = keyof T | (string & {}); -type DependentSchemaOptions>> = - Partial>; +type DependentSchemaOptions>> = + Partial>; type DependentRequiredOptions>> = Partial>; @@ -131,6 +119,16 @@ declare namespace s { name: string; } + export type JSONSchema = + | ObjectSchema + | StringSchema + | NumberSchema + | ArraySchema + | IntegerSchema + | BooleanSchema + | NullSchema + | ExtendedSchema; + export interface BaseSchema { id: (id: string) => T; title: (title: string) => T; From 0361dcf66e2ad08aed2e831e30e30c0b19063b7f Mon Sep 17 00:00:00 2001 From: cesarvspr Date: Fri, 28 Oct 2022 11:09:21 -0400 Subject: [PATCH 5/7] fix: export ExtendedSchema --- types/FluentJSONSchema.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/types/FluentJSONSchema.d.ts b/types/FluentJSONSchema.d.ts index aea0c71..9073b34 100644 --- a/types/FluentJSONSchema.d.ts +++ b/types/FluentJSONSchema.d.ts @@ -25,8 +25,6 @@ type FORMATS = { DATE_TIME: "date-time"; }; -type ExtendedSchema = Pick; - type InferSchemaMap = { string: s.StringSchema; number: s.NumberSchema; @@ -261,6 +259,8 @@ declare namespace s { FORMATS: FORMATS; } + export type ExtendedSchema = Pick; + export function withOptions(options: SchemaOptions): T; export const FluentSchema: S; From f438478a14e4a4e28e34092139c10ff0fdc11a78 Mon Sep 17 00:00:00 2001 From: Uzlopak Date: Mon, 21 Nov 2022 16:53:26 +0100 Subject: [PATCH 6/7] Apply suggestions from code review --- types/FluentJSONSchema.d.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/types/FluentJSONSchema.d.ts b/types/FluentJSONSchema.d.ts index 9073b34..e993678 100644 --- a/types/FluentJSONSchema.d.ts +++ b/types/FluentJSONSchema.d.ts @@ -1,4 +1,4 @@ -type TYPE = +export type TYPE = | "string" | "number" | "boolean" @@ -93,7 +93,7 @@ type MixedSchema7 = T extends [ InferSchemaMap[A extends TYPE ? A : never] : never; -type MixedSchema = +export type MixedSchema = | MixedSchema1 | MixedSchema2 | MixedSchema3 @@ -263,7 +263,7 @@ declare namespace s { export function withOptions(options: SchemaOptions): T; - export const FluentSchema: S; + const FluentSchema: S; export { FluentSchema as default }; } From 9b173045845dfb007b68d67bc92d1d1bb3d4f71f Mon Sep 17 00:00:00 2001 From: Uzlopak Date: Mon, 21 Nov 2022 17:02:55 +0100 Subject: [PATCH 7/7] fix nodenext --- types/FluentJSONSchema.d.ts | 278 ++++++++++++++++++------------------ 1 file changed, 139 insertions(+), 139 deletions(-) diff --git a/types/FluentJSONSchema.d.ts b/types/FluentJSONSchema.d.ts index e993678..f5495cd 100644 --- a/types/FluentJSONSchema.d.ts +++ b/types/FluentJSONSchema.d.ts @@ -1,131 +1,131 @@ -export type TYPE = - | "string" - | "number" - | "boolean" - | "integer" - | "object" - | "array" - | "null"; +declare namespace fluentSchema { + export type TYPE = + | "string" + | "number" + | "boolean" + | "integer" + | "object" + | "array" + | "null"; -type FORMATS = { - RELATIVE_JSON_POINTER: "relative-json-pointer"; - JSON_POINTER: "json-pointer"; - UUID: "uuid"; - REGEX: "regex"; - IPV6: "ipv6"; - IPV4: "ipv4"; - HOSTNAME: "hostname"; - EMAIL: "email"; - URL: "url"; - URI_TEMPLATE: "uri-template"; - URI_REFERENCE: "uri-reference"; - URI: "uri"; - TIME: "time"; - DATE: "date"; - DATE_TIME: "date-time"; -}; + type FORMATS = { + RELATIVE_JSON_POINTER: "relative-json-pointer"; + JSON_POINTER: "json-pointer"; + UUID: "uuid"; + REGEX: "regex"; + IPV6: "ipv6"; + IPV4: "ipv4"; + HOSTNAME: "hostname"; + EMAIL: "email"; + URL: "url"; + URI_TEMPLATE: "uri-template"; + URI_REFERENCE: "uri-reference"; + URI: "uri"; + TIME: "time"; + DATE: "date"; + DATE_TIME: "date-time"; + }; -type InferSchemaMap = { - string: s.StringSchema; - number: s.NumberSchema; - boolean: s.BooleanSchema; - integer: s.IntegerSchema; - object: s.ObjectSchema; - array: s.ArraySchema; - null: s.NullSchema; -}; + type InferSchemaMap = { + string: fluentSchema.StringSchema; + number: fluentSchema.NumberSchema; + boolean: fluentSchema.BooleanSchema; + integer: fluentSchema.IntegerSchema; + object: fluentSchema.ObjectSchema; + array: fluentSchema.ArraySchema; + null: fluentSchema.NullSchema; + }; -type MixedSchema1 = T extends [infer U] - ? InferSchemaMap[U extends TYPE ? U : never] - : never; -type MixedSchema2 = T extends [infer U, infer V] - ? InferSchemaMap[U extends TYPE ? U : never] & - InferSchemaMap[V extends TYPE ? V : never] - : never; -type MixedSchema3 = T extends [infer U, infer V, infer W] - ? InferSchemaMap[U extends TYPE ? U : never] & - InferSchemaMap[V extends TYPE ? V : never] & - InferSchemaMap[W extends TYPE ? W : never] - : never; -type MixedSchema4 = T extends [infer U, infer V, infer W, infer X] - ? InferSchemaMap[U extends TYPE ? U : never] & - InferSchemaMap[V extends TYPE ? V : never] & - InferSchemaMap[W extends TYPE ? W : never] & - InferSchemaMap[X extends TYPE ? X : never] - : never; -type MixedSchema5 = T extends [infer U, infer V, infer W, infer X, infer Y] - ? InferSchemaMap[U extends TYPE ? U : never] & - InferSchemaMap[V extends TYPE ? V : never] & - InferSchemaMap[W extends TYPE ? W : never] & - InferSchemaMap[X extends TYPE ? X : never] & - InferSchemaMap[Y extends TYPE ? Y : never] - : never; -type MixedSchema6 = T extends [ - infer U, - infer V, - infer W, - infer X, - infer Y, - infer Z -] - ? InferSchemaMap[U extends TYPE ? U : never] & - InferSchemaMap[V extends TYPE ? V : never] & - InferSchemaMap[W extends TYPE ? W : never] & - InferSchemaMap[X extends TYPE ? X : never] & - InferSchemaMap[Y extends TYPE ? Y : never] & - InferSchemaMap[Z extends TYPE ? Z : never] - : never; -type MixedSchema7 = T extends [ - infer U, - infer V, - infer W, - infer X, - infer Y, - infer Z, - infer A -] - ? InferSchemaMap[U extends TYPE ? U : never] & - InferSchemaMap[V extends TYPE ? V : never] & - InferSchemaMap[W extends TYPE ? W : never] & - InferSchemaMap[X extends TYPE ? X : never] & - InferSchemaMap[Y extends TYPE ? Y : never] & - InferSchemaMap[Z extends TYPE ? Z : never] & - InferSchemaMap[A extends TYPE ? A : never] - : never; + type MixedSchema1 = T extends [infer U] + ? InferSchemaMap[U extends TYPE ? U : never] + : never; + type MixedSchema2 = T extends [infer U, infer V] + ? InferSchemaMap[U extends TYPE ? U : never] & + InferSchemaMap[V extends TYPE ? V : never] + : never; + type MixedSchema3 = T extends [infer U, infer V, infer W] + ? InferSchemaMap[U extends TYPE ? U : never] & + InferSchemaMap[V extends TYPE ? V : never] & + InferSchemaMap[W extends TYPE ? W : never] + : never; + type MixedSchema4 = T extends [infer U, infer V, infer W, infer X] + ? InferSchemaMap[U extends TYPE ? U : never] & + InferSchemaMap[V extends TYPE ? V : never] & + InferSchemaMap[W extends TYPE ? W : never] & + InferSchemaMap[X extends TYPE ? X : never] + : never; + type MixedSchema5 = T extends [infer U, infer V, infer W, infer X, infer Y] + ? InferSchemaMap[U extends TYPE ? U : never] & + InferSchemaMap[V extends TYPE ? V : never] & + InferSchemaMap[W extends TYPE ? W : never] & + InferSchemaMap[X extends TYPE ? X : never] & + InferSchemaMap[Y extends TYPE ? Y : never] + : never; + type MixedSchema6 = T extends [ + infer U, + infer V, + infer W, + infer X, + infer Y, + infer Z + ] + ? InferSchemaMap[U extends TYPE ? U : never] & + InferSchemaMap[V extends TYPE ? V : never] & + InferSchemaMap[W extends TYPE ? W : never] & + InferSchemaMap[X extends TYPE ? X : never] & + InferSchemaMap[Y extends TYPE ? Y : never] & + InferSchemaMap[Z extends TYPE ? Z : never] + : never; + type MixedSchema7 = T extends [ + infer U, + infer V, + infer W, + infer X, + infer Y, + infer Z, + infer A + ] + ? InferSchemaMap[U extends TYPE ? U : never] & + InferSchemaMap[V extends TYPE ? V : never] & + InferSchemaMap[W extends TYPE ? W : never] & + InferSchemaMap[X extends TYPE ? X : never] & + InferSchemaMap[Y extends TYPE ? Y : never] & + InferSchemaMap[Z extends TYPE ? Z : never] & + InferSchemaMap[A extends TYPE ? A : never] + : never; -export type MixedSchema = - | MixedSchema1 - | MixedSchema2 - | MixedSchema3 - | MixedSchema4 - | MixedSchema5 - | MixedSchema6 - | MixedSchema7; + export type MixedSchema = + | MixedSchema1 + | MixedSchema2 + | MixedSchema3 + | MixedSchema4 + | MixedSchema5 + | MixedSchema6 + | MixedSchema7; -type Key = keyof T | (string & {}); + type Key = keyof T | (string & {}); -type DependentSchemaOptions>> = - Partial>; + type DependentSchemaOptions>> = + Partial>; -type DependentRequiredOptions>> = - Partial>; + type DependentRequiredOptions>> = + Partial>; -type ObjectPlaceholder = Record; + type ObjectPlaceholder = Record; -declare namespace s { export class FluentSchemaError extends Error { name: string; } export type JSONSchema = - | ObjectSchema - | StringSchema - | NumberSchema - | ArraySchema - | IntegerSchema - | BooleanSchema - | NullSchema - | ExtendedSchema; + | ObjectSchema + | StringSchema + | NumberSchema + | ArraySchema + | IntegerSchema + | BooleanSchema + | NullSchema + | ExtendedSchema; export interface BaseSchema { id: (id: string) => T; @@ -159,22 +159,22 @@ declare namespace s { schema: object; generateIds: boolean; } - + export interface PatternPropertiesOptions { [key: string]: JSONSchema; } - + export interface DependenciesOptions { [key: string]: JSONSchema[]; } - + export interface SchemaOptions { schema: object; generateIds: boolean; } - - export interface StringSchema extends s.BaseSchema { + + export interface StringSchema extends fluentSchema.BaseSchema { minLength: (min: number) => StringSchema; maxLength: (min: number) => StringSchema; format: (format: FORMATS[keyof FORMATS]) => StringSchema; @@ -182,32 +182,32 @@ declare namespace s { contentEncoding: (encoding: string) => StringSchema; contentMediaType: (mediaType: string) => StringSchema; } - + export interface NullSchema { null: () => StringSchema; } - - export interface BooleanSchema extends s.BaseSchema { + + export interface BooleanSchema extends fluentSchema.BaseSchema { boolean: () => BooleanSchema; } - - export interface NumberSchema extends s.BaseSchema { + + export interface NumberSchema extends fluentSchema.BaseSchema { minimum: (min: number) => NumberSchema; exclusiveMinimum: (min: number) => NumberSchema; maximum: (max: number) => NumberSchema; exclusiveMaximum: (max: number) => NumberSchema; multipleOf: (multiple: number) => NumberSchema; } - - export interface IntegerSchema extends s.BaseSchema { + + export interface IntegerSchema extends fluentSchema.BaseSchema { minimum: (min: number) => IntegerSchema; exclusiveMinimum: (min: number) => IntegerSchema; maximum: (max: number) => IntegerSchema; exclusiveMaximum: (max: number) => IntegerSchema; multipleOf: (multiple: number) => IntegerSchema; } - - export interface ArraySchema extends s.BaseSchema { + + export interface ArraySchema extends fluentSchema.BaseSchema { items: (items: JSONSchema | Array) => ArraySchema; additionalItems: (items: Array | boolean) => ArraySchema; contains: (value: JSONSchema | boolean) => ArraySchema; @@ -215,9 +215,9 @@ declare namespace s { minItems: (min: number) => ArraySchema; maxItems: (max: number) => ArraySchema; } - + export interface ObjectSchema = Record> - extends s.BaseSchema> { + extends fluentSchema.BaseSchema> { definition: (name: Key, props?: JSONSchema) => ObjectSchema; prop: (name: Key, props?: JSONSchema) => ObjectSchema; additionalProperties: (value: JSONSchema | boolean) => ObjectSchema; @@ -232,7 +232,7 @@ declare namespace s { dependentRequired: (options: DependentRequiredOptions) => ObjectSchema; dependentSchemas: (options: DependentSchemaOptions) => ObjectSchema; } - + export interface S extends BaseSchema { string: () => StringSchema; number: () => NumberSchema; @@ -245,13 +245,13 @@ declare namespace s { null: () => NullSchema; mixed: < T extends - | [TYPE] - | [TYPE, TYPE] - | [TYPE, TYPE, TYPE] - | [TYPE, TYPE, TYPE, TYPE] - | [TYPE, TYPE, TYPE, TYPE, TYPE] - | [TYPE, TYPE, TYPE, TYPE, TYPE, TYPE] - | [TYPE, TYPE, TYPE, TYPE, TYPE, TYPE, TYPE] + | [TYPE] + | [TYPE, TYPE] + | [TYPE, TYPE, TYPE] + | [TYPE, TYPE, TYPE, TYPE] + | [TYPE, TYPE, TYPE, TYPE, TYPE] + | [TYPE, TYPE, TYPE, TYPE, TYPE, TYPE] + | [TYPE, TYPE, TYPE, TYPE, TYPE, TYPE, TYPE] >( types: T ) => MixedSchema; @@ -267,4 +267,4 @@ declare namespace s { export { FluentSchema as default }; } -export = s; +export = fluentSchema;