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

Fix oneof #435

Closed
wants to merge 8 commits into from
Closed
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
51 changes: 51 additions & 0 deletions package/lib/SimpleSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,52 @@ class SimpleSchema {
return result;
}

// while potentially not very performant - this is only called once per validation for the total schema
// it is called per "key" of the original schema if that key is a oneOf, limited to the schema of the key.
// this is equivalent to at most once for the entire root schema.
recursiveKeys(startPrefix) {
const keys = new Set(Object.values(this._schemaKeys));
const recurse = (prefix) => {
keys.add(prefix);
this.objectKeys(prefix).forEach((suffix) => recurse(`${prefix}.${suffix}`));
};
Object.values(this._schemaKeys).filter((k) => k.endsWith('$')).forEach(recurse);
this.objectKeys(startPrefix).forEach(recurse);
return Array.from(keys);
}

// returns all keys that are internal to a oneOf - does not include the key that IS the oneOf.
// returns a map of the key => [{ prefix: String, suffix: String, schema: SimpleSchema }]
oneOfKeys() {
const oneOfKeys = new Map();
this._oneOfKeys.forEach((key) => {
if (this._schema[key].type.definitions.length > 1) {
this._schema[key].type.definitions.forEach((typeDef) => {
if (!(SimpleSchema.isSimpleSchema(typeDef.type))) return;
typeDef.type.recursiveKeys().forEach((oneOfKey) => {
const fullKey = `${key}.${oneOfKey}`;
if (oneOfKeys.has(fullKey)) {
oneOfKeys.get(fullKey).push({
schema: typeDef.type,
prefix: key,
suffix: oneOfKey,
});
} else {
oneOfKeys.set(fullKey, [{
schema: typeDef.type,
prefix: key,
suffix: oneOfKey,
}]);
}
});
});
}
});

// most places we're going to use this, a set is better than an array
return oneOfKeys;
}

// Returns an array of all the blackbox keys, including those in subschemas
blackboxKeys() {
const blackboxKeys = new Set(this._blackboxKeys);
Expand Down Expand Up @@ -535,6 +581,7 @@ class SimpleSchema {
this._schemaKeys = Object.keys(this._schema);
this._autoValues = [];
this._blackboxKeys = new Set();
this._oneOfKeys = new Set();
this._firstLevelSchemaKeys = [];
this._objectKeys = {};

Expand All @@ -551,6 +598,10 @@ class SimpleSchema {
// Keep list of all top level keys
if (fieldName.indexOf('.') === -1) this._firstLevelSchemaKeys.push(fieldName);

if (definition.type.definitions.length > 1) {
this._oneOfKeys.add(fieldName);
}

// Keep list of all blackbox keys for passing to MongoObject constructor
// XXX For now if any oneOf type is blackbox, then the whole field is.
/* eslint-disable no-restricted-syntax */
Expand Down
Loading