Last active
October 14, 2019 17:46
-
-
Save the94air/1ba7afafa3f7bb95b2ab77cd7aaddd68 to your computer and use it in GitHub Desktop.
Tiptap json validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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); | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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