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

Init schemas during module initialization #18

Merged
merged 2 commits into from
Dec 8, 2023
Merged
Changes from 1 commit
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
23 changes: 10 additions & 13 deletions schemaFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ function schemaFactory(rawSchema, foreignKeys) {
var schema = schemaWithAdditionalPropertiesNotAllowedAsDefault(rawSchema);
var schemaWitNoReadonlyProperties = schemaWitNoReadonly(schema);

var validateSchema;
var filterSchema;
var validateSchemaNoReadonly;
var validateSchema = getValidateSchemaInstance();
var filterSchema = getFilterSchemaInstance();
var validateSchemaNoReadonly = getValidateSchemaNoReadonlyInstance();
Comment on lines +19 to +21
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we replace all this with const finally? 🙂

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be a good opportunity to give the library a slight overhaul and update.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I almost did it here 😄 But I will make a new refactoring PR


var exampleJson = generateExampleJson(schema);
var example = parseExampleJson(exampleJson); //make sure it can be parser, this will throw if not
Expand Down Expand Up @@ -47,25 +47,22 @@ function schemaFactory(rawSchema, foreignKeys) {


function getValidateSchemaInstance() {
validateSchema = validateSchema || imjv(schema, {
return imjv(schema, {
formats: formats,
verbose: true
});
return validateSchema;
}
function getFilterSchemaInstance() {
filterSchema = filterSchema || imjv(schema, {
return imjv(schema, {
filter: true,
formats: formats
});
return filterSchema;
}

function getValidateSchemaNoReadonlyInstance() {
validateSchemaNoReadonly = validateSchemaNoReadonly || imjv(schemaWitNoReadonlyProperties, {
return imjv(schemaWitNoReadonlyProperties, {
filter: true
});
return validateSchemaNoReadonly;
}

function localize(locale) {
Expand All @@ -75,8 +72,8 @@ function schemaFactory(rawSchema, foreignKeys) {
function validate(document, options, optionalCallback) {
options = options || {};

getValidateSchemaInstance()(document);
var errors = getValidateSchemaInstance().errors || [];
validateSchema(document);
var errors = validateSchema.errors || [];
var doForeignKeyValidation = options && options.foreignKey === true;
if (errors.length === 0 && doForeignKeyValidation && foreignKeys) {
if (!optionalCallback) {
Expand Down Expand Up @@ -119,9 +116,9 @@ function schemaFactory(rawSchema, foreignKeys) {
}
});
if (options.removeReadOnlyFields === true) { // remove readonly fields from the object, default: false
getValidateSchemaNoReadonlyInstance()(document);
validateSchemaNoReadonly(document);
} else if (options.filter === true) {
getFilterSchemaInstance()(document);
filterSchema(document);
}
var result = { valid: !errors.length, errors: errors };
return result;
Expand Down