关于模式定制,我有几个问题。
数据/类别
[
{
"name": "Foo Bar",
"slug": "foo-bar"
},
{
"name": "Fuz Baz",
"slug": "fuz-baz"
}
]gatsby-config.js
...
mapping: {
"MarkdownRemark.frontmatter.author": `AuthorsJson.alias`,
"MarkdownRemark.frontmatter.category": `CategoriesJson.name`
}gatsby-node.js
exports.createSchemaCustomization = ({ actions, schema }) => {
const { createTypes } = actions;
const typeDefs = [
`type AuthorsJson implements Node @dontInfer {
alias: String,
name: String
slug: String,
bio: String,
profile: String
posts: [MarkdownRemark] @link(by: "frontmatter.author.alias", from: "alias")
}`,
`type CategoriesJson implements Node @dontInfer {
name: String,
slug: String,
posts: [MarkdownRemark] @link(by: "frontmatter.category.name", from: "name")
}`,
];
createTypes(typeDefs);
}最基本的问题是,我如何完成正面的验证?
categories.json中(例如,它们不能组成自己的类别)?如果前端物质是一个数组,如何确保数组中不存在超过3个元素?谢谢!
备选案文1:
这就是我在gatsby-node.js上的文章
var categories = [];
var aliases = [];
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions
if(node.internal.type === `CategoriesJson`) {
categories.push(node.name);
}
if(node.internal.type === `AuthorsJson`) {
aliases.push(node.alias);
}
if (node.internal.type === `MarkdownRemark`) {
const slug = createFilePath({ node, getNode, basePath: `pages` })
createNodeField({
node,
name: `slug`,
value: slug,
})
if (node.frontmatter.author === null || node.frontmatter.author === undefined) {
throw "Page is missing alias: " + node.fileAbsolutePath;
}
if (aliases.indexOf(node.frontmatter.author) == -1) {
throw "Page has invalid alias: " + node.fileAbsolutePath;
}
if (node.frontmatter.category === null || node.frontmatter.category === undefined) {
throw "Page is missing category: " + node.fileAbsolutePath;
}
if (categories.indexOf(node.frontmatter.category) == -1) {
throw "Page has invalid category ('" + node.frontmatter.category +"'): " + node.fileAbsolutePath;
}
if (node.frontmatter.tags.length > 3) {
throw "Page has more than 3 tags: " + node.fileAbsolutePath;
}
}
}有更好的办法吗?
发布于 2022-04-05 05:48:21
您可以通过以下方法简化所有条件:
let categories = [];
let aliases = [];
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions
if(node.internal.type === `CategoriesJson`) {
categories.push(node.name);
}
if(node.internal.type === `AuthorsJson`) {
aliases.push(node.alias);
}
if (node.internal.type === `MarkdownRemark`) {
const slug = createFilePath({ node, getNode, basePath: `pages` })
createNodeField({
node,
name: `slug`,
value: slug,
})
if (!node.frontmatter.author || !aliases.includes(node.frontmatter.author)) {
throw "Page is missing alias: " + node.fileAbsolutePath;
}
if (!node.frontmatter.category || !categories.includes(node.frontmatter.category)) {
throw "Page is missing category: " + node.fileAbsolutePath;
}
if (node.frontmatter.tags.length > 3) {
throw "Page has more than 3 tags: " + node.fileAbsolutePath;
}
}
}我还添加了let,而不是var,它现在已经被废弃了。检查https://www.javascripttutorial.net/es6/difference-between-var-and-let/上的差异,并将indexOf == -1替换为更语义的includes。
对于其他人来说,这种方法在我看来是相当稳固的,这是一个很好的方法。
如果前端是一个数组,我如何确保数组中不存在多于3个元素?
默认情况下(而且应该是这样),frontmatter始终是一个对象。
https://stackoverflow.com/questions/71744353
复制相似问题