Skip to content

Commit bca6908

Browse files
authored
Merge pull request #741 from Meteor-Community-Packages/migrate/3.0
Meteor 3.0 compatibility
2 parents cdfe749 + 9342ceb commit bca6908

File tree

15 files changed

+5340
-3791
lines changed

15 files changed

+5340
-3791
lines changed

.github/workflows/lint-test-publish.yml

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@ jobs:
1212
# runs-on: ubuntu-latest
1313
# steps:
1414
# - name: checkout
15-
# uses: actions/checkout@v3
15+
# uses: actions/checkout@v4
1616
#
1717
# - name: setup node
18-
# uses: actions/setup-node@v3
18+
# uses: actions/setup-node@v4
1919
# with:
20-
# node-version: 16
20+
# node-version: 20
2121
#
2222
# - name: cache dependencies
23-
# uses: actions/cache@v3
23+
# uses: actions/cache@v4
2424
# with:
2525
# path: ~/.npm
26-
# key: ${{ runner.os }}-node-16-${{ hashFiles('**/package-lock.json') }}
26+
# key: ${{ runner.os }}-node-20-${{ hashFiles('**/package-lock.json') }}
2727
# restore-keys: |
28-
# ${{ runner.os }}-node-16-
28+
# ${{ runner.os }}-node-20-
2929
#
3030
# - run: cd tests && npm ci && npm run setup && npm run lint
3131

@@ -36,29 +36,28 @@ jobs:
3636
strategy:
3737
matrix:
3838
meteorRelease:
39-
- '2.3'
40-
- '2.14'
39+
- '3.0.2'
4140
# Latest version
4241
steps:
4342
- name: Checkout code
44-
uses: actions/checkout@v3
43+
uses: actions/checkout@v4
4544

4645
- name: Install Node.js
47-
uses: actions/setup-node@v3
46+
uses: actions/setup-node@v4
4847
with:
49-
node-version: 16
48+
node-version: 20
5049

5150
- name: Setup meteor ${{ matrix.meteorRelease }}
5251
uses: meteorengineer/setup-meteor@v1
5352
with:
5453
meteor-release: ${{ matrix.meteorRelease }}
5554

5655
- name: cache dependencies
57-
uses: actions/cache@v3
56+
uses: actions/cache@v4
5857
with:
5958
path: ~/.npm
60-
key: ${{ runner.os }}-node-16-${{ hashFiles('**/package-lock.json') }}
59+
key: ${{ runner.os }}-node-20-${{ hashFiles('**/package-lock.json') }}
6160
restore-keys: |
62-
${{ runner.os }}-node-16-
61+
${{ runner.os }}-node-20-
6362
64-
- run: cd tests && npm ci && npm run setup && npm run test
63+
- run: cd tests && npm ci && npm run setup && npm run test

.versions

Lines changed: 20 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,24 @@
1-
aldeed:simple-schema@1.13.1
2-
allow-deny@1.1.1
3-
babel-compiler@7.10.5
4-
babel-runtime@1.5.1
5-
base64@1.0.12
6-
binary-heap@1.0.11
7-
boilerplate-generator@1.7.2
8-
callback-hook@1.5.1
9-
check@1.3.2
10-
ddp@1.4.1
11-
ddp-client@2.6.1
12-
ddp-common@1.4.0
13-
ddp-server@2.7.0
14-
diff-sequence@1.1.2
15-
dynamic-import@0.7.3
16-
ecmascript@0.16.8
17-
ecmascript-runtime@0.8.1
18-
ecmascript-runtime-client@0.12.1
19-
ecmascript-runtime-server@0.11.0
20-
ejson@1.1.3
21-
fetch@0.1.4
22-
geojson-utils@1.0.11
1+
aldeed:simple-schema@2.0.0
2+
babel-compiler@7.11.0
3+
babel-runtime@1.5.2
4+
core-runtime@1.0.0
5+
dynamic-import@0.7.4
6+
ecmascript@0.16.9
7+
ecmascript-runtime@0.8.2
8+
ecmascript-runtime-client@0.12.2
9+
ecmascript-runtime-server@0.11.1
10+
fetch@0.1.5
2311
http@1.4.4
24-
id-map@1.1.1
25-
inter-process-messaging@0.1.1
26-
lmieulet:meteor-coverage@3.2.0
27-
lmieulet:meteor-legacy-coverage@0.1.0
28-
local-test:aldeed:simple-schema@1.13.1
29-
logging@1.3.3
30-
meteor@1.11.4
12+
inter-process-messaging@0.1.2
13+
local-test:aldeed:simple-schema@2.0.0
14+
meteor@2.0.1
3115
meteortesting:browser-tests@1.4.2
3216
meteortesting:mocha@2.1.0
3317
meteortesting:mocha-core@8.0.1
34-
minimongo@1.9.3
35-
modern-browsers@0.1.10
36-
modules@0.20.0
37-
modules-runtime@0.13.1
38-
mongo@1.16.8
39-
mongo-decimal@0.1.3
40-
mongo-dev-server@1.1.0
41-
mongo-id@1.0.8
42-
npm-mongo@4.17.2
43-
ordered-dict@1.1.0
44-
promise@0.12.2
45-
random@1.2.1
46-
react-fast-refresh@0.2.8
47-
reload@1.3.1
48-
retry@1.1.0
49-
routepolicy@1.1.1
50-
socket-stream-client@0.5.2
51-
tracker@1.3.3
52-
typescript@4.9.5
53-
underscore@1.0.13
54-
url@1.3.2
55-
webapp@1.13.6
56-
webapp-hashing@1.1.1
18+
modern-browsers@0.1.11
19+
modules@0.20.1
20+
modules-runtime@0.13.2
21+
promise@1.0.0
22+
react-fast-refresh@0.2.9
23+
tracker@1.3.4
24+
url@1.3.3

lib/SimpleSchema.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,9 @@ class SimpleSchema {
118118
}
119119

120120
/**
121-
* For Meteor apps, add a reactive dependency on the label
122-
* for a key.
121+
* Add a reactive dependency on the label for a key.
122+
* @param key {string}
123+
* @param tracker {Tracker}
123124
*/
124125
reactiveLabelDependency(key, tracker = this._constructorOptions.tracker) {
125126
if (!key || !tracker) return;
@@ -614,10 +615,21 @@ class SimpleSchema {
614615
return null;
615616
}
616617

618+
/**
619+
* Creates a new unnamed ValidationContext instance
620+
* for this schema.
621+
* @return {ValidationContext}
622+
*/
617623
newContext() {
618624
return new ValidationContext(this);
619625
}
620626

627+
/**
628+
* Creates and stores a new named (scoped) ValidationContext for a given name
629+
* and this schema and returns it.
630+
* @param name {string}
631+
* @return {ValidationContext}
632+
*/
621633
namedContext(name) {
622634
if (typeof name !== 'string') name = 'default';
623635
if (!this._validationContexts[name]) {

lib/ValidationContext.js

Lines changed: 99 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,61 @@
11
import MongoObject from 'mongo-object';
22
import doValidation from './doValidation';
33

4+
/**
5+
* @typedef ValidationError
6+
* @type object
7+
* @property name {string} error name
8+
* @property type {string} error type name
9+
* @property value {string} actuall error message value
10+
*/
11+
12+
/**
13+
* State representation of a validation for
14+
* a given schema.
15+
*
16+
*
17+
*/
418
export default class ValidationContext {
519
/**
620
* @param {SimpleSchema} ss SimpleSchema instance to use for validation
721
* @param {String} [name] Optional context name, accessible on context.name.
822
*/
923
constructor(ss, name) {
1024
this.name = name;
25+
1126
this._simpleSchema = ss;
1227
this._schema = ss.schema();
1328
this._schemaKeys = Object.keys(this._schema);
1429
this._validationErrors = [];
30+
this._deps = {};
1531

1632
// Set up validation dependencies
17-
this._deps = {};
1833
const { tracker } = ss._constructorOptions;
19-
if (tracker) {
34+
this.reactive(tracker);
35+
}
36+
//---------------------------------------------------------------------------
37+
// PUBLIC
38+
//---------------------------------------------------------------------------
39+
40+
/**
41+
* Makes this validation context
42+
* reactive for Meteor-Tracker.
43+
* @param tracker {Tracker}
44+
*/
45+
reactive(tracker) {
46+
if (tracker && Object.keys(this._deps).length === 0) {
2047
this._depsAny = new tracker.Dependency();
2148
this._schemaKeys.forEach((key) => {
2249
this._deps[key] = new tracker.Dependency();
2350
});
2451
}
2552
}
2653

27-
_markKeyChanged(key) {
28-
const genericKey = MongoObject.makeKeyGeneric(key);
29-
if (Object.prototype.hasOwnProperty.call(this._deps, genericKey)) this._deps[genericKey].changed();
30-
}
31-
32-
_markKeysChanged(keys) {
33-
if (!keys || !Array.isArray(keys) || !keys.length) return;
34-
35-
keys.forEach((key) => this._markKeyChanged(key));
36-
37-
this._depsAny && this._depsAny.changed();
38-
}
39-
54+
/**
55+
* Merges existing with a list of new validation errors.
56+
* Reactive.
57+
* @param errors ValidationError[]
58+
*/
4059
setValidationErrors(errors) {
4160
const previousValidationErrors = this._validationErrors.map((o) => o.name);
4261
const newValidationErrors = errors.map((o) => o.name);
@@ -48,6 +67,10 @@ export default class ValidationContext {
4867
this._markKeysChanged(changedKeys);
4968
}
5069

70+
/**
71+
* Adds new validation errors to the list.
72+
* @param errors ValidationError[]
73+
*/
5174
addValidationErrors(errors) {
5275
const newValidationErrors = errors.map((o) => o.name);
5376

@@ -57,11 +80,20 @@ export default class ValidationContext {
5780
this._markKeysChanged(newValidationErrors);
5881
}
5982

60-
// Reset the validationErrors array
83+
/**
84+
* Flushes/empties the list of validation errors.
85+
*/
6186
reset() {
6287
this.setValidationErrors([]);
6388
}
6489

90+
/**
91+
* Returns a validation error for a given key.
92+
* @param key {string} the key of the field to access errors for
93+
* @param genericKey {string} generic version of the key, you usually don't need
94+
* to explcitly call this. If you do, you need to wrap it using `MongoObject.makeKeyGeneric`
95+
* @return {ValidationError|undefined}
96+
*/
6597
getErrorForKey(key, genericKey = MongoObject.makeKeyGeneric(key)) {
6698
const errors = this._validationErrors;
6799
const errorForKey = errors.find((error) => error.name === key);
@@ -70,17 +102,24 @@ export default class ValidationContext {
70102
return errors.find((error) => error.name === genericKey);
71103
}
72104

73-
_keyIsInvalid(key, genericKey) {
74-
return !!this.getErrorForKey(key, genericKey);
75-
}
76-
77-
// Like the internal one, but with deps
105+
/**
106+
* Returns, whether there is an error for a given key. Reactive.
107+
* @param key {string}
108+
* @param genericKey {string}
109+
* @return {boolean}
110+
*/
78111
keyIsInvalid(key, genericKey = MongoObject.makeKeyGeneric(key)) {
79112
if (Object.prototype.hasOwnProperty.call(this._deps, genericKey)) this._deps[genericKey].depend();
80113

81114
return this._keyIsInvalid(key, genericKey);
82115
}
83116

117+
/**
118+
*
119+
* @param key
120+
* @param genericKey
121+
* @return {string|*}
122+
*/
84123
keyErrorMessage(key, genericKey = MongoObject.makeKeyGeneric(key)) {
85124
if (Object.prototype.hasOwnProperty.call(this._deps, genericKey)) this._deps[genericKey].depend();
86125

@@ -91,7 +130,16 @@ export default class ValidationContext {
91130
}
92131

93132
/**
94-
* Validates the object against the simple schema and sets a reactive array of error objects
133+
* Validates the object against the simple schema
134+
* and sets a reactive array of error objects.
135+
* @param obj {object} the document (object) to validate
136+
* @param extendedCustomcontext {object=}
137+
* @param ignoreTypes {string[]=} list of names of ValidationError types to ignore
138+
* @param keysToValidate {string[]=} list of field names (keys) to validate. Other keys are ignored then
139+
* @param isModifier {boolean=} set to true if the document contains MongoDB modifiers
140+
* @param mongoObject {MongoObject=} MongoObject instance to generate keyInfo
141+
* @param isUpsert {boolean=} set to true if the document contains upsert modifiers
142+
* @return {boolean} true if no ValidationError was found, otherwise false
95143
*/
96144
validate(obj, {
97145
extendedCustomContext = {},
@@ -131,11 +179,19 @@ export default class ValidationContext {
131179
return !validationErrors.length;
132180
}
133181

182+
/**
183+
* returns if this context has no errors. reactive.
184+
* @return {boolean}
185+
*/
134186
isValid() {
135187
this._depsAny && this._depsAny.depend();
136188
return this._validationErrors.length === 0;
137189
}
138190

191+
/**
192+
* returns the list of validation errors. reactive.
193+
* @return {ValidationError[]}
194+
*/
139195
validationErrors() {
140196
this._depsAny && this._depsAny.depend();
141197
return this._validationErrors;
@@ -144,4 +200,25 @@ export default class ValidationContext {
144200
clean(...args) {
145201
return this._simpleSchema.clean(...args);
146202
}
203+
204+
//---------------------------------------------------------------------------
205+
// PRIVATE
206+
//---------------------------------------------------------------------------
207+
208+
_markKeyChanged(key) {
209+
const genericKey = MongoObject.makeKeyGeneric(key);
210+
if (Object.prototype.hasOwnProperty.call(this._deps, genericKey)) this._deps[genericKey].changed();
211+
}
212+
213+
_markKeysChanged(keys) {
214+
if (!keys || !Array.isArray(keys) || !keys.length) return;
215+
216+
keys.forEach((key) => this._markKeyChanged(key));
217+
218+
this._depsAny && this._depsAny.changed();
219+
}
220+
221+
_keyIsInvalid(key, genericKey) {
222+
return !!this.getErrorForKey(key, genericKey);
223+
}
147224
}

0 commit comments

Comments
 (0)