From e7ba0b97f3ddf53bd312be64fb85a73e7c576892 Mon Sep 17 00:00:00 2001 From: Allan Garcez <22946697+g4rcez@users.noreply.github.com> Date: Wed, 26 Oct 2022 06:36:38 -0300 Subject: [PATCH] feat: add improvements in Typescript API (#196) * feat: add improvements in Typescript API * feat: duplicate tests to use with/without type inference * fix: pr issues about spaces and {} type --- types/FluentJSONSchema.d.ts | 42 ++++++++++++------------ types/FluentJSONSchema.test-d.ts | 55 +++++++++++++++++++++++++++++++- 2 files changed, 75 insertions(+), 22 deletions(-) diff --git a/types/FluentJSONSchema.d.ts b/types/FluentJSONSchema.d.ts index 2d5ec35..fba1db0 100644 --- a/types/FluentJSONSchema.d.ts +++ b/types/FluentJSONSchema.d.ts @@ -114,20 +114,20 @@ export interface ArraySchema extends BaseSchema { maxItems: (max: number) => ArraySchema } -export interface ObjectSchema extends BaseSchema { - definition: (name: string, props?: JSONSchema) => ObjectSchema - prop: (name: string, 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 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 @@ -222,23 +222,23 @@ interface DependenciesOptions { [key: string]: JSONSchema[] } -interface DependentSchemaOptions { - [key: string]: JSONSchema -} +type Key = keyof T | (string & {}) -interface DependentRequiredOptions { - [key: string]: string[] -} +type DependentSchemaOptions>> = Partial> + +type DependentRequiredOptions>> = Partial> export function withOptions(options: SchemaOptions): T +type ObjectPlaceholder = Record; + export interface S extends BaseSchema { string: () => StringSchema number: () => NumberSchema integer: () => IntegerSchema boolean: () => BooleanSchema array: () => ArraySchema - object: () => ObjectSchema + object: () => ObjectSchema null: () => NullSchema mixed: < T extends diff --git a/types/FluentJSONSchema.test-d.ts b/types/FluentJSONSchema.test-d.ts index 1ca639b..7d3ef79 100644 --- a/types/FluentJSONSchema.test-d.ts +++ b/types/FluentJSONSchema.test-d.ts @@ -124,7 +124,7 @@ const dependentSchemas = S.object() .dependentSchemas({ foo: S.object().prop('bar'), }) - .prop('foo', S.object().prop('bar')) + .prop('bar', S.object().prop('bar')) .valueOf() console.log('dependentRequired:\n', JSON.stringify(dependentSchemas)) @@ -135,3 +135,56 @@ const deprecatedSchema = S.object() .valueOf() console.log('deprecatedSchema:\n', JSON.stringify(deprecatedSchema)) + +type Foo = { + foo: string + bar: string +} + +const dependentRequiredWithType = S.object() + .dependentRequired({ + foo: ['bar'], + }) + .prop('foo') + .prop('bar') + .valueOf() + +console.log('dependentRequired:\n', JSON.stringify(dependentRequiredWithType)) + +const dependentSchemasWithType = S.object() + .dependentSchemas({ + foo: S.object().prop('bar'), + }) + .prop('bar', S.object().prop('bar')) + .valueOf() + +console.log('dependentSchemasWithType:\n', JSON.stringify(dependentSchemasWithType)) + +const deprecatedSchemaWithType = S.object() + .deprecated() + .prop('foo', S.string().deprecated()) + .valueOf() + +console.log('deprecatedSchemaWithType:\n', JSON.stringify(deprecatedSchemaWithType)) + +type ReallyLongType = { + foo: string + bar: string + baz: string + xpto: string + abcd: number + kct: { + a: string + b: number + d: null + } +} + +const deepTestOnTypes = S.object() + .prop('bar', S.object().prop('bar')) + // you can provide any string, to avoid breaking changes + .prop('aaaa', S.anyOf([S.string()])) + .definition('abcd', S.number()) + .valueOf() + +console.log('deepTestOnTypes:\n', JSON.stringify(deepTestOnTypes))