Skip to content

Commit 38f589a

Browse files
committed
chore(schema-validation): update validation actions and reducers
1 parent 637948e commit 38f589a

File tree

9 files changed

+385
-457
lines changed

9 files changed

+385
-457
lines changed

packages/compass-schema-validation/src/modules/edit-mode.ts

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
import type { AnyAction } from 'redux';
21
import type { RootAction } from '.';
2+
import { isAction } from '../util';
33
import {
4-
SET_VALIDATION_TO_DEFAULT,
5-
type SetValidationToDefaultAction,
4+
type EmptyValidationFetchedAction,
5+
ValidationActions,
6+
type ValidationCanceledAction,
7+
type ValidationFetchedAction,
68
} from './validation';
79

8-
export function isAction<A extends AnyAction>(
9-
action: AnyAction,
10-
type: A['type']
11-
): action is A {
12-
return action.type === type;
13-
}
14-
1510
/**
1611
* The edit mode changed action.
1712
*/
@@ -84,11 +79,32 @@ export default function reducer(
8479
}
8580

8681
if (
87-
isAction<SetValidationToDefaultAction>(action, SET_VALIDATION_TO_DEFAULT)
82+
isAction<EmptyValidationFetchedAction>(
83+
action,
84+
ValidationActions.EmptyValidationFetched
85+
)
8886
) {
8987
return { ...state, isEditingEnabledByUser: true };
9088
}
9189

90+
if (
91+
isAction<ValidationFetchedAction>(
92+
action,
93+
ValidationActions.ValidationFetched
94+
)
95+
) {
96+
return { ...state, isEditingEnabledByUser: false };
97+
}
98+
99+
if (
100+
isAction<ValidationCanceledAction>(
101+
action,
102+
ValidationActions.ValidationCanceled
103+
)
104+
) {
105+
return { ...state, isEditingEnabledByUser: false };
106+
}
107+
92108
return state;
93109
}
94110

packages/compass-schema-validation/src/modules/is-loaded.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
import type { RootAction } from '.';
2+
import { isAction } from '../util';
3+
import { ValidationActions } from './validation';
4+
import type {
5+
EmptyValidationFetchedAction,
6+
ValidationFetchedAction,
7+
ValidationFetchErroredAction,
8+
} from './validation';
29

