@contentful/rich-text-types
Advanced tools
Comparing version 15.9.1 to 15.10.0
@@ -117,3 +117,3 @@ "use strict"; | ||
var errorsResult = (0, validation_1.validateRichTextDocument)(value); | ||
expect(errorsResult).toEqual([ | ||
expect(errorsResult).toEqual(expect.arrayContaining([ | ||
expect.objectContaining({ | ||
@@ -128,3 +128,3 @@ keyword: 'enum', | ||
}), | ||
]); | ||
])); | ||
}); | ||
@@ -192,3 +192,3 @@ }); | ||
var errorsResult = (0, validation_1.validateRichTextDocument)(value); | ||
expect(errorsResult).toEqual([ | ||
expect(errorsResult).toEqual(expect.arrayContaining([ | ||
expect.objectContaining({ | ||
@@ -203,3 +203,3 @@ keyword: 'enum', | ||
}), | ||
]); | ||
])); | ||
}); | ||
@@ -209,3 +209,3 @@ it('fail with text and inline as direct children', function () { | ||
var errorsResult = (0, validation_1.validateRichTextDocument)(value); | ||
expect(errorsResult).toEqual([ | ||
expect(errorsResult).toEqual(expect.arrayContaining([ | ||
expect.objectContaining({ | ||
@@ -220,3 +220,3 @@ keyword: 'enum', | ||
}), | ||
]); | ||
])); | ||
}); | ||
@@ -279,3 +279,3 @@ }); | ||
var errorsResult = (0, validation_1.validateRichTextDocument)(value); | ||
expect(errorsResult).toEqual([ | ||
expect(errorsResult).toEqual(expect.arrayContaining([ | ||
expect.objectContaining({ | ||
@@ -290,3 +290,3 @@ keyword: 'enum', | ||
}), | ||
]); | ||
])); | ||
}); | ||
@@ -296,3 +296,3 @@ it("allows only paragraphs as direct children of " + blocks_1.BLOCKS.TABLE_CELL + " nodes", function () { | ||
var errorsResult = (0, validation_1.validateRichTextDocument)(value); | ||
expect(errorsResult).toEqual([ | ||
expect(errorsResult).toEqual(expect.arrayContaining([ | ||
expect.objectContaining({ | ||
@@ -307,3 +307,3 @@ keyword: 'enum', | ||
}), | ||
]); | ||
])); | ||
}); | ||
@@ -313,3 +313,3 @@ it('allows inlines to contain only inline or text nodes', function () { | ||
var errorsResult = (0, validation_1.validateRichTextDocument)(value); | ||
expect(errorsResult).toEqual([ | ||
expect(errorsResult).toEqual(expect.arrayContaining([ | ||
expect.objectContaining({ | ||
@@ -324,3 +324,3 @@ keyword: 'enum', | ||
}), | ||
]); | ||
])); | ||
}); | ||
@@ -357,3 +357,3 @@ it("allows only " + blocks_1.BLOCKS.PARAGRAPH + " as children of " + blocks_1.BLOCKS.QUOTE, function () { | ||
var errorsResult = (0, validation_1.validateRichTextDocument)(value); | ||
expect(errorsResult).toEqual([ | ||
expect(errorsResult).toEqual(expect.arrayContaining([ | ||
expect.objectContaining({ | ||
@@ -368,3 +368,3 @@ keyword: 'enum', | ||
}), | ||
]); | ||
])); | ||
}); | ||
@@ -371,0 +371,0 @@ it('fail without required `content` property', function () { |
@@ -8,2 +8,7 @@ import { ErrorObject } from 'ajv'; | ||
}; | ||
declare type Path = (string | number)[]; | ||
declare type ErrorTransformer<T = unknown> = (error: ErrorObject, path: Path) => T; | ||
export declare type ValidationOptions<T> = { | ||
transformError?: ErrorTransformer<T>; | ||
}; | ||
/** | ||
@@ -27,3 +32,3 @@ * Validates a rich text document against our JSON schemas using AJV. | ||
*/ | ||
export declare function validateRichTextDocument(document: AnyNode): ErrorObject[]; | ||
export declare function validateRichTextDocument<T>(document: AnyNode, options?: ValidationOptions<T>): T[]; | ||
export {}; |
@@ -39,5 +39,7 @@ "use strict"; | ||
*/ | ||
function validateRichTextDocument(document) { | ||
function validateRichTextDocument(document, options) { | ||
var _a; | ||
var validateRootNode = getValidator(blocks_1.BLOCKS.DOCUMENT); | ||
var rootNodeIsValid = validateRootNode(removeGrandChildNodes(document)); | ||
var transformError = (_a = options === null || options === void 0 ? void 0 : options.transformError) !== null && _a !== void 0 ? _a : (function (error) { return error; }); | ||
/** | ||
@@ -52,6 +54,6 @@ * Note that this is not the most beautiful / functional implementation | ||
if (rootNodeIsValid) { | ||
validateChildNodes(document, ['content'], errors); | ||
validateChildNodes(document, ['content'], errors, transformError); | ||
} | ||
else { | ||
buildSchemaErrors(validateRootNode, [], errors); | ||
buildSchemaErrors(validateRootNode, [], errors, transformError); | ||
} | ||
@@ -65,16 +67,16 @@ return errors; | ||
*/ | ||
function validateChildNodes(node, path, errors) { | ||
function validateChildNodes(node, path, errors, transform) { | ||
for (var i = 0; i < node.content.length; i++) { | ||
validateNode(node.content[i], __spreadArray(__spreadArray([], path, true), [i], false), errors); | ||
validateNode(node.content[i], __spreadArray(__spreadArray([], path, true), [i], false), errors, transform); | ||
} | ||
} | ||
function validateNode(node, path, errors) { | ||
function validateNode(node, path, errors, transform) { | ||
var validateSchema = getValidator(node.nodeType); | ||
var isValid = validateSchema(removeGrandChildNodes(resetChildNodes(node))); | ||
if (!isValid) { | ||
buildSchemaErrors(validateSchema, path, errors); | ||
buildSchemaErrors(validateSchema, path, errors, transform); | ||
return; | ||
} | ||
if (!isLeafNode(node)) { | ||
validateChildNodes(node, __spreadArray(__spreadArray([], path, true), ['content'], false), errors); | ||
validateChildNodes(node, __spreadArray(__spreadArray([], path, true), ['content'], false), errors, transform); | ||
} | ||
@@ -92,12 +94,4 @@ } | ||
} | ||
function isConstraintError(error) { | ||
return error.keyword === 'enum' || error.keyword === 'anyOf'; | ||
} | ||
function buildSchemaErrors(validateSchema, path, errors) { | ||
var schemaErrors = validateSchema.errors; | ||
var constraintError = schemaErrors.find(isConstraintError); | ||
if (constraintError) { | ||
errors.push(constraintError); | ||
return; | ||
} | ||
function buildSchemaErrors(validateSchema, path, errors, transform) { | ||
var schemaErrors = (validateSchema.errors || []).map(function (error) { return transform(error, path); }); | ||
errors.push.apply(errors, schemaErrors); | ||
@@ -104,0 +98,0 @@ } |
{ | ||
"name": "@contentful/rich-text-types", | ||
"version": "15.9.1", | ||
"version": "15.10.0", | ||
"main": "dist/index.js", | ||
@@ -50,3 +50,3 @@ "typings": "dist/types/index.d.ts", | ||
}, | ||
"gitHead": "ccedf46a17edcdb71bbb18bc3fce63db1b8f78c0" | ||
"gitHead": "1be2e11998f8e9817bc9b8f72f044b43e76277f4" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
171004
0
5233