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

Stricter types #1205

Merged
merged 7 commits into from
Jan 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions docs/api-bridges.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ If only a simple or no validation is needed, this bridge is perfectly suited to
The constructor accepts three arguments:

- `schema: GraphQLType` can be any type parsed and extracted from a GraphQL schema.
- `validator: (model: Record<string, any>) => any` a custom validator function that should return a falsy value if no errors are present or information about errors in the model as described in the [custom bridge section](/docs/examples-custom-bridge#validator-definition).
- `extras: Record<string, any> = {}` used to extend the schema generated from GraphQL type with extra field configuration.
- `validator: (model: Record<string, unknown>) => any` a custom validator function that should return a falsy value if no errors are present or information about errors in the model as described in the [custom bridge section](/docs/examples-custom-bridge#validator-definition).
- `extras: Record<string, unknown> = {}` used to extend the schema generated from GraphQL type with extra field configuration.

### Code example

Expand Down
20 changes: 11 additions & 9 deletions packages/uniforms-antd/__tests__/_createContext.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { BaseForm, Context, randomIds } from 'uniforms';
import { SimpleSchemaDefinition } from 'simpl-schema';
import { Context, UnknownObject, randomIds, ChangedMap } from 'uniforms';

import createSchema from './_createSchema';

const randomId = randomIds();

export default function createContext(
schema?: object,
context?: Partial<Context<any>>,
model?: object,
): { context: Context<any> } {
export default function createContext<Model extends UnknownObject>(
schema?: SimpleSchemaDefinition,
context?: Partial<Context<Model>>,
model = {} as Model,
): { context: Context<Model> } {
return {
context: {
changed: false,
changedMap: {},
changedMap: {} as ChangedMap<Model>,
error: null,
model: model ?? {},
model,
name: [],
onChange() {},
onSubmit() {},
Expand All @@ -32,7 +33,8 @@ export default function createContext(
showInlineError: false,
...context?.state,
},
formRef: {} as BaseForm<unknown>,
// @ts-expect-error We don't have a true ref in tests.
formRef: null,
},
};
}
4 changes: 2 additions & 2 deletions packages/uniforms-antd/__tests__/_createSchema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import SimpleSchema from 'simpl-schema';
import SimpleSchema, { SimpleSchemaDefinition } from 'simpl-schema';
import { SimpleSchema2Bridge } from 'uniforms-bridge-simple-schema-2';

export default function createSchema(schema = {}) {
export default function createSchema(schema: SimpleSchemaDefinition = {}) {
return new SimpleSchema2Bridge(new SimpleSchema(schema));
}
2 changes: 1 addition & 1 deletion packages/uniforms-antd/src/AutoField.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import invariant from 'invariant';
import { createAutoField } from 'uniforms';
export { AutoFieldProps } from 'uniforms';
import { createAutoField } from 'uniforms';

import BoolField from './BoolField';
import DateField from './DateField';
Expand Down
3 changes: 2 additions & 1 deletion packages/uniforms-antd/src/HiddenField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type HiddenFieldProps = Override<
inputRef?: Ref<HTMLInputElement>;
name: string;
noDOM?: boolean;
value?: any;
value?: unknown;
}
>;

Expand All @@ -27,6 +27,7 @@ export default function HiddenField({ value, ...rawProps }: HiddenFieldProps) {
readOnly={props.readOnly}
ref={props.inputRef}
type="hidden"
// @ts-expect-error `value` should be serializable.
value={value ?? props.value ?? ''}
{...filterDOMProps(props)}
/>
Expand Down
20 changes: 11 additions & 9 deletions packages/uniforms-bootstrap3/__tests__/_createContext.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { BaseForm, Context, randomIds } from 'uniforms';
import { SimpleSchemaDefinition } from 'simpl-schema';
import { Context, UnknownObject, randomIds, ChangedMap } from 'uniforms';

import createSchema from './_createSchema';

const randomId = randomIds();

export default function createContext(
schema?: object,
context?: Partial<Context<any>>,
model?: object,
): { context: Context<any> } {
export default function createContext<Model extends UnknownObject>(
schema?: SimpleSchemaDefinition,
context?: Partial<Context<Model>>,
model = {} as Model,
): { context: Context<Model> } {
return {
context: {
changed: false,
changedMap: {},
changedMap: {} as ChangedMap<Model>,
error: null,
model: model ?? {},
model,
name: [],
onChange() {},
onSubmit() {},
Expand All @@ -32,7 +33,8 @@ export default function createContext(
showInlineError: false,
...context?.state,
},
formRef: {} as BaseForm<unknown>,
// @ts-expect-error We don't have a true ref in tests.
formRef: null,
},
};
}
4 changes: 2 additions & 2 deletions packages/uniforms-bootstrap3/__tests__/_createSchema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import SimpleSchema from 'simpl-schema';
import SimpleSchema, { SimpleSchemaDefinition } from 'simpl-schema';
import { SimpleSchema2Bridge } from 'uniforms-bridge-simple-schema-2';

export default function createSchema(schema = {}) {
export default function createSchema(schema: SimpleSchemaDefinition = {}) {
return new SimpleSchema2Bridge(new SimpleSchema(schema));
}
2 changes: 1 addition & 1 deletion packages/uniforms-bootstrap3/src/AutoField.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import invariant from 'invariant';
import { createAutoField } from 'uniforms';
export { AutoFieldProps } from 'uniforms';
import { createAutoField } from 'uniforms';

import BoolField from './BoolField';
import DateField from './DateField';
Expand Down
5 changes: 3 additions & 2 deletions packages/uniforms-bootstrap3/src/HiddenField.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React, { HTMLProps, Ref, useEffect } from 'react';
import { filterDOMProps, Override, useField } from 'uniforms';
import { Override, filterDOMProps, useField } from 'uniforms';

export type HiddenFieldProps = Override<
HTMLProps<HTMLInputElement>,
{
inputRef?: Ref<HTMLInputElement>;
name: string;
noDOM?: boolean;
value?: any;
value?: unknown;
}
>;

Expand All @@ -27,6 +27,7 @@ export default function HiddenField({ value, ...rawProps }: HiddenFieldProps) {
readOnly={props.readOnly}
ref={props.inputRef}
type="hidden"
// @ts-expect-error `value` should be serializable.
value={value ?? props.value ?? ''}
{...filterDOMProps(props)}
/>
Expand Down
20 changes: 11 additions & 9 deletions packages/uniforms-bootstrap4/__tests__/_createContext.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { BaseForm, Context, randomIds } from 'uniforms';
import { SimpleSchemaDefinition } from 'simpl-schema';
import { Context, UnknownObject, randomIds, ChangedMap } from 'uniforms';

import createSchema from './_createSchema';

const randomId = randomIds();

export default function createContext(
schema?: object,
context?: Partial<Context<any>>,
model?: object,
): { context: Context<any> } {
export default function createContext<Model extends UnknownObject>(
schema?: SimpleSchemaDefinition,
context?: Partial<Context<Model>>,
model = {} as Model,
): { context: Context<Model> } {
return {
context: {
changed: false,
changedMap: {},
changedMap: {} as ChangedMap<Model>,
error: null,
model: model ?? {},
model,
name: [],
onChange() {},
onSubmit() {},
Expand All @@ -32,7 +33,8 @@ export default function createContext(
showInlineError: false,
...context?.state,
},
formRef: {} as BaseForm<unknown>,
// @ts-expect-error We don't have a true ref in tests.
formRef: null,
},
};
}
4 changes: 2 additions & 2 deletions packages/uniforms-bootstrap4/__tests__/_createSchema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import SimpleSchema from 'simpl-schema';
import SimpleSchema, { SimpleSchemaDefinition } from 'simpl-schema';
import { SimpleSchema2Bridge } from 'uniforms-bridge-simple-schema-2';

export default function createSchema(schema = {}) {
export default function createSchema(schema: SimpleSchemaDefinition = {}) {
return new SimpleSchema2Bridge(new SimpleSchema(schema));
}
2 changes: 1 addition & 1 deletion packages/uniforms-bootstrap4/src/AutoField.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import invariant from 'invariant';
import { createAutoField } from 'uniforms';
export { AutoFieldProps } from 'uniforms';
import { createAutoField } from 'uniforms';

import BoolField from './BoolField';
import DateField from './DateField';
Expand Down
5 changes: 3 additions & 2 deletions packages/uniforms-bootstrap4/src/HiddenField.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React, { HTMLProps, Ref, useEffect } from 'react';
import { filterDOMProps, Override, useField } from 'uniforms';
import { Override, filterDOMProps, useField } from 'uniforms';

export type HiddenFieldProps = Override<
HTMLProps<HTMLInputElement>,
{
inputRef?: Ref<HTMLInputElement>;
name: string;
noDOM?: boolean;
value?: any;
value?: unknown;
}
>;

Expand All @@ -27,6 +27,7 @@ export default function HiddenField({ value, ...rawProps }: HiddenFieldProps) {
readOnly={props.readOnly}
ref={props.inputRef}
type="hidden"
// @ts-expect-error `value` should be serializable.
value={value ?? props.value ?? ''}
{...filterDOMProps(props)}
/>
Expand Down
20 changes: 11 additions & 9 deletions packages/uniforms-bootstrap5/__tests__/_createContext.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { BaseForm, Context, randomIds } from 'uniforms';
import { SimpleSchemaDefinition } from 'simpl-schema';
import { Context, UnknownObject, randomIds, ChangedMap } from 'uniforms';

import createSchema from './_createSchema';

const randomId = randomIds();

export default function createContext(
schema?: object,
context?: Partial<Context<any>>,
model?: object,
): { context: Context<any> } {
export default function createContext<Model extends UnknownObject>(
schema?: SimpleSchemaDefinition,
context?: Partial<Context<Model>>,
model = {} as Model,
): { context: Context<Model> } {
return {
context: {
changed: false,
changedMap: {},
changedMap: {} as ChangedMap<Model>,
error: null,
model: model ?? {},
model,
name: [],
onChange() {},
onSubmit() {},
Expand All @@ -32,7 +33,8 @@ export default function createContext(
showInlineError: false,
...context?.state,
},
formRef: {} as BaseForm<unknown>,
// @ts-expect-error We don't have a true ref in tests.
formRef: null,
},
};
}
4 changes: 2 additions & 2 deletions packages/uniforms-bootstrap5/__tests__/_createSchema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import SimpleSchema from 'simpl-schema';
import SimpleSchema, { SimpleSchemaDefinition } from 'simpl-schema';
import { SimpleSchema2Bridge } from 'uniforms-bridge-simple-schema-2';

export default function createSchema(schema = {}) {
export default function createSchema(schema: SimpleSchemaDefinition = {}) {
return new SimpleSchema2Bridge(new SimpleSchema(schema));
}
2 changes: 1 addition & 1 deletion packages/uniforms-bootstrap5/src/AutoField.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import invariant from 'invariant';
import { createAutoField } from 'uniforms';
export { AutoFieldProps } from 'uniforms';
import { createAutoField } from 'uniforms';

import BoolField from './BoolField';
import DateField from './DateField';
Expand Down
5 changes: 3 additions & 2 deletions packages/uniforms-bootstrap5/src/HiddenField.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React, { HTMLProps, Ref, useEffect } from 'react';
import { filterDOMProps, Override, useField } from 'uniforms';
import { Override, filterDOMProps, useField } from 'uniforms';

export type HiddenFieldProps = Override<
HTMLProps<HTMLInputElement>,
{
inputRef?: Ref<HTMLInputElement>;
name: string;
noDOM?: boolean;
value?: any;
value?: unknown;
}
>;

Expand All @@ -27,6 +27,7 @@ export default function HiddenField({ value, ...rawProps }: HiddenFieldProps) {
readOnly={props.readOnly}
ref={props.inputRef}
type="hidden"
// @ts-expect-error `value` should be serializable.
value={value ?? props.value ?? ''}
{...filterDOMProps(props)}
/>
Expand Down
Loading