Skip to content

Instantly share code, notes, and snippets.

@DragorWW
Created October 7, 2017 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DragorWW/b721247bc91dae6b3c71be9263f45972 to your computer and use it in GitHub Desktop.
Save DragorWW/b721247bc91dae6b3c71be9263f45972 to your computer and use it in GitHub Desktop.
normalize js validator
const normalizedData = normalizr.normalize(data, article);
console.log(article, data, normalizedData);
const errors = normalizeValidator(normalizedData, article);
console.log(errors);
normalizeValidatorLogger(errors);
function getSchemasByName(schemas) {
let newSchemas = {}
newSchemas[schemas._key] = schemas;
Object.keys(schemas.schema).forEach((schemaName) => {
let schema = schemas.schema[schemaName];
if (Array.isArray(schema)) {
schema = schema[0];
}
if (newSchemas[schema._key] && schema.schema && Object.keys(schema.schema).length) {
newSchemas = {
...newSchemas,
...getSchemasByName(schema),
}
}
newSchemas[schema._key] = schema;
})
return newSchemas;
}
function normalizeValidator(data, schemas) {
const errors = {};
const entitieNames = Object.keys(data.entities);
const normalizedSchemas = getSchemasByName(schemas);
function pushError(name, error) {
if (!errors[name]) {
errors[name] = [];
}
let item = errors[name].find(item => error.id === item.id)
if (item) {
item = _.mergeWith(item, error, function (objValue, srcValue) {
if (_.isArray(objValue)) {
return objValue.concat(srcValue);
}
})
} else {
errors[name].push(error);
}
}
entitieNames.forEach((entitieName) => {
const items = data.entities[entitieName];
const schema = normalizedSchemas[entitieName];
const subSchemas = schema.schema;
const itemsIds = Object.keys(items);
itemsIds.forEach((id) => {
const item = items[id];
Object.keys(subSchemas).forEach((subSchemaName) => {
let subSchema = subSchemas[subSchemaName];
if (Array.isArray(subSchema)) {
subSchema = subSchema[0];
}
const value = item[subSchemaName];
if (Array.isArray(value)) {
value.forEach((value) => {
if (!data.entities[subSchema._key][value]) {
pushError(entitieName, {
id: id,
[subSchemaName]: {
value: [value],
schema: subSchema._key,
}
});
}
});
} else {
if (!data.entities[subSchema._key][value]) {
pushError(entitieName, {
id: id,
[subSchemaName]: {
value: value,
schema: subSchema._key,
}
});
}
}
})
})
})
return errors;
}
function normalizeValidatorLogger(entitiesErrors, name = '') {
function getValue(value) {
if (Array.isArray(value)) {
return `[..., ${value.map(getValue).join(', ')}]`;
}
return `"${value}"`;
}
const entitieNames = Object.keys(entitiesErrors);
if (entitieNames.length) {
console.groupCollapsed(`[normalize validator]: ${name? name : entitieNames.join(', ')}`);
}
entitieNames.forEach((entitieName) => {
const errors = entitiesErrors[entitieName];
console.groupCollapsed(`${entitieName}`);
errors.forEach(({id, ...fields}) => {
const fieldsName = Object.keys(fields);
console.warn(`
<${_.upperFirst(entitieName)}> = {
id: "${id}",
${fieldsName
.map((fieldName) => ` ${fieldName}: ${getValue(fields[fieldName].value)}, // <${_.upperFirst(fields[fieldName].schema)}>`)
.join("\n")}
};`
)
})
console.groupEnd();
});
if (entitieNames.length) {
console.groupEnd();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment