Skip to content

Commit

Permalink
feat: add improvements in Typescript API
Browse files Browse the repository at this point in the history
  • Loading branch information
g4rcez committed Oct 8, 2022
1 parent 707e44e commit e1def77
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 24 deletions.
41 changes: 20 additions & 21 deletions types/FluentJSONSchema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,21 @@ 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, never> = Record<string, never>> 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,13 +223,11 @@ 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

Expand All @@ -238,7 +237,7 @@ export interface S extends BaseSchema<S> {
integer: () => IntegerSchema
boolean: () => BooleanSchema
array: () => ArraySchema
object: () => ObjectSchema
object: <T extends {} = {}>() => ObjectSchema<T>
null: () => NullSchema
mixed: <
T extends
Expand Down
33 changes: 30 additions & 3 deletions types/FluentJSONSchema.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ const rawNullableSchema = S.object()

console.log('raw schema with nullable props\n', JSON.stringify(rawNullableSchema))

const dependentRequired = S.object()
type Foo = {
foo: string
bar: string
}

const dependentRequired = S.object<Foo>()
.dependentRequired({
foo: ['bar'],
})
Expand All @@ -120,11 +125,11 @@ const dependentRequired = S.object()

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

const dependentSchemas = S.object()
const dependentSchemas = S.object<Foo>()
.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 +140,25 @@ const deprecatedSchema = S.object()
.valueOf()

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

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 e1def77

Please sign in to comment.