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

Improve ts validator #422

Merged
merged 4 commits into from
Jun 19, 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
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@
"@typescript-eslint/no-explicit-any": "error",
// https://github.com/typescript-eslint/typescript-eslint/issues/2421- closed, but not resolved
"@typescript-eslint/no-unnecessary-condition": "off",
"@typescript-eslint/consistent-type-imports": "error",
"no-unused-vars": "off",
"no-undef": "off"
}
Expand Down
12 changes: 8 additions & 4 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import GridXhrModel from './grid/models/GridXhrModel';
import PureGridComponent from './grid/PureGridComponent';
import AbstractListModel from './list/AbstractListModel';
import ListXhrModel from './list/ListXhrModel';
import DeprecatedValidator from './validation/DeprecatedValidator';
import booleanValidationRule from './validation/rules/boolean';
import dateValidationRule from './validation/rules/date';
import enumValidationRule from './validation/rules/enum';
Expand All @@ -44,16 +45,19 @@ import notNullValidationRule from './validation/rules/notNull';
import regExpValidationRule from './validation/rules/regExp';
import setValidationRule from './validation/rules/set';
import ValidationErrors from './validation/ValidationErrors';
import Validator from './validation/Validator';
import ValidatorBuilder from './validation/ValidatorBuilder';

const UIKernel = {
applyGridFilters,
Grid: Component,
PureGrid: PureGridComponent,
Form: FormService,
connectForm,
createValidator: Validator.create,
Validator,
/**
* @deprecated use ValidatorBuilder instead
*/
createValidator: DeprecatedValidator.create,
ValidatorBuilder,
ValidationErrors,
Models: {
Grid: {
Expand Down Expand Up @@ -96,7 +100,7 @@ const UIKernel = {
regExp: regExpValidationRule,
notNull: notNullValidationRule,
/**
* @deprecated use integer insted
* @deprecated use integer instead
*/
number: integerValidationRule,
integer: integerValidationRule,
Expand Down
2 changes: 1 addition & 1 deletion src/common/EventsModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* LICENSE file in the root directory of this source tree.
*/

import {EventListener, IObservable} from './types';
import type {EventListener, IObservable} from './types';

type Subscribers<TListenerArgsByEventName extends Record<string, unknown[]>> = {
[K in keyof TListenerArgsByEventName & string]?: EventListener<TListenerArgsByEventName[K]>[];
Expand Down
2 changes: 1 addition & 1 deletion src/common/assert.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {IsNever} from 'ts-essentials';
import type {IsNever} from 'ts-essentials';
import ArgumentsError from './error/ArgumentsError';

type Assert = (value: unknown, message?: string) => asserts value;
Expand Down
2 changes: 1 addition & 1 deletion src/common/asyncServerRouteHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* LICENSE file in the root directory of this source tree.
*/

import {Request, RequestHandler, Response} from 'express';
import type {Request, RequestHandler, Response} from 'express';

function asyncServerRouteHandler(handler: (req: Request, res: Response) => Promise<void>): RequestHandler {
return (req, res, next) => {
Expand Down
5 changes: 3 additions & 2 deletions src/common/defaultXhr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
* LICENSE file in the root directory of this source tree.
*/

import {StrictOmit} from 'ts-essentials';
import xhr, {XhrResponse, XhrUriConfig, XhrUrlConfig} from 'xhr';
import type {StrictOmit} from 'ts-essentials';
import type {XhrResponse, XhrUriConfig, XhrUrlConfig} from 'xhr';
import xhr from 'xhr';
import RequestError from './error/RequestError';
import variables from './variables';

Expand Down
2 changes: 1 addition & 1 deletion src/common/mock-utils/createMockInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* LICENSE file in the root directory of this source tree.
*/

import {Newable} from 'ts-essentials';
import type {Newable} from 'ts-essentials';
import createMockedMethod from './createMockedMethod';

function createMockInstance<TClass>(Schema: Newable<TClass>): jest.Mocked<TClass> {
Expand Down
2 changes: 1 addition & 1 deletion src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import omitBy from 'lodash/omitBy';
import pick from 'lodash/pick';
import EqualMap from './EqualMap';
import type EqualMap from './EqualMap';

/**
* Check if two arrays intersection exists
Expand Down
2 changes: 1 addition & 1 deletion src/common/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* LICENSE file in the root directory of this source tree.
*/

import {DefaultXhr} from './defaultXhr';
import type {DefaultXhr} from './defaultXhr';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const collection: Record<string, unknown> = {};
Expand Down
2 changes: 1 addition & 1 deletion src/editors/Number.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

import React from 'react';
import {StrictOmit} from 'ts-essentials';
import type {StrictOmit} from 'ts-essentials';
import {assertNonNullish} from '../common/assert';
import {isEqual} from '../common/utils';
import floatValidator from '../validation/rules/float';
Expand Down
2 changes: 1 addition & 1 deletion src/editors/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import omit from 'lodash/omit';
import React from 'react';
import {StrictOmit} from 'ts-essentials';
import type {StrictOmit} from 'ts-essentials';
import {isEqual} from '../common/utils';

type AdvancedOptions<TValue> = [
Expand Down
4 changes: 2 additions & 2 deletions src/editors/SuggestBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
import omit from 'lodash/omit';
import React from 'react';
import {findDOMNode} from 'react-dom';
import {StrictOmit} from 'ts-essentials';
import type {StrictOmit} from 'ts-essentials';
import {assertNonNullish} from '../common/assert';
import ThrottleError from '../common/error/ThrottleError';
import throttle from '../common/throttle';
import {isEqual, parents} from '../common/utils';
import {IListModel, ListModelReadResult} from '../list/types/IListModel';
import type {IListModel, ListModelReadResult} from '../list/types/IListModel';
import Portal from '../portal/Portal';

const PRODUCT_ID = '__suggestBoxPopUp';
Expand Down
4 changes: 2 additions & 2 deletions src/form/AbstractFormModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
*/

import EventsModel from '../common/EventsModel';
import {IObservable, EventListener} from '../common/types';
import type {IObservable, EventListener} from '../common/types';
import ValidationErrors from '../validation/ValidationErrors';
import {IFormModel} from './types/IFormModel';
import type {IFormModel} from './types/IFormModel';

abstract class AbstractFormModel<
TRecord extends Record<string, unknown>,
Expand Down
7 changes: 4 additions & 3 deletions src/form/FormExpressApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
* LICENSE file in the root directory of this source tree.
*/

import {Request, RequestHandler, Response, Router} from 'express';
import type {Request, RequestHandler, Response} from 'express';
import {Router} from 'express';
import multer from 'multer';
import {assertNonNullish} from '../common/assert';
import asyncServerRouteHandler from '../common/asyncServerRouteHandler';
import parseJson from '../common/parseJson';
import ValidationErrors from '../validation/ValidationErrors';
import {IFormModel} from './types/IFormModel';
import {JsonFormApiResult} from './types/JsonFormApiResult';
import type {IFormModel} from './types/IFormModel';
import type {JsonFormApiResult} from './types/JsonFormApiResult';

const DEFAULT_MAX_FILE_SIZE = 104857600; // 100 MB

Expand Down
17 changes: 9 additions & 8 deletions src/form/FormModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
* LICENSE file in the root directory of this source tree.
*/

import {IObservable} from '../common/types';
import ValidationErrors from '../validation/ValidationErrors';
import Validator from '../validation/Validator';
import type {IObservable} from '../common/types';
import type {IValidator} from '../validation/types/IValidator';
import type ValidationErrors from '../validation/ValidationErrors';
import ValidatorBuilder from '../validation/ValidatorBuilder';
import AbstractFormModel from './AbstractFormModel';
import {FormModelListenerArgsByEventName} from './types/FormModelListenerArgsByEventName';
import {IFormModel} from './types/IFormModel';
import type {FormModelListenerArgsByEventName} from './types/FormModelListenerArgsByEventName';
import type {IFormModel} from './types/IFormModel';

/**
* Simple form model
Expand All @@ -22,12 +23,12 @@ class FormModel<TRecord extends Record<string, unknown>>
{
static create<TRecord extends Record<string, unknown>>(
defaultValues?: Partial<TRecord>,
validator?: Validator<TRecord>
validator?: IValidator<TRecord>
): FormModel<TRecord> {
return new FormModel(validator ?? new Validator(), defaultValues ?? {});
return new FormModel(validator ?? ValidatorBuilder.createEmptyValidator(), defaultValues ?? {});
}

constructor(private validator: Validator<TRecord>, private data: Partial<TRecord>) {
constructor(private validator: IValidator<TRecord>, private data: Partial<TRecord>) {
super();
}

Expand Down
16 changes: 9 additions & 7 deletions src/form/FormService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,28 @@
import ThrottleError from '../common/error/ThrottleError';
import EventEmitter from '../common/EventsModel';
import throttle from '../common/throttle';
import {EventListener, IObservable} from '../common/types';
import type {EventListener, IObservable} from '../common/types';
import {getRecordChanges, keys, parseValueFromEvent, isEmpty, isEqual, warn} from '../common/utils';
import type {IValidator} from '../validation/types/IValidator';
import ValidationErrors from '../validation/ValidationErrors';
import Validator from '../validation/Validator';
import {FormModelListenerArgsByEventName} from './types/FormModelListenerArgsByEventName';
import {IFormModel} from './types/IFormModel';
import IFormService, {
import ValidatorBuilder from '../validation/ValidatorBuilder';
import type {FormModelListenerArgsByEventName} from './types/FormModelListenerArgsByEventName';
import type {IFormModel} from './types/IFormModel';
import type {
FormServiceEmptyState,
FormServiceParams,
FormServiceState,
FormServiceStateFields,
FormServiceListenerArgsByEventName
} from './types/IFormService';
import type IFormService from './types/IFormService';

type InitalizedState<TRecord extends Record<string, unknown>> =
| {
data: Partial<TRecord>;
initialized: true;
model: IFormModel<TRecord> & IObservable<FormModelListenerArgsByEventName<TRecord>>;
warningsValidator: Validator<TRecord>;
warningsValidator: IValidator<TRecord>;
}
| {
data: undefined;
Expand Down Expand Up @@ -74,7 +76,7 @@ class FormService<TRecord extends Record<string, unknown>, TAvailableField exten
model,
data,
changes = {},
warningsValidator = new Validator(),
warningsValidator = ValidatorBuilder.createEmptyValidator(),
partialErrorChecking = false,
submitAll = false
}: FormServiceParams<TRecord, TAvailableField, FormModelListenerArgsByEventName<TRecord>>): Promise<void> {
Expand Down
20 changes: 11 additions & 9 deletions src/form/FormXhrModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@

import url from 'url';
import {assertNonNullish} from '../common/assert';
import defaultXhr, {DefaultXhr} from '../common/defaultXhr';
import type {DefaultXhr} from '../common/defaultXhr';
import defaultXhr from '../common/defaultXhr';
import EventsModel from '../common/EventsModel';
import parseJson from '../common/parseJson';
import {EventListener, IObservable} from '../common/types';
import type {EventListener, IObservable} from '../common/types';
import type {IValidator} from '../validation/types/IValidator';
import ValidationErrors from '../validation/ValidationErrors';
import Validator from '../validation/Validator';
import {FormModelListenerArgsByEventName} from './types/FormModelListenerArgsByEventName';
import {IFormModel} from './types/IFormModel';
import {JsonFormApiResult} from './types/JsonFormApiResult';
import ValidatorBuilder from '../validation/ValidatorBuilder';
import type {FormModelListenerArgsByEventName} from './types/FormModelListenerArgsByEventName';
import type {IFormModel} from './types/IFormModel';
import type {JsonFormApiResult} from './types/JsonFormApiResult';

const MAX_URI_LENGTH = 2048;

Expand All @@ -25,23 +27,23 @@ type FormXhrModelParams<TRecord extends Record<string, unknown>> = {
eventsModel?: EventsModel<FormModelListenerArgsByEventName<TRecord>>;
multipartFormData?: boolean;
validateOnClient?: boolean;
validator?: Validator<TRecord>;
validator?: IValidator<TRecord>;
xhr?: DefaultXhr;
};

class FormXhrModel<TRecord extends Record<string, unknown>>
implements IFormModel<TRecord>, IObservable<FormModelListenerArgsByEventName<TRecord>>
{
private multipartFormDataEncoded: boolean;
private validator: Validator<TRecord>;
private validator: IValidator<TRecord>;
private validateOnClient: boolean;
private xhr: DefaultXhr;
private apiURL: string;
private eventsModel: EventsModel<FormModelListenerArgsByEventName<TRecord>>;

constructor(settings: FormXhrModelParams<TRecord>) {
this.multipartFormDataEncoded = settings.multipartFormData ?? false;
this.validator = settings.validator ?? new Validator();
this.validator = settings.validator ?? ValidatorBuilder.createEmptyValidator();
this.validateOnClient = settings.validateOnClient ?? false;
this.xhr = settings.xhr ?? defaultXhr;
this.eventsModel = settings.eventsModel ?? new EventsModel();
Expand Down
8 changes: 4 additions & 4 deletions src/form/adapters/GridToFormCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
*/

import pick from 'lodash/pick';
import {IObservable} from '../../common/types';
import {IGridModel} from '../../grid/models/types/IGridModel';
import ValidationErrors from '../../validation/ValidationErrors';
import {IFormModel} from '../types/IFormModel';
import type {IObservable} from '../../common/types';
import type {IGridModel} from '../../grid/models/types/IGridModel';
import type ValidationErrors from '../../validation/ValidationErrors';
import type {IFormModel} from '../types/IFormModel';

/**
* Adapter allows to use Grid model as a model for new form record creation
Expand Down
10 changes: 5 additions & 5 deletions src/form/adapters/GridToFormUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
*/

import {assertNonNullish} from '../../common/assert';
import {EventListener, IObservable} from '../../common/types';
import type {EventListener, IObservable} from '../../common/types';
import {isEqual} from '../../common/utils';
import {GridModelListenerArgsByEventName} from '../../grid/models/types/GridModelListenerArgsByEventName';
import {IGridModel} from '../../grid/models/types/IGridModel';
import type {GridModelListenerArgsByEventName} from '../../grid/models/types/GridModelListenerArgsByEventName';
import type {IGridModel} from '../../grid/models/types/IGridModel';
import ValidationErrors from '../../validation/ValidationErrors';
import {FormModelListenerArgsByEventName} from '../types/FormModelListenerArgsByEventName';
import {IFormModel} from '../types/IFormModel';
import type {FormModelListenerArgsByEventName} from '../types/FormModelListenerArgsByEventName';
import type {IFormModel} from '../types/IFormModel';

/**
* Adapter that allows us to use Grid model record as a form model
Expand Down
4 changes: 2 additions & 2 deletions src/form/mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import throttle from '../common/throttle';
import toPromise from '../common/toPromise';
import {parseValueFromEvent, getRecordChanges, forEach, isEmpty, isEqual} from '../common/utils';
import ValidationErrors from '../validation/ValidationErrors';
import Validator from '../validation/Validator';
import ValidatorBuilder from '../validation/ValidatorBuilder';

/**
* Grid form mixin
Expand Down Expand Up @@ -546,7 +546,7 @@ const FormMixin = {
pendingClearErrors: [],
submitting: false,
showDependentFields: settings.showDependentFields || false,
warningsValidator: settings.warningsValidator || new Validator(),
warningsValidator: settings.warningsValidator || ValidatorBuilder.createEmptyValidator(),

partialErrorChecking: settings.partialErrorChecking, // Current mode
partialErrorCheckingDefault: settings.partialErrorChecking, // Default mode
Expand Down
2 changes: 1 addition & 1 deletion src/form/types/IFormModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* LICENSE file in the root directory of this source tree.
*/

import ValidationErrors from '../../validation/ValidationErrors';
import type ValidationErrors from '../../validation/ValidationErrors';

export interface IFormModel<TRecord extends Record<string, unknown>> {
/**
Expand Down
Loading