Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
data-to-jsonschema-to-ts
Advanced tools
Convert plain javascript objects to JSON Schema and TypeScript typings
Convert plain javascript objects to JSON Schema and TypeScript typings.
npm install data-to-jsonschema-to-ts
import {
convertJsonSchemaToTs,
createJsonSchemaValidator,
generateJsonSchemaFromData,
} from "data-to-jsonschema-to-ts";
const TEST_DATA = [
{
id: 1,
name: "John Doe",
age: 30,
email: "email@email.com",
},
{
id: 1,
name: "John Doe",
age: 30,
email: "email@email.com",
address: {
street: "123 Main St",
city: "Anytown",
state: "NY",
zip: "12345",
},
},
{
id: 1,
name: "John Doe",
age: 30,
email: null,
preferences: [{ name: "pref1", value: "value1" }],
},
];
const jsonSchema = generateJsonSchemaFromData(TEST_DATA, "TestData", {
additionalProperties: false,
existingSchema: undefined,
});
console.log(JSON.stringify(jsonSchema, null, 2));
const validator = createJsonSchemaValidator(jsonSchema)
console.log(validator({id: 1, name: "John Doe", age: 30, email: "something@email.com"})); // valid
console.log(validator({id: 1, name: "John Doe", age: 30 })); // invalid, missing email
convertJsonSchemaToTs(generatedSchema, {
additionalProperties: false,
}).then((generatedTs) => {
console.log(generatedTs);
});
Generate a function that validates an object against a JSON Schema.
import { createJsonSchemaValidator } from "data-to-jsonschema-to-ts";
const validator = createJsonSchemaValidator({
type: "object",
properties: {
id: { type: "number" },
name: { type: "string" },
age: { type: "number" },
email: { type: "string" }
},
required: ["id", "name", "age", "email"]
});
validator({
id: 1,
name: "John Doe",
age: 30,
email: "myemail.com"
}); // { valid: true, errors: undefined }
validator({
id: 1,
age: null
}); // { valid: false, errors: [{...}] }
Walk a plain javascript object and call a callback function for each primitive value and it's dot notation path.
import { traverseObject } from "data-to-jsonschema-to-ts";
const obj = {
id: 1,
profile: {
name: "John Doe",
}
}
traverseObject(obj, (value, jsonDotPath) => {
console.log(value, jsonDotPath);
});
// console output:
// 1 "id"
// 'John Doe', "profile.name"
FAQs
Convert plain javascript objects to JSON Schema and TypeScript typings
The npm package data-to-jsonschema-to-ts receives a total of 36 weekly downloads. As such, data-to-jsonschema-to-ts popularity was classified as not popular.
We found that data-to-jsonschema-to-ts demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.