Skip to content

Commit

Permalink
feat: add improvements in Typescript API (#196)
Browse files Browse the repository at this point in the history
* feat: add improvements in Typescript API

* feat: duplicate tests to use with/without type inference

* fix: pr issues about spaces and {} type
  • Loading branch information
g4rcez committed Oct 26, 2022
1 parent 707e44e commit e7ba0b9
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 22 deletions.
42 changes: 21 additions & 21 deletions types/FluentJSONSchema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,20 @@ export interface ArraySchema extends BaseSchema<ArraySchema> {
maxItems: (max: number) => ArraySchema
}

export interface ObjectSchema extends BaseSchema<ObjectSchema> {
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<T extends Record<string, any> = Record<string, any>> extends BaseSchema<ObjectSchema<T>> {
definition: (name: Key<T>, props?: JSONSchema) => ObjectSchema<T>
prop: (name: Key<T>, props?: JSONSchema) => ObjectSchema<T>
additionalProperties: (value: JSONSchema | boolean) => ObjectSchema<T>
maxProperties: (max: number) => ObjectSchema<T>
minProperties: (min: number) => ObjectSchema<T>
patternProperties: (options: PatternPropertiesOptions) => ObjectSchema<T>
dependencies: (options: DependenciesOptions) => ObjectSchema<T>
propertyNames: (value: JSONSchema) => ObjectSchema<T>
extend: (schema: ObjectSchema<T> | ExtendedSchema) => ExtendedSchema
only: (properties: string[]) => ObjectSchema<T>
without: (properties: string[]) => ObjectSchema<T>
dependentRequired: (options: DependentRequiredOptions<T>) => ObjectSchema<T>
dependentSchemas: (options: DependentSchemaOptions<T>) => ObjectSchema<T>
}

export type ExtendedSchema = Pick<ObjectSchema, 'isFluentSchema' | 'extend'>
Expand Down Expand Up @@ -222,23 +222,23 @@ interface DependenciesOptions {
[key: string]: JSONSchema[]
}

interface DependentSchemaOptions {
[key: string]: JSONSchema
}
type Key<T> = keyof T | (string & {})

interface DependentRequiredOptions {
[key: string]: string[]
}
type DependentSchemaOptions<T extends Partial<Record<string, JSONSchema>>> = Partial<Record<keyof T, JSONSchema>>

type DependentRequiredOptions<T extends Partial<Record<string, string[]>>> = Partial<Record<keyof T, string[]>>

export function withOptions<T>(options: SchemaOptions): T

type ObjectPlaceholder = Record<string | number | symbol, any>;

export interface S extends BaseSchema<S> {
string: () => StringSchema
number: () => NumberSchema
integer: () => IntegerSchema
boolean: () => BooleanSchema
array: () => ArraySchema
object: () => ObjectSchema
object: <T extends ObjectPlaceholder = ObjectPlaceholder>() => ObjectSchema<T>
null: () => NullSchema
mixed: <
T extends
Expand Down
55 changes: 54 additions & 1 deletion types/FluentJSONSchema.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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<Foo>()
.dependentRequired({
foo: ['bar'],
})
.prop('foo')
.prop('bar')
.valueOf()

console.log('dependentRequired:\n', JSON.stringify(dependentRequiredWithType))

const dependentSchemasWithType = S.object<Foo>()
.dependentSchemas({
foo: S.object().prop('bar'),
})
.prop('bar', S.object().prop('bar'))
.valueOf()

console.log('dependentSchemasWithType:\n', JSON.stringify(dependentSchemasWithType))

const deprecatedSchemaWithType = S.object<Foo>()
.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<ReallyLongType>()
.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))

0 comments on commit e7ba0b9

Please sign in to comment.