@benshi.ai/js-sdk
Version:
Benshi SDK
153 lines (130 loc) • 4.53 kB
text/typescript
const fs = require('fs')
const yaml = require('js-yaml');
import Ajv from 'ajv'
describe('Schema validation', () => {
let validate;
beforeAll(() => {
const schemaPath = `${__dirname}/ingest_schema.yml`
const ymlSchema = fs.readFileSync(schemaPath, 'utf8')
const ajv = new Ajv()
const jsonSchema = yaml.load(ymlSchema)
validate = ajv.compile(jsonSchema)
})
describe('Root level', () => {
it('should check a correct first level fields', () => {
const event = {
block: "e-commerce",
d_id: "1f3046ba-cc02-495c-98ea-36598bfb996e",
dn: 0,
ol: true,
os: "Linux x86_64",
props: {
action: 'background'
},
sdk: "js/1.0.0-beta.7.3",
ts: "2022-05-19T18:17:34.694Z",
type: "app",
u_id: "BwM09LMYToesygtMZ6bUGTNArC33",
up: 0
}
const valid = validate(event)
expect(valid).toEqual(true)
})
it('should detect a field with an incorrect type', () => {
const event = {
block: "e-commerce",
d_id: "1f3046ba-cc02-495c-98ea-36598bfb996e",
dn: '0', // error -> should be number
ol: true,
os: "Linux x86_64",
props: {
action: 'background'
},
sdk: "js/1.0.0-beta.7.3",
ts: "2022-05-19T18:17:34.694Z",
type: "app",
u_id: "BwM09LMYToesygtMZ6bUGTNArC33",
up: 0
}
const valid = validate(event)
expect(valid).toEqual(false)
})
it('should detect a additional field', () => {
const event = {
block: "e-commerce",
newField: 'aa',
d_id: "1f3046ba-cc02-495c-98ea-36598bfb996e",
dn: 0,
ol: true,
os: "Linux x86_64",
props: {
action: 'brackground'
},
sdk: "js/1.0.0-beta.7.3",
ts: "2022-05-19T18:17:34.694Z",
type: "app",
u_id: "BwM09LMYToesygtMZ6bUGTNArC33",
up: 0
}
const valid = validate(event)
expect(valid).toEqual(false)
})
it('should detect missing field', () => {
// missing field block
const event = {
d_id: "1f3046ba-cc02-495c-98ea-36598bfb996e",
dn: 0,
ol: true,
os: "Linux x86_64",
props: {
action: 'brackground'
},
sdk: "js/1.0.0-beta.7.3",
ts: "2022-05-19T18:17:34.694Z",
type: "app",
u_id: "BwM09LMYToesygtMZ6bUGTNArC33",
up: 0
}
const valid = validate(event)
expect(valid).toEqual(false)
})
it('should detect an invalid value for a first level field', () => {
const event = {
block: "e-commerce",
d_id: "1f3046ba-cc02-495c-98ea-36598bfb996e",
dn: 0,
ol: true,
os: "Linux x86_64",
props: {
action: 'brackground'
},
sdk: "js/1.0.0-beta.7.3",
ts: "2022-05-19T18:17:34.694Z",
type: "invalid-type",
u_id: "BwM09LMYToesygtMZ6bUGTNArC33",
up: 0
}
const valid = validate(event)
expect(valid).toEqual(false)
})
it('should detect an invalid block', () => {
const event = {
block: "unknown-block",
d_id: "1f3046ba-cc02-495c-98ea-36598bfb996e",
dn: 0,
ol: true,
os: "Linux x86_64",
props: {
action: 'background'
},
sdk: "js/1.0.0-beta.7.3",
ts: "2022-05-19T18:17:34.694Z",
type: "invalid-type",
u_id: "BwM09LMYToesygtMZ6bUGTNArC33",
up: 0
}
const valid = validate(event)
expect(valid).toEqual(false)
})
})
})