Skip to content

Commit

Permalink
schema validators
Browse files Browse the repository at this point in the history
  • Loading branch information
pavjacko committed Oct 9, 2023
1 parent 21e42b4 commit 46a21c9
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 53 deletions.
2 changes: 1 addition & 1 deletion buildHooks/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { prePublish } from './prePublish';

import { gitCommit, gitTag, gitCommitAndTag } from '@rnv/build-hooks-git';
import { generateSchema } from '@rnv/build-hooks-git';
import { generateSchema } from '@rnv/build-hooks-schema';

const hooks = {
prePublish,
Expand Down
20 changes: 11 additions & 9 deletions packages/build-hooks-schema/src/schema.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { RnvContext } from '@rnv/core';
import { RnvContext, RootProjectSchema, logSuccess } from '@rnv/core';
import { zodToJsonSchema } from 'zod-to-json-schema';
import path from 'path';
import fs from 'fs';

// import path from 'path';
// import fs from 'fs';
export const generateSchema = async (c: RnvContext) => {
const jsonSchema = zodToJsonSchema(RootProjectSchema, 'mySchema');
jsonSchema['$schema'] = 'http://json-schema.org/draft-04/schema#';

export const generateSchema = async (_c: RnvContext) => {
//TODO: switch to zod parser
// const schema: Record<string, any> = getRenativeJsonSchema();
// schema['$schema'] = 'http://json-schema.org/draft-04/schema#';
// const destPath = path.join(c.paths.project.dir, 'packages/core/schemas/renative.json');
// fs.writeFileSync(destPath, JSON.stringify(schema, null, 2));
const destPath = path.join(c.paths.project.dir, '.rnv/schema/renative.project.json');
fs.writeFileSync(destPath, JSON.stringify(jsonSchema, null, 2));

logSuccess('Sucessfully exported renative.project.json schema');
};
4 changes: 2 additions & 2 deletions packages/core/src/schema/configRootApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const Hidden = z

//LEVEl 0 (ROOT)

const RootApp = z.object({
export const RootAppSchema = z.object({
id: z.optional(Id),
version: z.optional(Version),
versionCode: z.optional(VersionCode),
Expand All @@ -95,4 +95,4 @@ const RootApp = z.object({
extend: z.optional(Extend),
});

export type ConfigRootApp = z.infer<typeof RootApp>;
export type _ConfigRootApp = z.infer<typeof RootAppSchema>;
4 changes: 2 additions & 2 deletions packages/core/src/schema/configRootProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ const Paths = z

//LEVEl 0 (ROOT)

const RootProject = z.object({
export const RootProjectSchema = z.object({
workspaceID: WorkspaceID,
projectName: ProjectName,
isMonorepo: z.optional(IsMonoRepo),
Expand All @@ -224,4 +224,4 @@ const RootProject = z.object({
});
//.catchall(z.never());

export type ConfigRootProject = z.infer<typeof RootProject>;
export type _ConfigRootProject = z.infer<typeof RootProjectSchema>;
23 changes: 3 additions & 20 deletions packages/core/src/schema/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
// import Ajv from 'ajv';
// import { SCHEMAS_RENATIVE_JSON } from './schemaRenativeJson';
// import { SCHEMAS_RUNTIME_OBJECT } from './schemaRuntimeObject';

// const ajvRenativeJson = new Ajv({ schemas: SCHEMAS_RENATIVE_JSON, allErrors: true, allowUnionTypes: true });
// const ajvRuntimeObject = new Ajv({ schemas: SCHEMAS_RUNTIME_OBJECT, allErrors: true, allowUnionTypes: true });

// export const getRenativeJsonSchema = () => SCHEMAS_RENATIVE_JSON[0];
// export const validateRenativeJsonSchema = (cObj: any) => {
// const valid = ajvRenativeJson.validate(SCHEMAS_RENATIVE_JSON[0], cObj);
// return [valid, ajvRenativeJson];
// };

// export const getRuntimeObjectSchema = () => SCHEMAS_RUNTIME_OBJECT[0];
// export const validateRuntimeObjectSchema = (cObj: any) => {
// const valid = ajvRuntimeObject.validate(SCHEMAS_RUNTIME_OBJECT[0], cObj);
// return [valid, ajvRuntimeObject];
// };

export default {};
export { RootAppSchema } from './configRootApp';
export { RootProjectSchema } from './configRootProject';
export * from './validators';
5 changes: 5 additions & 0 deletions packages/core/src/schema/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { _ConfigRootApp } from './configRootApp';
import type { _ConfigRootProject } from './configRootProject';

export type ConfigRootProject = _ConfigRootProject;
export type ConfigRootApp = _ConfigRootApp;
5 changes: 5 additions & 0 deletions packages/core/src/schema/validators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { RootProjectSchema } from './configRootProject';

export const validateRenativeProjectSchema = (inputJson: any) => {
return RootProjectSchema.safeParse(inputJson);
};
37 changes: 19 additions & 18 deletions packages/engine-core/src/tasks/task.rnv.doctor.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import lGet from 'lodash.get';
import {
// chalk,
chalk,
logTask,
// logToSummary,
logToSummary,
PARAMS,
TASK_DOCTOR,
TASK_APP_CONFIGURE,
executeTask,
configureRuntimeDefaults,
// readObjectSync,
readObjectSync,
fsExistsSync,
// validateRenativeJsonSchema,
RnvTaskFn,
validateRenativeProjectSchema,
} from '@rnv/core';

const configTargets = [
Expand Down Expand Up @@ -43,13 +43,19 @@ export const taskRnvDoctor: RnvTaskFn = async (c, parentTask, originTask) => {
}
});

// let errMsg = 'RENATIVE JSON SCHEMA VALIDITY CHECK:\n\n';
// let hasErrors = false;
let errMsg = 'RENATIVE JSON SCHEMA VALIDITY CHECK:\n\n';
let hasErrors = false;
configPaths.forEach((cPath) => {
if (fsExistsSync(cPath)) {
// const cObj = readObjectSync(cPath);
// TODO: switch to ZOD
// const [valid, ajv] = validateRenativeJsonSchema(cObj);
const cObj = readObjectSync(cPath);
const result = validateRenativeProjectSchema(cObj);
if (!result.success) {
hasErrors = true;
errMsg += chalk().yellow(`\nInvalid schema in ${cPath}. ISSUES:\n\n`);
result.error.errors?.forEach((err) => {
errMsg += chalk().yellow(`${chalk().grey(err.path)}: ${err.message}`);
});
}
// if (!valid) {
// hasErrors = true;
// // console.log('ERROR', ajv.errors);
Expand All @@ -74,16 +80,11 @@ export const taskRnvDoctor: RnvTaskFn = async (c, parentTask, originTask) => {
}
});

// if (!hasErrors) {
// errMsg += chalk().green(`PASSED ${configPaths.length} files`);
// }

// const [valid, ajv] = validateRuntimeObjectSchema(c);
// if (!valid) {
// console.log('ERROR', ajv.errors);
// }
if (!hasErrors) {
errMsg += chalk().green(`PASSED ${configPaths.length} files`);
}

// logToSummary(errMsg);
logToSummary(errMsg);
};

export default {
Expand Down
2 changes: 1 addition & 1 deletion packages/template-starter/renative.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "../../.rnv/schema/renative.json",
"$schema": "../../.rnv/schema/renative.project.json",
"engines": {
"@rnv/engine-lightning": "source:rnv",
"@rnv/engine-rn": "source:rnv",
Expand Down

0 comments on commit 46a21c9

Please sign in to comment.