Skip to content

Commit

Permalink
[schema-registry-json] Update validation sample (#26774)
Browse files Browse the repository at this point in the history
  • Loading branch information
minhanh-phan committed Aug 10, 2023
1 parent fc40831 commit dcbfdd3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,19 @@ const groupName = process.env["SCHEMA_REGISTRY_GROUP"] || "AzureSdkSampleGroup";

// Sample Json Schema for user with first and last names
export const schemaObject = {
$schema: "https://json-schema.org/draft/2020-12/schema",
$id: "user",
title: "User",
description: "A user for the product",
type: "object",
properties: {
firstName: {
type: "string",
required: true,
},
lastName: {
type: "string",
required: true,
},
},
required: ["firstName", "lastName"],
};

// Matching TypeScript interface for schema
Expand Down Expand Up @@ -82,9 +80,8 @@ export async function main() {
// Validation using a third party library
const ajv = new Ajv();
const validator = ajv.compile(JSON.parse(schema));
let validators = new Map<string, ValidateFunction>();
const validators = new Map<string, ValidateFunction>();
validators.set(schema, validator);

const validateOptions: DeserializeOptions = {
validateCallback(value, schema) {
const validator = validators.get(schema);
Expand All @@ -93,8 +90,9 @@ export async function main() {
if (!valid) {
throw new Error(JSON.stringify(validator.errors));
}
} else {
throw new Error("Unable to find validator");
}
throw new Error("Unable to find validator");
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,19 @@ const groupName = process.env["SCHEMA_REGISTRY_GROUP"] || "AzureSdkSampleGroup";

// Sample Json Schema for user with first and last names
const schemaObject = {
$schema: "https://json-schema.org/draft/2020-12/schema",
$id: "user",
title: "User",
description: "A user for the product",
type: "object",
properties: {
firstName: {
type: "string",
required: true,
},
lastName: {
type: "string",
required: true,
},
},
required: ["firstName", "lastName"],
};

const schema = JSON.stringify(schemaObject);
Expand Down Expand Up @@ -71,9 +69,8 @@ async function main() {
// Validation using a third party library
const ajv = new Ajv();
const validator = ajv.compile(JSON.parse(schema));
let validators = new Map();
const validators = new Map();
validators.set(schema, validator);

const validateOptions = {
validateCallback(value, schema) {
const validator = validators.get(schema);
Expand All @@ -82,8 +79,9 @@ async function main() {
if (!valid) {
throw new Error(JSON.stringify(validator.errors));
}
} else {
throw new Error("Unable to find validator");
}
throw new Error("Unable to find validator");
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,19 @@ const groupName = process.env["SCHEMA_REGISTRY_GROUP"] || "AzureSdkSampleGroup";

// Sample Json Schema for user with first and last names
export const schemaObject = {
$schema: "https://json-schema.org/draft/2020-12/schema",
$id: "user",
title: "User",
description: "A user for the product",
type: "object",
properties: {
firstName: {
type: "string",
required: true,
},
lastName: {
type: "string",
required: true,
},
},
required: ["firstName", "lastName"],
};

// Matching TypeScript interface for schema
Expand Down Expand Up @@ -82,9 +80,8 @@ export async function main() {
// Validation using a third party library
const ajv = new Ajv();
const validator = ajv.compile(JSON.parse(schema));
let validators = new Map<string, ValidateFunction>();
const validators = new Map<string, ValidateFunction>();
validators.set(schema, validator);

const validateOptions: DeserializeOptions = {
validateCallback(value, schema) {
const validator = validators.get(schema);
Expand All @@ -93,8 +90,9 @@ export async function main() {
if (!valid) {
throw new Error(JSON.stringify(validator.errors));
}
} else {
throw new Error("Unable to find validator");
}
throw new Error("Unable to find validator");
},
};

Expand Down

0 comments on commit dcbfdd3

Please sign in to comment.