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

Add normalize method #275

Merged
merged 2 commits into from
Oct 17, 2021
Merged
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
13 changes: 13 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1084,4 +1084,17 @@ export default class Validator {
schema: ValidationSchema;
ruleFunction: Function;
};

/**
* Normalize a schema, type or short hand definition by expanding it to a full form. The 'normalized'
* form is the equivalent schema with any short hands undone. This ensure that each rule; always includes
* a 'type' key, arrays always have an 'items' key, 'multi' always have a 'rules' key and objects always
* have their properties defined in a 'props' key
*
* @param { ValidationSchema | string | any } value The value to normalize
* @return {ValidationRule | ValidationSchema } The normalized form of the given rule or schema
*/
normalize(
value: ValidationSchema | string | any
): ValidationRule | ValidationSchema
}
126 changes: 94 additions & 32 deletions lib/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,38 +301,7 @@ class Validator {
* @returns {Object} rule
*/
getRuleFromSchema(schema) {
if (typeof schema === "string") {
schema = this.parseShortHand(schema);
} else if (Array.isArray(schema)) {
if (schema.length == 0)
throw new Error("Invalid schema.");

schema = {
type: "multi",
rules: schema
};

// Check 'optional' flag
const isOptional = schema.rules
.map(s => this.getRuleFromSchema(s))
.every(rule => rule.schema.optional == true);
if (isOptional)
schema.optional = true;
}

if (schema.$$type) {
const type = schema.$$type;
const otherShorthandProps = this.getRuleFromSchema(type).schema;
delete schema.$$type;
const props = Object.assign({}, schema);

for (const key in schema) { // clear object without changing reference
delete schema[key];
}

deepExtend(schema, otherShorthandProps, { skipIfExist: true });
schema.props = props;
}
schema = this.resolveType(schema);

const alias = this.aliases[schema.type];
if (alias) {
Expand Down Expand Up @@ -502,6 +471,99 @@ class Validator {
if (typeof fn !== "function") throw new Error("Plugin fn type must be function");
return fn(this);
}

/**
* Resolve the schema 'type' by:
* - parsing short hands into full type definitions
* - expanding arrays into 'multi' types with a rules property
* - objects which have a root $$type property into a schema which
* explicitly has a 'type' property and a 'props' property.
*
* @param schema The schema to resolve the type of
*/
resolveType(schema) {
if (typeof schema === "string") {
schema = this.parseShortHand(schema);
} else if (Array.isArray(schema)) {
if (schema.length === 0)
throw new Error("Invalid schema.");

schema = {
type: "multi",
rules: schema
};

// Check 'optional' flag
const isOptional = schema.rules
.map(s => this.getRuleFromSchema(s))
.every(rule => rule.schema.optional === true);
if (isOptional)
schema.optional = true;
}

if (schema.$$type) {
const type = schema.$$type;
const otherShorthandProps = this.getRuleFromSchema(type).schema;
delete schema.$$type;
const props = Object.assign({}, schema);

for (const key in schema) { // clear object without changing reference
delete schema[key];
}

deepExtend(schema, otherShorthandProps, { skipIfExist: true });
schema.props = props;
}

return schema;
}

/**
* Normalize a schema, type or short hand definition by expanding it to a full form. The 'normalized'
* form is the equivalent schema with any short hands undone. This ensure that each rule; always includes
* a 'type' key, arrays always have an 'items' key, 'multi' always have a 'rules' key and objects always
* have their properties defined in a 'props' key
*
* @param {Object|String} value The value to normalize
* @returns {Object} The normalized form of the given rule or schema
*/
normalize(value) {
let result = this.resolveType(value);
if(this.aliases[result.type])
result = deepExtend(result, this.normalize(this.aliases[result.type]), { skipIfExists: true});

result = deepExtend(result, this.defaults[result.type], { skipIfExist: true });

if(result.type === "multi") {
result.rules = result.rules.map(r => this.normalize(r));
result.optional = result.rules.every(r => r.optional === true);
return result;
}
if(result.type === "array") {
result.items = this.normalize(result.items);
return result;
}
if(result.type === "object") {
if(result.props) {
for (const [k, v] of Object.entries(result.props)) {
result.props[k] = this.normalize(v);
}
}
}
if(typeof value === "object") {
if(value.type) {
const config = this.normalize(value.type);
deepExtend(result, config, { skipIfExists: true });
}
else{
for (const [k, v] of Object.entries(value)) {
icebob marked this conversation as resolved.
Show resolved Hide resolved
result[k] = this.normalize(v);
}
}
}

return result;
}
}

module.exports = Validator;
Loading