File tree Expand file tree Collapse file tree 3 files changed +35
-2
lines changed Expand file tree Collapse file tree 3 files changed +35
-2
lines changed Original file line number Diff line number Diff line change @@ -586,7 +586,7 @@ class SimpleSchema {
586
586
namedContext ( name ) {
587
587
if ( typeof name !== 'string' ) name = 'default' ;
588
588
if ( ! this . _validationContexts [ name ] ) {
589
- this . _validationContexts [ name ] = new ValidationContext ( this ) ;
589
+ this . _validationContexts [ name ] = new ValidationContext ( this , name ) ;
590
590
}
591
591
return this . _validationContexts [ name ] ;
592
592
}
Original file line number Diff line number Diff line change
1
+ /* eslint-disable func-names, prefer-arrow-callback */
2
+
3
+ import expect from 'expect' ;
4
+ import { SimpleSchema } from './SimpleSchema' ;
5
+
6
+ describe ( 'SimpleSchema - namedContext' , function ( ) {
7
+ it ( 'returns a named context' , function ( ) {
8
+ const schema = new SimpleSchema ( { } ) ;
9
+ const context = schema . namedContext ( 'form' ) ;
10
+ expect ( context . name ) . toBe ( 'form' ) ;
11
+ expect ( schema . _validationContexts . form ) . toBe ( context ) ;
12
+ } ) ;
13
+
14
+ it ( 'returns a context named "default" if no name is passed' , function ( ) {
15
+ const schema = new SimpleSchema ( { } ) ;
16
+ const context = schema . namedContext ( ) ;
17
+ expect ( context . name ) . toBe ( 'default' ) ;
18
+ expect ( schema . _validationContexts . default ) . toBe ( context ) ;
19
+ } ) ;
20
+
21
+ it ( 'returns the same context instance when called with the same name' , function ( ) {
22
+ const schema = new SimpleSchema ( { } ) ;
23
+ const context1 = schema . namedContext ( 'abc' ) ;
24
+ expect ( schema . _validationContexts . abc ) . toBe ( context1 ) ;
25
+ const context2 = schema . namedContext ( 'abc' ) ;
26
+ expect ( context2 ) . toBe ( context1 ) ;
27
+ } ) ;
28
+ } ) ;
Original file line number Diff line number Diff line change @@ -2,7 +2,12 @@ import MongoObject from 'mongo-object';
2
2
import doValidation from './doValidation' ;
3
3
4
4
export default class ValidationContext {
5
- constructor ( ss ) {
5
+ /**
6
+ * @param {SimpleSchema } ss SimpleSchema instance to use for validation
7
+ * @param {String } [name] Optional context name, accessible on context.name.
8
+ */
9
+ constructor ( ss , name ) {
10
+ this . name = name ;
6
11
this . _simpleSchema = ss ;
7
12
this . _schema = ss . schema ( ) ;
8
13
this . _schemaKeys = Object . keys ( this . _schema ) ;
You can’t perform that action at this time.
0 commit comments