Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add improvements in Typescript API #196

Merged
merged 3 commits into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

g4rcez marked this conversation as resolved.
Show resolved Hide resolved
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>
g4rcez marked this conversation as resolved.
Show resolved Hide resolved
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>()
g4rcez marked this conversation as resolved.
Show resolved Hide resolved
.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))