Skip to content

Commit

Permalink
Move headlessConfigSchema from the next package to the core package a…
Browse files Browse the repository at this point in the history
…nd tighten checks further
  • Loading branch information
sambrodie committed Aug 6, 2024
1 parent e35476c commit 6612a87
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 101 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

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

13 changes: 7 additions & 6 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,26 @@
"@testing-library/react": "^16.0.0",
"@types/jest": "^29.0.3",
"@types/node-fetch": "^2.5.3",
"@types/react": "^18",
"@types/react-dom": "^18",
"expect-type": "^0.15.0",
"isomorphic-fetch": "^3.0.0",
"jest": "^29.3.1",
"msw": "^0.35.0",
"ts-jest": "^29.0.3",
"typescript": "^5.5.3",
"isomorphic-fetch": "^3.0.0",
"tsc-esm-fix": "^2.20.27",
"@types/react": "^18",
"@types/react-dom": "^18"
"typescript": "^5.5.3"
},
"dependencies": {
"@justinribeiro/lite-youtube": "^1.3.1",
"deepmerge": "^4.3.1",
"html-react-parser": "^3.0.4",
"path-to-regexp": "^6.2.0",
"react-inspector": "^6.0.1",
"schema-dts": "^1.1.2",
"swr": "^2.2.5",
"xss": "^1.0.15",
"deepmerge": "^4.3.1",
"schema-dts": "^1.1.2"
"yup": "^1.4.0"
},
"peerDependencies": {
"react": ">= 17.0.2"
Expand Down
157 changes: 157 additions & 0 deletions packages/core/src/utils/headlessConfigSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
import { object, string, mixed, array, boolean, lazy } from 'yup';
import { CustomPostTypes, CustomTaxonomies } from '../types';

// Define the schema for CustomPostType
const customPostTypeSchema = object({
slug: string().required(),
endpoint: string().required(),
single: string(),
archive: string(),
matchSinglePath: boolean(),
}).noUnknown();

// Define the schema for CustomPostTypes (array of CustomPostType)
const customPostTypesSchema = array().of(customPostTypeSchema);

// Define the schema for CustomTaxonomy
const customTaxonomySchema = object({
slug: string().required(),
endpoint: string().required(),
rewrite: string(),
restParam: string(),
matchArchivePath: boolean(),
}).noUnknown();

// Define the schema for CustomTaxonomies (array of CustomTaxonomy)
const customTaxonomiesSchema = array().of(customTaxonomySchema);

export const headlessConfigSchema = object({
host: string(),
locale: string(),
sourceUrl: string(),
hostUrl: string(),

customPostTypes: mixed<
CustomPostTypes | ((defaultPostTypes: CustomPostTypes) => CustomPostTypes)
>().test(
'is-custom-post-types',
'customPostTypes must be an array or a function that returns an array',
(value) => {
if (typeof value === 'function') {
// Test the function by calling it with a dummy array
const result = value([]);
return customPostTypesSchema.isValidSync(result, {
strict: true,
abortEarly: false,
stripUnknown: false,
});
}

return customPostTypesSchema.isValidSync(value) || value === undefined;
},
),

customTaxonomies: mixed<
CustomTaxonomies | ((defaultPostTypes: CustomTaxonomies) => CustomTaxonomies)
>().test(
'is-custom-taxonomies',
'customTaxonomies must be an array or a function that returns an array',
(value) => {
if (typeof value === 'function') {
// Test the function by calling it with a dummy array
const result = value([]);

return customTaxonomiesSchema.isValidSync(result, {
strict: true,
abortEarly: false,
stripUnknown: false,
});
}

return customTaxonomiesSchema.isValidSync(value) || value === undefined;
},
),

redirectStrategy: string().oneOf(['404', 'none', 'always']),

useWordPressPlugin: boolean(),

integrations: object({
yoastSEO: object({
enabled: boolean(),
}),
polylang: object({
enabled: boolean(),
}),
}),

i18n: object({
locales: array(string()).required(),
defaultLocale: string().required(),
localeDetection: boolean(),
}).default(undefined),

preview: object({
alternativeAuthorizationHeader: boolean(),
usePostLinkForRedirect: boolean(),
}),

debug: object({
requests: boolean(),
redirects: boolean(),
devMode: boolean(),
}),

cache: object({
ttl: mixed().test(
'is-valid-ttl',
'cache.ttl must be a number or a function that returns a number',
(value) => {
if (typeof value === 'function') {
return typeof value() === 'number';
}

return typeof value === 'number' || value === undefined;
},
),
enabled: mixed().test(
'is-valid-cache-enabled',
'cache.enabled must be a boolean or a function that returns a boolean',
(value) => {
if (typeof value === 'function') {
return typeof value() === 'boolean';
}

return typeof value === 'boolean' || value === undefined;
},
),
beforeSet: mixed<Function>()
.test(
'is-function',
'cache.beforeSet must be a function',
(value) => typeof value === 'function' || value === undefined,
)
.optional(),
afterGet: mixed<Function>()
.test(
'is-function',
'cache.afterGet must be a function',
(value) => typeof value === 'function' || value === undefined,
)
.optional(),
cacheHandler: object({
set: mixed<Function>().test(
'is-function',
'cacheHandler.set must be a function',
(value) => typeof value === 'function',
),
get: mixed<Function>().test(
'is-function',
'cacheHandler.get must be a function',
(value) => typeof value === 'function',
),
}),
}),

sites: array(lazy(() => headlessConfigSchema.default(undefined))),
}).noUnknown();
1 change: 1 addition & 0 deletions packages/core/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from './url';
export * from './log';
export * from './decodeHtmlSpeciaChars';
export * from './getObjectProperty';
export * from './headlessConfigSchema';
3 changes: 1 addition & 2 deletions packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@
"deepmerge": "^4.3.1",
"loader-utils": "^3.2.0",
"negotiator": "^0.6.3",
"schema-utils": "^4.0.0",
"yup": "^1.4.0"
"schema-utils": "^4.0.0"
},
"devDependencies": {
"@testing-library/dom": "^10.3.1",
Expand Down
87 changes: 0 additions & 87 deletions packages/next/src/config/headlessConfigSchema.ts

This file was deleted.

7 changes: 4 additions & 3 deletions packages/next/src/config/withHeadstartWPConfig.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { ConfigError, HeadlessConfig } from '@headstartwp/core';
import { ConfigError, HeadlessConfig, headlessConfigSchema } from '@headstartwp/core';
import { NextConfig } from 'next';
import fs from 'fs';
import { ValidationError } from 'yup';
import { ModifySourcePlugin, ConcatOperation } from './plugins/ModifySourcePlugin';
import headlessConfigSchema from './headlessConfigSchema';

const LINARIA_EXTENSION = '.linaria.module.css';

Expand Down Expand Up @@ -117,8 +116,10 @@ export function withHeadstartWPConfig(
});
} catch (error) {
if (error instanceof ValidationError) {
console.error(`Error in the configuration file: ${error.errors.map(String)}`);
// eslint-disable-next-line no-console
console.error(`Error in the configuration file: ${error.errors.join(', ')}`);
} else {
// eslint-disable-next-line no-console
console.error('Unexpected error in the configuration file:', error);
}
}
Expand Down

0 comments on commit 6612a87

Please sign in to comment.