Skip to content

Commit

Permalink
SOF-6944: accept schema as optional argument in validation method
Browse files Browse the repository at this point in the history
  • Loading branch information
pranabdas committed Oct 2, 2024
1 parent 45d1fff commit 487d245
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion dist/js/entity/in_memory.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export declare class InMemoryEntity {
* @summary Clone this entity
*/
clone(extraContext?: object): this;
static validateData(data: AnyObject, clean?: boolean): AnyObject;
static validateData(data: AnyObject, clean?: boolean, jsonSchema?: import("json-schema").JSONSchema7 | undefined): AnyObject;
/**
* @summary Validate entity contents against schema
*/
Expand Down
10 changes: 5 additions & 5 deletions dist/js/entity/in_memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,22 @@ class InMemoryEntity {
...extraContext,
});
}
static validateData(data, clean = false) {
if (!this.jsonSchema) {
static validateData(data, clean = false, jsonSchema = this.jsonSchema) {
if (!jsonSchema) {
return data;
}
const result = clean
? ajv.validateAndClean(data, this.jsonSchema, {
? ajv.validateAndClean(data, jsonSchema, {
coerceTypes: this.allowJsonSchemaTypesCoercing,
})
: ajv.validate(data, this.jsonSchema);
: ajv.validate(data, jsonSchema);
if (!result.isValid) {
throw new EntityError({
code: ValidationErrorCode.IN_MEMORY_ENTITY_DATA_INVALID,
details: {
error: result === null || result === void 0 ? void 0 : result.errors,
json: data,
schema: this.jsonSchema,
schema: jsonSchema,
},
});
}
Expand Down
10 changes: 5 additions & 5 deletions src/js/entity/in_memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,23 +116,23 @@ export class InMemoryEntity {
});
}

static validateData(data: AnyObject, clean = false) {
if (!this.jsonSchema) {
static validateData(data: AnyObject, clean = false, jsonSchema = this.jsonSchema) {
if (!jsonSchema) {
return data;
}
const result = clean
? ajv.validateAndClean(data, this.jsonSchema, {
? ajv.validateAndClean(data, jsonSchema, {
coerceTypes: this.allowJsonSchemaTypesCoercing,
})
: ajv.validate(data, this.jsonSchema);
: ajv.validate(data, jsonSchema);

if (!result.isValid) {
throw new EntityError({
code: ValidationErrorCode.IN_MEMORY_ENTITY_DATA_INVALID,
details: {
error: result?.errors,
json: data,
schema: this.jsonSchema,
schema: jsonSchema,
},
});
}
Expand Down

0 comments on commit 487d245

Please sign in to comment.