Skip to content

Commit

Permalink
in-line schema
Browse files Browse the repository at this point in the history
  • Loading branch information
ipatka committed Oct 31, 2023
1 parent 83164b0 commit 49e86f8
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 63 deletions.
62 changes: 59 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { resolve, join, dirname } from 'path'
import { fileURLToPath } from 'url'
import Ajv from 'ajv'

import schema_IPolicyEnginePolicyResponse from './schemas/schema_IPolicyEnginePolicyResponse'

// Native modules are not currently supported with ES module imports.
// https://nodejs.org/api/esm.html#esm_no_native_module_loading
import { createRequire } from 'module'
Expand Down Expand Up @@ -87,7 +85,7 @@ export const isAuthorized = (request: string) => {

export function validatePolicyEngineResponse(maybePolicyEngineResponse: any) {
const ajv = new Ajv()
const validate = ajv.compile(schema_IPolicyEnginePolicyResponse)
const validate = ajv.compile(IPolicyEnginePolicyResponseSchema)
return validate(maybePolicyEngineResponse)
}

Expand Down Expand Up @@ -127,3 +125,61 @@ export const validatePolicy = (request: IValidatePolicyPayload): any => {
const parsedResponse = JSON.parse(result) // TODO validate
return parsedResponse
}

export const IPolicyEnginePolicyResponseSchema = {
"type": "object",
"properties": {
"reasons": {
"type": "array",
"items": {
"$ref": "#/definitions/IPolicyStatementResult"
}
},
"decision": {
"$ref": "#/definitions/PolicyDecision"
},
"errors": {
"type": "array",
"items": {
"type": "string"
}
}
},
"required": [
"decision",
"errors",
"reasons"
],
"definitions": {
"IPolicyStatementResult": {
"type": "object",
"properties": {
"policy_id": {
"type": "string"
},
"invoked": {
"type": "boolean"
},
"annotations": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"required": [
"annotations",
"invoked",
"policy_id"
]
},
"PolicyDecision": {
"enum": [
"Allow",
"Deny"
],
"type": "string"
}
},
"$schema": "http://json-schema.org/draft-07/schema#"
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shield3/banyan",
"version": "0.3.3",
"version": "0.3.4",
"description": "Banyan policy engine",
"main": "dist/index.js",
"scripts": {
Expand Down
57 changes: 0 additions & 57 deletions schemas/schema_IPolicyEnginePolicyResponse.ts

This file was deleted.

7 changes: 5 additions & 2 deletions scripts/compileSchemas.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { writeFileSync } from 'fs'
import { writeFileSync, appendFileSync } from 'fs'
import { join, resolve } from 'path'
import * as TJS from 'typescript-json-schema'

Expand All @@ -9,6 +9,8 @@ const generate = async () => {

const tsConfigPath = resolve('tsconfig.json')
const outputPath = resolve('schemas/')
const indexFilePath = resolve('index.ts')


// const program = TJS.getProgramFromFiles([resolve('services/utils/Interfaces/CommandCenter/IPolicies.ts')])
const includePaths = [
Expand All @@ -25,7 +27,8 @@ const generate = async () => {
// Get symbols for different types from generator.
const schema = generator.getSchemaForSymbol(symbol)

writeFileSync(join(outputPath, `schema_${symbol}.ts`), `export default ${JSON.stringify(schema, null, 2)}`)
appendFileSync(indexFilePath, `\nexport const ${symbol}Schema = ${JSON.stringify(schema, null, 2)}\n`)


}
}
Expand Down

0 comments on commit 49e86f8

Please sign in to comment.