UNPKG

@apistudio/apim-cli

Version:

CLI for API Management Products

90 lines (74 loc) 2.7 kB
/** * Copyright IBM Corp. 2024, 2025 */ import { invalidSchemaAssertion, validSchemaAssertion, } from '../../src/helpers/schema-validator.helper.js'; describe('SchemaValidatorHelper', () => { describe('validSchemaAssertion', () => { it('should return valid and validate when data matches schema', () => { const schema = { type: 'object', properties: { name: { type: 'string' } }, required: ['name'], }; const data = { name: 'John Doe' }; expect(() => validSchemaAssertion(data, schema)).not.toThrow(); }); it('should return invalid and validate when data does not match schema', () => { const schema = { type: 'object', properties: { name: { type: 'string' } }, required: ['name'], }; const data = { age: 30 }; expect(() => validSchemaAssertion(data, schema)).toThrow( "must have required property 'name'", ); }); it('should throw an error when schema is invalid', () => { const invalidSchema = 'invalid schema'; expect(() => validSchemaAssertion({}, invalidSchema)).toThrow( 'Unexpected token \'i\', "invalid schema" is not valid JSON', ); }); it('should throw an error when schema is null', () => { expect(() => validSchemaAssertion({}, null)).toThrow('Invalid schema'); }); }); describe('invalidSchemaAssertion', () => { it('should return invalid and validate when data doesnt matches schema', () => { const schema = { type: 'object', properties: { name: { type: 'string' } }, required: ['name'], }; const data = { name: 'John Doe' }; expect(() => invalidSchemaAssertion(data, schema)).toThrow( 'Schema is valid', ); }); it('should return valid and validate when data does not match schema', () => { const schema = { type: 'object', properties: { name: { type: 'string' } }, required: ['name'], }; const data = { age: 30 }; expect(() => invalidSchemaAssertion(data, schema)).not.toThrow(); }); it('should throw an error when schema is invalid', () => { const invalidSchema = 'invalid schema'; expect(() => invalidSchemaAssertion({}, invalidSchema)).toThrow( 'Unexpected token \'i\', "invalid schema" is not valid JSON', ); }); it('should throw an error when schema is null', () => { expect(() => invalidSchemaAssertion({}, null)).toThrow('Invalid schema'); }); it('should throw an error when schema is invalid', () => { expect(() => invalidSchemaAssertion({}, 3)).toThrow('Invalid schema'); }); }); });