Skip to content

Commit

Permalink
refactor: modular schema registration
Browse files Browse the repository at this point in the history
  • Loading branch information
eyadmba committed Apr 4, 2024
1 parent cf7af6b commit dfd3b61
Show file tree
Hide file tree
Showing 5 changed files with 261 additions and 1 deletion.
6 changes: 6 additions & 0 deletions external/resolvedSchemas.json
Original file line number Diff line number Diff line change
Expand Up @@ -9506,10 +9506,16 @@
"description": "A listing of all IP addresses associated with this Host",
"anyOf": [
{ "type": "string", "format": "ip" },
{ "type": "string", "format": "ipCidr" },
{
"type": "array",
"uniqueItems": true,
"items": { "type": "string", "format": "ip" }
},
{
"type": "array",
"uniqueItems": true,
"items": { "type": "string", "format": "ipCidr" }
}
]
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"scripts": {
"prebuild": "rm -rf dist/*",
"build": "yarn tsc --project tsconfig.dist.json --declaration",
"generate-schema-imports": "./tools/generate-schema-imports.sh",
"generate-schema-imports": "./tools/generate-schema-imports.sh && ./tools/generate-register-schemas-function.sh",
"generate-resolved-schemas": "node tools/generate-resolved-schemas/index.js src/schemas external/resolvedSchemas.json && prettier --write external/resolvedSchemas.json",
"test": "jest",
"lint": "eslint src --ext .ts,.js",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ export type EntityClass = keyof typeof entitySchemas;
export const entityClasses = Object.keys(entitySchemas) as EntityClass[];

export { validateEntityWithSchema } from './validateEntityWithSchema';
export { registerSchemas } from './registerSchemas';
export { getSchema } from './getSchema';
207 changes: 207 additions & 0 deletions src/registerSchemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
// THIS FILE IS AUTOMATICALLY GENERATED!
// using the script tools/generate-register-schemas-function.sh

import Ajv from 'ajv';

// Dynamically imported schemas
import AccessKeyJson from './schemas/AccessKey.json';
import AccessPolicyJson from './schemas/AccessPolicy.json';
import AccessRoleJson from './schemas/AccessRole.json';
import AccountJson from './schemas/Account.json';
import AlertJson from './schemas/Alert.json';
import ApplicationJson from './schemas/Application.json';
import ApplicationEndpointJson from './schemas/ApplicationEndpoint.json';
import AssessmentJson from './schemas/Assessment.json';
import AttackerJson from './schemas/Attacker.json';
import BackupJson from './schemas/Backup.json';
import CertificateJson from './schemas/Certificate.json';
import ChannelJson from './schemas/Channel.json';
import ClusterJson from './schemas/Cluster.json';
import CodeCommitJson from './schemas/CodeCommit.json';
import CodeDeployJson from './schemas/CodeDeploy.json';
import CodeModuleJson from './schemas/CodeModule.json';
import CodeRepoJson from './schemas/CodeRepo.json';
import CodeReviewJson from './schemas/CodeReview.json';
import ConfigurationJson from './schemas/Configuration.json';
import ContainerJson from './schemas/Container.json';
import ControlJson from './schemas/Control.json';
import ControlPolicyJson from './schemas/ControlPolicy.json';
import CryptoKeyJson from './schemas/CryptoKey.json';
import DataCollectionJson from './schemas/DataCollection.json';
import DataObjectJson from './schemas/DataObject.json';
import DataStoreJson from './schemas/DataStore.json';
import DatabaseJson from './schemas/Database.json';
import DeploymentJson from './schemas/Deployment.json';
import DeviceJson from './schemas/Device.json';
import DirectoryJson from './schemas/Directory.json';
import DiskJson from './schemas/Disk.json';
import DocumentJson from './schemas/Document.json';
import DomainJson from './schemas/Domain.json';
import DomainRecordJson from './schemas/DomainRecord.json';
import DomainZoneJson from './schemas/DomainZone.json';
import EntityJson from './schemas/Entity.json';
import FindingJson from './schemas/Finding.json';
import FirewallJson from './schemas/Firewall.json';
import FrameworkJson from './schemas/Framework.json';
import FunctionJson from './schemas/Function.json';
import GatewayJson from './schemas/Gateway.json';
import GraphObjectJson from './schemas/GraphObject.json';
import GroupJson from './schemas/Group.json';
import HostJson from './schemas/Host.json';
import HostAgentJson from './schemas/HostAgent.json';
import ImageJson from './schemas/Image.json';
import IncidentJson from './schemas/Incident.json';
import InternetJson from './schemas/Internet.json';
import IpAddressJson from './schemas/IpAddress.json';
import IssueJson from './schemas/Issue.json';
import KeyJson from './schemas/Key.json';
import LogsJson from './schemas/Logs.json';
import ModelJson from './schemas/Model.json';
import ModuleJson from './schemas/Module.json';
import NetworkJson from './schemas/Network.json';
import NetworkEndpointJson from './schemas/NetworkEndpoint.json';
import NetworkInterfaceJson from './schemas/NetworkInterface.json';
import OrganizationJson from './schemas/Organization.json';
import PRJson from './schemas/PR.json';
import PasswordPolicyJson from './schemas/PasswordPolicy.json';
import PersonJson from './schemas/Person.json';
import PolicyJson from './schemas/Policy.json';
import ProblemJson from './schemas/Problem.json';
import ProcedureJson from './schemas/Procedure.json';
import ProcessJson from './schemas/Process.json';
import ProductJson from './schemas/Product.json';
import ProgramJson from './schemas/Program.json';
import ProjectJson from './schemas/Project.json';
import QuestionJson from './schemas/Question.json';
import QueueJson from './schemas/Queue.json';
import RecordJson from './schemas/Record.json';
import RecordEntityJson from './schemas/RecordEntity.json';
import RepositoryJson from './schemas/Repository.json';
import RequirementJson from './schemas/Requirement.json';
import ResourceJson from './schemas/Resource.json';
import ReviewJson from './schemas/Review.json';
import RiskJson from './schemas/Risk.json';
import RootJson from './schemas/Root.json';
import RuleJson from './schemas/Rule.json';
import RulesetJson from './schemas/Ruleset.json';
import ScannerJson from './schemas/Scanner.json';
import SecretJson from './schemas/Secret.json';
import SectionJson from './schemas/Section.json';
import ServiceJson from './schemas/Service.json';
import SiteJson from './schemas/Site.json';
import StandardJson from './schemas/Standard.json';
import SubscriptionJson from './schemas/Subscription.json';
import TaskJson from './schemas/Task.json';
import TeamJson from './schemas/Team.json';
import ThreatIntelJson from './schemas/ThreatIntel.json';
import TrainingJson from './schemas/Training.json';
import UserJson from './schemas/User.json';
import UserGroupJson from './schemas/UserGroup.json';
import VaultJson from './schemas/Vault.json';
import VendorJson from './schemas/Vendor.json';
import VulnerabilityJson from './schemas/Vulnerability.json';
import WeaknessJson from './schemas/Weakness.json';
import WorkflowJson from './schemas/Workflow.json';
import WorkloadJson from './schemas/Workload.json';

export function registerSchemas(ajvInstance: Ajv): void {
ajvInstance.addSchema(AccessKeyJson);
ajvInstance.addSchema(AccessPolicyJson);
ajvInstance.addSchema(AccessRoleJson);
ajvInstance.addSchema(AccountJson);
ajvInstance.addSchema(AlertJson);
ajvInstance.addSchema(ApplicationJson);
ajvInstance.addSchema(ApplicationEndpointJson);
ajvInstance.addSchema(AssessmentJson);
ajvInstance.addSchema(AttackerJson);
ajvInstance.addSchema(BackupJson);
ajvInstance.addSchema(CertificateJson);
ajvInstance.addSchema(ChannelJson);
ajvInstance.addSchema(ClusterJson);
ajvInstance.addSchema(CodeCommitJson);
ajvInstance.addSchema(CodeDeployJson);
ajvInstance.addSchema(CodeModuleJson);
ajvInstance.addSchema(CodeRepoJson);
ajvInstance.addSchema(CodeReviewJson);
ajvInstance.addSchema(ConfigurationJson);
ajvInstance.addSchema(ContainerJson);
ajvInstance.addSchema(ControlJson);
ajvInstance.addSchema(ControlPolicyJson);
ajvInstance.addSchema(CryptoKeyJson);
ajvInstance.addSchema(DataCollectionJson);
ajvInstance.addSchema(DataObjectJson);
ajvInstance.addSchema(DataStoreJson);
ajvInstance.addSchema(DatabaseJson);
ajvInstance.addSchema(DeploymentJson);
ajvInstance.addSchema(DeviceJson);
ajvInstance.addSchema(DirectoryJson);
ajvInstance.addSchema(DiskJson);
ajvInstance.addSchema(DocumentJson);
ajvInstance.addSchema(DomainJson);
ajvInstance.addSchema(DomainRecordJson);
ajvInstance.addSchema(DomainZoneJson);
ajvInstance.addSchema(EntityJson);
ajvInstance.addSchema(FindingJson);
ajvInstance.addSchema(FirewallJson);
ajvInstance.addSchema(FrameworkJson);
ajvInstance.addSchema(FunctionJson);
ajvInstance.addSchema(GatewayJson);
ajvInstance.addSchema(GraphObjectJson);
ajvInstance.addSchema(GroupJson);
ajvInstance.addSchema(HostJson);
ajvInstance.addSchema(HostAgentJson);
ajvInstance.addSchema(ImageJson);
ajvInstance.addSchema(IncidentJson);
ajvInstance.addSchema(InternetJson);
ajvInstance.addSchema(IpAddressJson);
ajvInstance.addSchema(IssueJson);
ajvInstance.addSchema(KeyJson);
ajvInstance.addSchema(LogsJson);
ajvInstance.addSchema(ModelJson);
ajvInstance.addSchema(ModuleJson);
ajvInstance.addSchema(NetworkJson);
ajvInstance.addSchema(NetworkEndpointJson);
ajvInstance.addSchema(NetworkInterfaceJson);
ajvInstance.addSchema(OrganizationJson);
ajvInstance.addSchema(PRJson);
ajvInstance.addSchema(PasswordPolicyJson);
ajvInstance.addSchema(PersonJson);
ajvInstance.addSchema(PolicyJson);
ajvInstance.addSchema(ProblemJson);
ajvInstance.addSchema(ProcedureJson);
ajvInstance.addSchema(ProcessJson);
ajvInstance.addSchema(ProductJson);
ajvInstance.addSchema(ProgramJson);
ajvInstance.addSchema(ProjectJson);
ajvInstance.addSchema(QuestionJson);
ajvInstance.addSchema(QueueJson);
ajvInstance.addSchema(RecordJson);
ajvInstance.addSchema(RecordEntityJson);
ajvInstance.addSchema(RepositoryJson);
ajvInstance.addSchema(RequirementJson);
ajvInstance.addSchema(ResourceJson);
ajvInstance.addSchema(ReviewJson);
ajvInstance.addSchema(RiskJson);
ajvInstance.addSchema(RootJson);
ajvInstance.addSchema(RuleJson);
ajvInstance.addSchema(RulesetJson);
ajvInstance.addSchema(ScannerJson);
ajvInstance.addSchema(SecretJson);
ajvInstance.addSchema(SectionJson);
ajvInstance.addSchema(ServiceJson);
ajvInstance.addSchema(SiteJson);
ajvInstance.addSchema(StandardJson);
ajvInstance.addSchema(SubscriptionJson);
ajvInstance.addSchema(TaskJson);
ajvInstance.addSchema(TeamJson);
ajvInstance.addSchema(ThreatIntelJson);
ajvInstance.addSchema(TrainingJson);
ajvInstance.addSchema(UserJson);
ajvInstance.addSchema(UserGroupJson);
ajvInstance.addSchema(VaultJson);
ajvInstance.addSchema(VendorJson);
ajvInstance.addSchema(VulnerabilityJson);
ajvInstance.addSchema(WeaknessJson);
ajvInstance.addSchema(WorkflowJson);
ajvInstance.addSchema(WorkloadJson);
}
46 changes: 46 additions & 0 deletions tools/generate-register-schemas-function.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash

# absolute path to where this script is located
here=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )

cd $here;

# Define the path to the directory containing the JSON schemas
schemaDir="../src/schemas"

# Define the output TypeScript file
outputFile="../src/registerSchemas.ts"

# Write the fixed part of the template to the outputFile
echo "" > "$outputFile"
echo "// THIS FILE IS AUTOMATICALLY GENERATED!" >> "$outputFile"
echo "// using the script tools/generate-register-schemas-function.sh" >> "$outputFile"
echo "" >> "$outputFile"
echo "import Ajv from 'ajv';" >> "$outputFile"
echo "" >> "$outputFile"

# Dynamically generate import statements and ajvInstance.addSchema calls
importStatements=""
schemaCalls=""

# Loop over the JSON files in the schema directory without changing the current directory
for file in "$schemaDir"/*.json; do
# Extract just the filename from the path
filename=$(basename -- "$file")

# Remove the file extension to get the schema name
schemaName=$(echo "$filename" | sed 's/.json$//')

# Create the import statement
importStatements+="import ${schemaName}Json from './schemas/${filename}';"$'\n'

# Create the ajvInstance.addSchema call
schemaCalls+=" ajvInstance.addSchema(${schemaName}Json);"$'\n'
done

# Write the dynamic parts to the outputFile
echo "// Dynamically imported schemas" >> "$outputFile"
echo "$importStatements" >> "$outputFile"
echo "export function registerSchemas(ajvInstance: Ajv): void {" >> "$outputFile"
echo "$schemaCalls" >> "$outputFile"
echo "}" >> "$outputFile"

0 comments on commit dfd3b61

Please sign in to comment.