310
export const IS_LOADED_CHANGED =
411
'validation/namespace/IS_LOADED_CHANGED' as const;
@@ -16,6 +23,23 @@ export default function reducer(
1623
state: IsLoadedState = INITIAL_STATE,
1724
action: RootAction
1825
): IsLoadedState {
26+
if (
27+
isAction<ValidationFetchedAction>(
28+
action,
29+
ValidationActions.ValidationFetched
30+
) ||
31+
isAction<EmptyValidationFetchedAction>(
32+
action,
33+
ValidationActions.EmptyValidationFetched
34+
) ||
35+
isAction<ValidationFetchErroredAction>(
36+
action,
37+
ValidationActions.ValidationFetchErrored
38+
)
39+
) {
40+
return true;
41+
}
42+
1943
if (action.type === IS_LOADED_CHANGED) {
2044
return action.isLoaded;
2145
}

packages/compass-schema-validation/src/modules/rules-generation.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,9 @@ import type { SchemaValidationThunkAction } from '.';
22
import { zeroStateChanged } from './zero-state';
33
import { enableEditRules } from './edit-mode';
44
import type { MongoError } from 'mongodb';
5-
import type { Action, AnyAction, Reducer } from 'redux';
5+
import type { Action, Reducer } from 'redux';
66
import { validationLevelChanged, validatorChanged } from './validation';
7-
8-
export function isAction<A extends AnyAction>(
9-
action: AnyAction,
10-
type: A['type']
11-
): action is A {
12-
return action.type === type;
13-
}
14-
15-
export type ValidationServerAction = 'error' | 'warn';
16-
export type ValidationLevel = 'off' | 'moderate' | 'strict';
7+
import { isAction } from '../util';
178

189
const ERROR_CODE_MAX_TIME_MS_EXPIRED = 50;
1910

packages/compass-schema-validation/src/modules/sample-documents.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import type { PreferencesAccess } from 'compass-preferences-model';
2-
import { checkValidator } from './validation';
2+
import {
3+
checkValidator,
4+
ValidationActions,
5+
type ValidationCanceledAction,
6+
} from './validation';
37
import type { DataService } from '@mongodb-js/compass-connections/provider';
48
import type { RootAction, SchemaValidationThunkAction } from '.';
9+
import { isAction } from '../util';
510

611
export const SAMPLE_SIZE = 10000;
712

@@ -195,6 +200,15 @@ export default function (
195200
} else if (action.type === FETCHING_SAMPLE_DOCUMENTS)
196201
fn = startFetchingSampleDocuments;
197202

203+
if (
204+
isAction<ValidationCanceledAction>(
205+
action,
206+
ValidationActions.ValidationCanceled
207+
)
208+
) {
209+
fn = clearingSampleDocuments;
210+
}
211+
198212
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
199213
// @ts-ignore-error TS does not understand that action can be passed to fn
200214
return fn ? fn(state, action) : state;

packages/compass-schema-validation/src/modules/validation.spec.ts

Lines changed: 4 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,7 @@ import reducer, {
77
validationLevelChanged,
88
validatorChanged,
99
validationFetched,
10-
validationCanceled,
1110
validationSaveFailed,
12-
VALIDATOR_CHANGED,
13-
VALIDATION_CANCELED,
14-
VALIDATION_SAVE_FAILED,
15-
VALIDATION_FETCHED,
16-
VALIDATION_ACTION_CHANGED,
17-
VALIDATION_LEVEL_CHANGED,
1811
} from './validation';
1912

2013
describe('validation module', function () {
@@ -29,93 +22,6 @@ describe('validation module', function () {
2922
});
3023
});
3124

32-
describe('#validationActionChanged', function () {
33-
it('returns the VALIDATION_ACTION_CHANGED action', function () {
34-
expect(validationActionChanged('warn')).to.deep.equal({
35-
type: VALIDATION_ACTION_CHANGED,
36-
validationAction: 'warn',
37-
});
38-
});
39-
});
40-
41-
describe('#validationLevelChanged', function () {
42-
it('returns the VALIDATION_LEVEL_CHANGED action', function () {
43-
expect(validationLevelChanged('moderate')).to.deep.equal({
44-
type: VALIDATION_LEVEL_CHANGED,
45-
validationLevel: 'moderate',
46-
});
47-
});
48-
});
49-
50-
describe('#validatorChanged', function () {
51-
it('returns the VALIDATOR_CHANGED action', function () {
52-
expect(
53-
validatorChanged(
54-
"{ $jsonSchema: { bsonType: 'object', required: [ 'name' ] } }"
55-
)
56-
).to.deep.equal({
57-
type: VALIDATOR_CHANGED,
58-
validator:
59-
"{ $jsonSchema: { bsonType: 'object', required: [ 'name' ] } }",
60-
});
61-
});
62-
});
63-
64-
describe('#validationFetched', function () {
65-
it('returns the VALIDATION_FETCHED action', function () {
66-
expect(
67-
validationFetched({
68-
validator: '{ name: { $exists: true } }',
69-
validationAction: 'warn',
70-
validationLevel: 'off',
71-
})
72-
).to.deep.equal({
73-
type: VALIDATION_FETCHED,
74-
validation: {
75-
validator: '{ name: { $exists: true } }',
76-
validationAction: 'warn',
77-
validationLevel: 'off',
78-
},
79-
});
80-
});
81-
});
82-
83-
describe('#validationCanceled', function () {
84-
it('returns the VALIDATION_CANCELED action', function () {
85-
expect(
86-
validationCanceled({
87-
isChanged: false,
88-
validator: '{ name: { $exists: true } }',
89-
validationAction: 'warn',
90-
validationLevel: 'off',
91-
error: null,
92-
})
93-
).to.deep.equal({
94-
type: VALIDATION_CANCELED,
95-
validation: {
96-
isChanged: false,
97-
validator: '{ name: { $exists: true } }',
98-
validationAction: 'warn',
99-
validationLevel: 'off',
100-
error: null,
101-
},
102-
});
103-
});
104-
});
105-
106-
describe('#validationSaveFailed', function () {
107-
it('returns the VALIDATION_SAVE_FAILED action', function () {
108-
expect(
109-
validationSaveFailed({
110-
message: 'Validation save failed!',
111-
})
112-
).to.deep.equal({
113-
type: VALIDATION_SAVE_FAILED,
114-
error: { message: 'Validation save failed!' },
115-
});
116-
});
117-
});
118-
11925
describe('#reducer', function () {
12026
context(
12127
'when the action is not presented in validation module',
@@ -209,7 +115,7 @@ describe('validation module', function () {
209115
const validation = reducer(
210116
undefined,
211117
validationFetched({
212-
validator: '{ name: { $exists: true } }',
118+
validator: { name: { $exists: true } },
213119
validationAction: 'warn',
214120
validationLevel: 'off',
215121
})
@@ -252,7 +158,9 @@ describe('validation module', function () {
252158
validationLevel: 'strict',
253159
isChanged: false,
254160
syntaxError: null,
255-
error: { message: 'Validation save failed!' },
161+
error: {
162+
message: 'Validation save failed!',
163+
},
256164
});
257165
});
258166
});

0 commit comments

Comments
 (0)