Skip to content

Commit

Permalink
feat: support custom error transformer (#296)
Browse files Browse the repository at this point in the history
Co-authored-by: Cyberxon <marouenbs93@gmail.com>
  • Loading branch information
z0al and Cyberxon committed Dec 15, 2021
1 parent 744e940 commit 9449b87
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 119 deletions.
38 changes: 19 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/contentful-slatejs-adapter/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/rich-text-html-renderer/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

168 changes: 91 additions & 77 deletions packages/rich-text-types/src/__test__/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,19 @@ describe('validateRichTextDocument', () => {

const errorsResult = validateRichTextDocument(value);

expect(errorsResult).toEqual([
expect.objectContaining({
keyword: 'enum',
instancePath: '/content/0/nodeType',
message: 'must be equal to one of the allowed values',
params: {
allowedValues: Object.values(INLINES).sort(),
},
data: 'custom-type',
}),
]);
expect(errorsResult).toEqual(
expect.arrayContaining([
expect.objectContaining({
keyword: 'enum',
instancePath: '/content/0/nodeType',
message: 'must be equal to one of the allowed values',
params: {
allowedValues: Object.values(INLINES).sort(),
},
data: 'custom-type',
}),
]),
);
});
});

Expand Down Expand Up @@ -202,17 +204,19 @@ describe('validateRichTextDocument', () => {
const value = document({}, text());
const errorsResult = validateRichTextDocument(value);

expect(errorsResult).toEqual([
expect.objectContaining({
keyword: 'enum',
instancePath: '/content/0/nodeType',
message: `must be equal to one of the allowed values`,
params: {
allowedValues: topLevelBlocks,
},
data: 'text',
}),
]);
expect(errorsResult).toEqual(
expect.arrayContaining([
expect.objectContaining({
keyword: 'enum',
instancePath: '/content/0/nodeType',
message: `must be equal to one of the allowed values`,
params: {
allowedValues: topLevelBlocks,
},
data: 'text',
}),
]),
);
});

it('fail with text and inline as direct children', () => {
Expand All @@ -225,17 +229,19 @@ describe('validateRichTextDocument', () => {

const errorsResult = validateRichTextDocument(value);

expect(errorsResult).toEqual([
expect.objectContaining({
keyword: 'enum',
instancePath: '/content/0/nodeType',
message: `must be equal to one of the allowed values`,
params: {
allowedValues: topLevelBlocks,
},
data: 'text',
}),
]);
expect(errorsResult).toEqual(
expect.arrayContaining([
expect.objectContaining({
keyword: 'enum',
instancePath: '/content/0/nodeType',
message: `must be equal to one of the allowed values`,
params: {
allowedValues: topLevelBlocks,
},
data: 'text',
}),
]),
);
});
});

Expand Down Expand Up @@ -302,17 +308,19 @@ describe('validateRichTextDocument', () => {
const value = document({}, node(BLOCKS.UL_LIST, {}, node(BLOCKS.LIST_ITEM, {}, text(''))));
const errorsResult = validateRichTextDocument(value);

expect(errorsResult).toEqual([
expect.objectContaining({
keyword: 'enum',
instancePath: '/content/0/nodeType',
message: `must be equal to one of the allowed values`,
params: {
allowedValues: listBlocks,
},
data: 'text',
}),
]);
expect(errorsResult).toEqual(
expect.arrayContaining([
expect.objectContaining({
keyword: 'enum',
instancePath: '/content/0/nodeType',
message: `must be equal to one of the allowed values`,
params: {
allowedValues: listBlocks,
},
data: 'text',
}),
]),
);
});

it(`allows only paragraphs as direct children of ${BLOCKS.TABLE_CELL} nodes`, () => {
Expand All @@ -323,17 +331,19 @@ describe('validateRichTextDocument', () => {

const errorsResult = validateRichTextDocument(value);

expect(errorsResult).toEqual([
expect.objectContaining({
keyword: 'enum',
instancePath: '/content/0/nodeType',
message: `must be equal to one of the allowed values`,
params: {
allowedValues: ['paragraph'],
},
data: 'text',
}),
]);
expect(errorsResult).toEqual(
expect.arrayContaining([
expect.objectContaining({
keyword: 'enum',
instancePath: '/content/0/nodeType',
message: `must be equal to one of the allowed values`,
params: {
allowedValues: ['paragraph'],
},
data: 'text',
}),
]),
);
});

it('allows inlines to contain only inline or text nodes', () => {
Expand All @@ -348,17 +358,19 @@ describe('validateRichTextDocument', () => {

const errorsResult = validateRichTextDocument(value);

expect(errorsResult).toEqual([
expect.objectContaining({
keyword: 'enum',
instancePath: '/content/0/nodeType',
message: `must be equal to one of the allowed values`,
params: {
allowedValues: ['text'],
},
data: BLOCKS.PARAGRAPH,
}),
]);
expect(errorsResult).toEqual(
expect.arrayContaining([
expect.objectContaining({
keyword: 'enum',
instancePath: '/content/0/nodeType',
message: `must be equal to one of the allowed values`,
params: {
allowedValues: ['text'],
},
data: BLOCKS.PARAGRAPH,
}),
]),
);
});

it(`allows only ${BLOCKS.PARAGRAPH} as children of ${BLOCKS.QUOTE}`, () => {
Expand Down Expand Up @@ -400,17 +412,19 @@ describe('validateRichTextDocument', () => {
const value = document({}, node(BLOCKS.PARAGRAPH, { data: {}, nodeType: null }, text('')));
const errorsResult = validateRichTextDocument(value);

expect(errorsResult).toEqual([
expect.objectContaining({
keyword: 'enum',
instancePath: '/content/0/nodeType',
message: 'must be equal to one of the allowed values',
params: {
allowedValues: topLevelBlocks,
},
data: null,
}),
]);
expect(errorsResult).toEqual(
expect.arrayContaining([
expect.objectContaining({
keyword: 'enum',
instancePath: '/content/0/nodeType',
message: 'must be equal to one of the allowed values',
params: {
allowedValues: topLevelBlocks,
},
data: null,
}),
]),
);
});

it('fail without required `content` property', () => {
Expand Down
Loading

0 comments on commit 9449b87

Please sign in to comment.