Skip to content

Instantly share code, notes, and snippets.

@the94air
Last active October 14, 2019 17:46
Show Gist options
  • Save the94air/1ba7afafa3f7bb95b2ab77cd7aaddd68 to your computer and use it in GitHub Desktop.
Save the94air/1ba7afafa3f7bb95b2ab77cd7aaddd68 to your computer and use it in GitHub Desktop.
Tiptap json validation
.custom(value => {
const post = JSON.parse(value);
if (!post || post.type !== 'doc' ||
!Array.isArray(post.content) || post.content.length === 0
) {
return Promise.reject(errors.jsonHTML)
}
const [item] = post.content;
if (item.type === 'paragraph' || item.type === 'heading') {
return !!(Array.isArray(item.content) && item.content.length > 0) ? true : Promise.reject(errors.jsonHTML);
}
if (item.type === 'bullet_list' || item.type === 'ordered_list') {
if (!Array.isArray(item.content) || item.content.length === 0) {
return Promise.reject(errors.jsonHTML);
}
const [item2] = item.content;
if (item2.type === 'list_item' && Array.isArray(item2.content) && item2.content.length > 0) {
const [item3] = item2.content;
if (item3.type === 'paragraph') {
return !!(Array.isArray(item3.content) && item3.content.length > 0) ? true : Promise.reject(errors.jsonHTML);
}
}
}
return Promise.reject(errors.jsonHTML);
})
.custom(value => {
var post = JSON.parse(value);
var result = false;
if(post) {
if(post.type == 'doc') {
if(Array.isArray(post.content) && post.content.length > 0) {
post.content.map((item, index) => {
if(index === 0) {
if(item.type == 'paragraph' || item.type == 'heading') {
if(Array.isArray(item.content) && item.content.length > 0) {
result = true;
} else {
result = false;
}
}
if(item.type == 'bullet_list' || item.type == 'ordered_list') {
if(Array.isArray(item.content) && item.content.length > 0) {
return item.content.map((item, index) => {
if(index === 0) {
if(item.type == 'list_item') {
if(Array.isArray(item.content) && item.content.length > 0) {
return item.content.map((item, index) => {
if(index === 0) {
if(item.type == 'paragraph') {
if(Array.isArray(item.content) && item.content.length > 0) {
result = true;
} else {
result = false;
}
}
}
})
}
}
}
})
}
}
}
});
}
}
}
return result ? true : Promise.reject(errors.jsonHTML);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment