|
| 1 | +import type {DataValidationCxt} from "../../lib/types" |
| 2 | +import Ajv from "../ajv" |
| 3 | +import chai from "../chai" |
| 4 | + |
| 5 | +chai.should() |
| 6 | + |
| 7 | +type OasMode = "request" | "response" |
| 8 | + |
| 9 | +function buildContext(data: unknown): DataValidationCxt { |
| 10 | + const dynamicAnchors: DataValidationCxt["dynamicAnchors"] = {} |
| 11 | + return { |
| 12 | + instancePath: "", |
| 13 | + parentData: {root: data}, |
| 14 | + parentDataProperty: "root", |
| 15 | + rootData: data as Record<string, unknown>, |
| 16 | + dynamicAnchors, |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +function getAjv() { |
| 21 | + return new Ajv({passContext: true}) |
| 22 | +} |
| 23 | + |
| 24 | +function getOasContext(mode: OasMode, location?: "requestBody" | "responseBody") { |
| 25 | + return {oas: {mode, location}} |
| 26 | +} |
| 27 | + |
| 28 | +type Validate = ReturnType<InstanceType<typeof Ajv>["compile"]> |
| 29 | + |
| 30 | +function expectValid( |
| 31 | + validate: Validate, |
| 32 | + data: unknown, |
| 33 | + ctx?: {oas: {mode: OasMode; location?: "requestBody" | "responseBody"}} |
| 34 | +) { |
| 35 | + const valid = ctx ? validate.call(ctx, data, buildContext(data)) : validate(data) |
| 36 | + valid.should.equal(true) |
| 37 | +} |
| 38 | + |
| 39 | +function expectInvalid( |
| 40 | + validate: Validate, |
| 41 | + data: unknown, |
| 42 | + keyword: "readOnly" | "writeOnly" | "required", |
| 43 | + ctx?: {oas: {mode: OasMode; location?: "requestBody" | "responseBody"}} |
| 44 | +) { |
| 45 | + const valid = ctx ? validate.call(ctx, data, buildContext(data)) : validate(data) |
| 46 | + valid.should.equal(false) |
| 47 | + validate.errors?.[0].keyword.should.equal(keyword) |
| 48 | +} |
| 49 | + |
| 50 | +describe("readOnly/writeOnly", () => { |
| 51 | + const baseSchema = { |
| 52 | + type: "object", |
| 53 | + properties: { |
| 54 | + id: {type: "string", readOnly: true}, |
| 55 | + password: {type: "string", writeOnly: true}, |
| 56 | + }, |
| 57 | + } |
| 58 | + |
| 59 | + describe("default behavior without OAS context", () => { |
| 60 | + it("should not validate readOnly/writeOnly", () => { |
| 61 | + const validate = getAjv().compile(baseSchema) |
| 62 | + expectValid(validate, {id: "1", password: "secret"}) |
| 63 | + expectValid(validate, {}) |
| 64 | + }) |
| 65 | + }) |
| 66 | + |
| 67 | + describe("with required", () => { |
| 68 | + const schemaWithRequired = { |
| 69 | + ...baseSchema, |
| 70 | + required: ["id", "password"], |
| 71 | + } |
| 72 | + |
| 73 | + it("should keep required without context", () => { |
| 74 | + const validate = getAjv().compile(schemaWithRequired) |
| 75 | + expectValid(validate, {id: "1", password: "secret"}) |
| 76 | + expectInvalid(validate, {}, "required") |
| 77 | + }) |
| 78 | + |
| 79 | + it("should skip readOnly in request context and still require writeOnly", () => { |
| 80 | + const validate = getAjv().compile(schemaWithRequired) |
| 81 | + const requestCtx = getOasContext("request", "requestBody") |
| 82 | + |
| 83 | + expectValid(validate, {password: "secret"}, requestCtx) |
| 84 | + expectInvalid(validate, {id: "1", password: "secret"}, "readOnly", requestCtx) |
| 85 | + expectInvalid(validate, {}, "required", requestCtx) |
| 86 | + }) |
| 87 | + |
| 88 | + it("should skip writeOnly in response context and still require readOnly", () => { |
| 89 | + const validate = getAjv().compile(schemaWithRequired) |
| 90 | + const responseCtx = getOasContext("response", "responseBody") |
| 91 | + |
| 92 | + expectValid(validate, {id: "1"}, responseCtx) |
| 93 | + expectInvalid(validate, {id: "1", password: "secret"}, "writeOnly", responseCtx) |
| 94 | + expectInvalid(validate, {}, "required", responseCtx) |
| 95 | + }) |
| 96 | + }) |
| 97 | + |
| 98 | + describe("presence validation with context", () => { |
| 99 | + it("should reject readOnly in request context", () => { |
| 100 | + const validate = getAjv().compile(baseSchema) |
| 101 | + const requestCtx = getOasContext("request", "requestBody") |
| 102 | + |
| 103 | + expectInvalid(validate, {id: "1"}, "readOnly", requestCtx) |
| 104 | + }) |
| 105 | + |
| 106 | + it("should reject writeOnly in response context", () => { |
| 107 | + const validate = getAjv().compile(baseSchema) |
| 108 | + const responseCtx = getOasContext("response", "responseBody") |
| 109 | + |
| 110 | + expectInvalid(validate, {password: "secret"}, "writeOnly", responseCtx) |
| 111 | + }) |
| 112 | + }) |
| 113 | +}) |
0 commit comments