Skip to content

Commit

Permalink
Merge pull request #1072 from JupiterOne/APP-15141
Browse files Browse the repository at this point in the history
[APP-15141] - Replace validateEntityWithSchema
  • Loading branch information
Geovanni Pacheco committed May 8, 2024
2 parents 06d649d + 2bd2a7a commit e4af097
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@ and this project adheres to
# Unreleased

- Add expiremental `OnDiskDuplicateKeyTracker`
- Replace validateEntityWithSchema

# 12.6.0 - 2024-04-24

Expand Down
3 changes: 2 additions & 1 deletion packages/integration-sdk-core/package.json
Expand Up @@ -23,7 +23,8 @@
"prepack": "yarn build:dist"
},
"dependencies": {
"@jupiterone/data-model": "^0.55.0",
"@jupiterone/data-model": "^0.57.0",
"@jupiterone/integration-sdk-entity-validator": "^12.7.0-alpha.0",
"lodash": "^4.17.21"
},
"devDependencies": {
Expand Down
@@ -1,8 +1,9 @@
import {
getSchema,
IntegrationEntitySchema,
validateEntityWithSchema,
entitySchemas,
} from '@jupiterone/data-model';
import { EntityValidator } from '@jupiterone/integration-sdk-entity-validator';

import { IntegrationError } from '../errors';
import { Entity, EntityRawData } from '../types';
Expand All @@ -11,6 +12,9 @@ import { validateRawData } from './rawData';
import { assignTags, ResourceTagList, ResourceTagMap } from './tagging';

const SUPPORTED_TYPES = ['string', 'number', 'boolean'];
const VALIDATOR = new EntityValidator({
schemas: Object.values(entitySchemas),
});

/**
* Properties to be assigned to a generated entity which are declared in code
Expand Down Expand Up @@ -92,6 +96,40 @@ export type IntegrationEntityBuilderInput = {
// integrations.
};

const validateEntityWithSchema = (entity: GeneratedEntity) => {
const { isValid, errors, skippedSchemas } = VALIDATOR.validateEntity(entity);

const classNotFound = skippedSchemas?.find(
(skippedSchema) =>
skippedSchema.reason === 'not-found' && skippedSchema.type === 'class',
);

if (classNotFound) {
throw new Error(
`Could not find schema for class ${classNotFound.schemaId.replace(
'#',
'',
)}!`,
);
}

const validationType = skippedSchemas?.find(
(skippedSchema) =>
skippedSchema.type === 'class' &&
skippedSchema.reason === 'type-already-validated',
)
? 'type'
: 'class';

if (!isValid) {
throw new Error(
`Entity fails to validate as ${validationType} '${
validationType === 'type' ? entity._type : entity._class
}':\n\n${JSON.stringify(errors, null, 2)}`,
);
}
};

/**
* Generates an `Entity` using the provided `entityData`.
*/
Expand Down
3 changes: 2 additions & 1 deletion packages/integration-sdk-core/tsconfig.json
Expand Up @@ -3,5 +3,6 @@
"compilerOptions": {
"outDir": "dist"
},
"exclude": ["**/dist/*"]
"exclude": ["**/dist/*"],
"references": [{ "path": "../integration-sdk-entity-validator" }]
}
8 changes: 8 additions & 0 deletions yarn.lock
Expand Up @@ -2427,6 +2427,14 @@
ajv "^8.0.0"
ajv-formats "^2.0.0"

"@jupiterone/data-model@^0.57.0":
version "0.57.0"
resolved "https://registry.yarnpkg.com/@jupiterone/data-model/-/data-model-0.57.0.tgz#f543ba0bfad34ef5820ec6155fd3d77cb6e9ee08"
integrity sha512-AK/F8TQ7QVdz9ULujaHNYcBPiktAoq/MUVTdEEjSBe8ycDNPC5AskwZVKJtBD69XzdZ80jHum3YdrhKnoU0y9Q==
dependencies:
ajv "^8.0.0"
ajv-formats "^2.0.0"

"@jupiterone/hierarchical-token-bucket@^0.3.1":
version "0.3.1"
resolved "https://registry.yarnpkg.com/@jupiterone/hierarchical-token-bucket/-/hierarchical-token-bucket-0.3.1.tgz#b4bda4b5b1eed2fde3db776ce7241162c83b9b01"
Expand Down

0 comments on commit e4af097

Please sign in to comment.