🥳19. Chai ile Schema Oluşturma Örnekleri

1. Örnek

// Data
const validData = {
    name: "Furkan", // required field => string
    age: 24 // optional => null or number
}

const invalidData = {
    name: 24, // required field => string
    age: "Furkan" // optional => null or number
}

// Schema
const dataSchema = {
    title: "Data Schema",
    type: "object",
    required: ["name"],
    properties: {
        name: {
            type: "string"
        },
        age: {
            type: ["number", "null"],
        }
    }
}

Cypress testlerini yazalım.

// Cypress test
it("test schema with valid data", () => {
    expect(validData).to.be.jsonSchema(dataSchema);
})
 
// Cypress test
it("test schema with invalid data", () => {
    expect(badData).to.not.be.jsonSchema(dataSchema);
})

2.Örnek

// Data
const goodApple = {
    skin: "thin",
    colors: ["red", "green", "yellow"],
    taste: 10,
};

const badApple = {
    colors: ["brown"],
    taste: 0,
    worms: 2,
};

// Schema
const fruitSchema = {
	title: "fresh fruit schema ",
	type: "object",
	required: ["skin", "colors", "taste"],
	properties: {
		colors: {
		   type: ["array", "null"], // null değerleri de kabul eder
		   minItems: 1,
		   uniqueItems: true,
		   items: {
			   type: "string",
		    },
		},
		skin: {
		    type: "string",
		},
		taste: {
		    type: "number",
		    minimum: 5,
		},
	},
};

Cypress testlerini yazalım.

// Cypress test
it("test schema with valid data", () => {
    expect(goodApple).to.be.jsonSchema(fruitSchema);
})
 
// Cypress test
it("test schema with invalid data", () => {
    expect(badApple).to.not.be.jsonSchema(fruitSchema);
})

Last updated