Skip to content

Commit

Permalink
fix: make name required on field types (#337)
Browse files Browse the repository at this point in the history
* fix: make name required on field types

* fix: improve typescript types
  • Loading branch information
DanRibbens authored Oct 11, 2021
1 parent d0259ce commit b257e01
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/admin/components/forms/field-types/Code/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React, { Suspense, lazy } from 'react';
import Loading from '../../../elements/Loading';
import { Props } from './types';

const Code = lazy(() => import('./Code'));

const CodeField: React.FC = (props) => (
const CodeField: React.FC<Props> = (props) => (
<Suspense fallback={<Loading />}>
<Code {...props} />
</Suspense>
Expand Down
4 changes: 2 additions & 2 deletions src/admin/components/forms/withCondition/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const withCondition = <P extends Record<string, unknown>>(Field: React.Component
admin: {
condition,
} = {},
} = props as FieldBase;
} = props as Partial<FieldBase>;

if (condition) {
return <WithCondition {...props} />;
Expand All @@ -24,7 +24,7 @@ const withCondition = <P extends Record<string, unknown>>(Field: React.Component
admin: {
condition,
} = {},
} = props as FieldBase & {
} = props as Partial<FieldBase> & {
path?: string
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ describe('Cell Types', () => {
describe('Select', () => {
const fieldWithOptionsObject: SelectField = {
type: 'select',
name: 'selectObject',
options: [{
value: 'one',
label: 'One',
Expand All @@ -129,6 +130,7 @@ describe('Cell Types', () => {
};
const fieldWithStringsOptions: SelectField = {
type: 'select',
name: 'selectString',
options: ['blue', 'green', 'yellow'],
};
it('renders options objects', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/collections/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default function registerCollections(ctx: Payload): void {
};

// eslint-disable-next-line func-names
schema.methods.incLoginAttempts = function (this: mongoose.Document<any> & LoginSchema, cb) {
schema.methods.incLoginAttempts = function (this: mongoose.Document & LoginSchema, cb) {
// Expired lock, restart count at 1
if (this.lockUntil && this.lockUntil < Date.now()) {
return this.updateOne({
Expand All @@ -49,7 +49,7 @@ export default function registerCollections(ctx: Payload): void {
if (this.loginAttempts + 1 >= maxLoginAttempts && !this.isLocked) {
updates.$set = { lockUntil: Date.now() + lockTime };
}
return this.updateOne(updates, cb);
return this.updateOne(updates as mongoose.Document, cb);
};

// eslint-disable-next-line func-names
Expand Down
3 changes: 2 additions & 1 deletion src/fields/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export type OptionObject = {
export type Option = OptionObject | string

export interface FieldBase {
name?: string;
name: string;
label?: string | false;
required?: boolean;
unique?: boolean;
Expand Down Expand Up @@ -146,6 +146,7 @@ export type RowAdmin = Omit<Admin, 'description'> & {
};

export type RowField = Omit<FieldBase, 'admin'> & {
name?: string;
admin?: RowAdmin;
type: 'row';
fields: Field[];
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import express, { Express, Router } from 'express';
import crypto from 'crypto';
import { Document, Model } from 'mongoose';
import { Document } from 'mongoose';
import {
SanitizedConfig,
EmailOptions,
Expand Down Expand Up @@ -36,13 +36,13 @@ import { encrypt, decrypt } from './auth/crypto';
import { BuildEmailResult, Message } from './email/types';
import { PayloadRequest } from './express/types';
import sendEmail from './email/sendEmail';
import { Preferences } from './preferences/types';

import { Options as CreateOptions } from './collections/operations/local/create';
import { Options as FindOptions } from './collections/operations/local/find';
import { Options as FindByIDOptions } from './collections/operations/local/findByID';
import { Options as UpdateOptions } from './collections/operations/local/update';
import { Options as DeleteOptions } from './collections/operations/local/delete';
import { Preference } from './preferences/types';

require('isomorphic-fetch');

Expand All @@ -58,7 +58,7 @@ export class Payload {
resolvers: GraphQLResolvers
};

preferences: { Model: Model<Document<Preference>> };
preferences: Preferences;

globals: Globals;

Expand Down
3 changes: 2 additions & 1 deletion src/preferences/model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import mongoose, { Schema } from 'mongoose';
import { Preference } from './types';

const Model = mongoose.model('_preferences', new Schema({
const Model = mongoose.model<Preference>('_preferences', new Schema({
user: {
type: Schema.Types.ObjectId,
refPath: 'userCollection',
Expand Down
5 changes: 5 additions & 0 deletions src/preferences/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Request } from 'express';
import { Model } from 'mongoose';
import { User } from '../auth';

export type Preference = {
Expand All @@ -10,6 +11,10 @@ export type Preference = {
updatedAt?: Date;
};

export type Preferences = {
Model: Model<Preference>
}

export type PreferenceRequest = {
overrideAccess?: boolean;
req: Request;
Expand Down

0 comments on commit b257e01

Please sign in to comment.