Skip to content

Commit

Permalink
refactor: modular format registration
Browse files Browse the repository at this point in the history
  • Loading branch information
eyadmba committed Apr 4, 2024
1 parent 70d0878 commit f356be1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
14 changes: 2 additions & 12 deletions src/IntegrationSchema.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import Ajv from 'ajv';
import addFormats from 'ajv-formats';

const ipv4 = addFormats.get('ipv4') as RegExp;
const ipv6 = addFormats.get('ipv6') as RegExp;
const ipv4CidrRegex = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/(3[0-2]|[12]?[0-9])$/;
const ipv6CidrRegex = /^([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])$|^([0-9a-fA-F]{1,4}:){1,7}:\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])$|^::\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])$|^([0-9a-fA-F]{1,4}:){1,7}[0-9a-fA-F]{1,4}$/;
import { registerFormats } from './registerFormats';

// JSON Schema allows an object to contain properties that are not specified by
// the schema. This can be disabled with `additionalProperties: false`. Ajv then
Expand All @@ -24,14 +19,9 @@ const ipv6CidrRegex = /^([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}\/(12[0-8]|1[01]
export const IntegrationSchema = new Ajv({
// Ignore "excludes", "multiple"; used in JupiterOne UI?
strictSchema: false,
formats: {
ip: (x) => ipv4.test(x) || ipv6.test(x),
ipCidr: (x) => ipv4CidrRegex.test(x) || ipv6CidrRegex.test(x),
},
});

// Install ajv-formats
addFormats(IntegrationSchema);
registerFormats(IntegrationSchema);

// Schema Imports : generated by tools/generate-schema-imports.sh
import WorkloadJson from './schemas/Workload.json';
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export { registerSchemas } from './registerSchemas';
export { registerFormats } from './registerFormats';

export { EVERYONE, INTERNET } from './globalEntities';

export { RelationshipClass } from './RelationshipClass';
Expand All @@ -21,5 +24,4 @@ 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';
24 changes: 24 additions & 0 deletions src/registerFormats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import addFormats from 'ajv-formats';
import Ajv from 'ajv';

const ipv4 = addFormats.get('ipv4') as RegExp;
const ipv6 = addFormats.get('ipv6') as RegExp;
const ipv4CidrRegex = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/(3[0-2]|[12]?[0-9])$/;
const ipv6CidrRegex = /^([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])$|^([0-9a-fA-F]{1,4}:){1,7}:\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])$|^::\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])$|^([0-9a-fA-F]{1,4}:){1,7}[0-9a-fA-F]{1,4}$/;

const isValidIpFormat = {
name: 'ip',
fn: (x) => ipv4.test(x) || ipv6.test(x),
};

const isValidIpCidrFormat = {
name: 'ipCidr',
fn: (x) => ipv4CidrRegex.test(x) || ipv6CidrRegex.test(x),
};

export function registerFormats(ajvInstance: Ajv): void {
ajvInstance.addFormat(isValidIpFormat.name, isValidIpFormat.fn);
ajvInstance.addFormat(isValidIpCidrFormat.name, isValidIpCidrFormat.fn);

addFormats(ajvInstance);
}

0 comments on commit f356be1

Please sign in to comment.