Skip to content

Commit

Permalink
feat: set context name
Browse files Browse the repository at this point in the history
on ValidationContext instance
  • Loading branch information
aldeed committed Jul 22, 2020
1 parent ab227a3 commit fc3f58f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package/lib/SimpleSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ class SimpleSchema {
namedContext(name) {
if (typeof name !== 'string') name = 'default';
if (!this._validationContexts[name]) {
this._validationContexts[name] = new ValidationContext(this);
this._validationContexts[name] = new ValidationContext(this, name);
}
return this._validationContexts[name];
}
Expand Down
28 changes: 28 additions & 0 deletions package/lib/SimpleSchema_namedContext.tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* eslint-disable func-names, prefer-arrow-callback */

import expect from 'expect';
import { SimpleSchema } from './SimpleSchema';

describe('SimpleSchema - namedContext', function () {
it('returns a named context', function () {
const schema = new SimpleSchema({});
const context = schema.namedContext('form');
expect(context.name).toBe('form');
expect(schema._validationContexts.form).toBe(context);
});

it('returns a context named "default" if no name is passed', function () {
const schema = new SimpleSchema({});
const context = schema.namedContext();
expect(context.name).toBe('default');
expect(schema._validationContexts.default).toBe(context);
});

it('returns the same context instance when called with the same name', function () {
const schema = new SimpleSchema({});
const context1 = schema.namedContext('abc');
expect(schema._validationContexts.abc).toBe(context1);
const context2 = schema.namedContext('abc');
expect(context2).toBe(context1);
});
});
7 changes: 6 additions & 1 deletion package/lib/ValidationContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import MongoObject from 'mongo-object';
import doValidation from './doValidation';

export default class ValidationContext {
constructor(ss) {
/**
* @param {SimpleSchema} ss SimpleSchema instance to use for validation
* @param {String} [name] Optional context name, accessible on context.name.
*/
constructor(ss, name) {
this.name = name;
this._simpleSchema = ss;
this._schema = ss.schema();
this._schemaKeys = Object.keys(this._schema);
Expand Down

0 comments on commit fc3f58f

Please sign in to comment.