Skip to content

Commit

Permalink
feat: adding json schema validator service
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel78uk committed Dec 1, 2023
1 parent 32ef84e commit ea4adf0
Showing 1 changed file with 45 additions and 43 deletions.
88 changes: 45 additions & 43 deletions tests/unit/services/schema-validator.service.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,50 @@ const {
const wqJsonSchema = require('../../../server/services/json-schemas/sirp_json_schema_WQ_v0.3.json')
const wqSampleResponse = require('../../samples/sample_water_quality_incident.json')

describe('validateDataAgainstSchema function', () => {
it('should validate correctly against a valid schema', () => {
const schema = {
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'number' }
},
required: ['name', 'age']
}

const data = {
name: 'John',
age: 30
}

const results = validateDataAgainstSchema(data, schema)

expect(results).toBe(true)
})

it('should return false and log errors for invalid data against schema', () => {
const schema = {
type: 'object',
properties: {
email: { type: 'string', format: 'email' }
},
required: ['email']
}

const data = {
email: 'notAnEmail'
}

const results = validateDataAgainstSchema(data, schema)

expect(results).toBe(false)
})

it('should compile and validate sample against a given schema', () => {
const results = validateDataAgainstSchema(wqSampleResponse,wqJsonSchema)

expect(results).toBe(true)
describe('SchemaValidatorService', () => {
describe('validateDataAgainstSchema', () => {
it('should validate correctly against a valid schema', () => {
const schema = {
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'number' }
},
required: ['name', 'age']
}

const data = {
name: 'John',
age: 30
}

const results = validateDataAgainstSchema(data, schema)

expect(results).toBe(true)
})

it('should return false and log errors for invalid data against schema', () => {
const schema = {
type: 'object',
properties: {
email: { type: 'string', format: 'email' }
},
required: ['email']
}

const data = {
email: 'notAnEmail'
}

const results = validateDataAgainstSchema(data, schema)

expect(results).toBe(false)
})

it('should compile and validate sample against a given schema', () => {
const results = validateDataAgainstSchema(wqSampleResponse, wqJsonSchema)

expect(results).toBe(true)
})
})
})

0 comments on commit ea4adf0

Please sign in to comment.