RunKit + npm: ajv

node v8.17.0
endpointsharetweet
var Ajv = require('ajv'); var ajv = new Ajv(); var schema = { "host": "cert", "paths": { "products": { "get": { "responses": { "401": { "description": "Problem with the client request", "headers": { "x-correlator": { "type": "string", "format": "uuid", "description": "Correlation id" } }, "schema": { "$ref": "errors.json#/definitions/Unauthenticated" } } } } }, "products": { "get": { "responses": { "401": { "description": "Problem with the client request", "headers": { "x-correlator": { "type": "string", "format": "uuid", "description": "Correlation id" } }, "schema": { "$ref": "errors.json#/definitions/Unauthenticated" }, "examples": { "application/json": { "code": "UNAUTHENTICATED", "message": "Authentication error" } } } } } } } } var errors_schema = { "info": { "description": "Common errors", "version": "3.0.1", "title": "Common errors", "contact": { "name": "Team" } }, "definitions": { "ModelError": { "type": "object", "required": [ "message" ], "properties": { "message": { "type": "string", "description": "A human readable description" } } }, "Unauthenticated": { "allOf": [ { "type": "object", "required": [ "code" ], "properties": { "code": { "type": "string", "enum": [ "UNAUTHENTICATED" ], "default": "UNAUTHENTICATED", "description": "Request not authenticated due to missing, invalid, or expired credentials." } } }, { "$ref": "#/definitions/ModelError" } ] } } } ajv.addSchema(errors_schema, 'errors.json'); ajv.addSchema(schema, 'swagger.json') var testajv = ajv.compile({ $ref: 'errors.json#/definitions/Unauthenticated' }); console.log(testajv({"code": 123, "message":"token expired"}), testajv.errors); // Fails console.log(testajv({"code": "AAA", "message":"token expired"}), testajv.errors); // Fails console.log(testajv({"code": "UNAUTHENTICATED", "message":"token expired"}), testajv.errors); // Passes var testajv2 = ajv.compile({ $ref: 'swagger.json#/paths/products/get/responses/401/schema' }); console.log(testajv2({"code": 123, "message":"token expired"}), testajv2.errors); // Fails console.log(testajv2({"code": "AAA", "message":"token expired"}), testajv2.errors); // Fails console.log(testajv2({"code": "UNAUTHENTICATED", "message":"token expired"}), testajv2.errors); // Passes
Created from: https://npm.runkit.com/ajv
Loading…