This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Kamto Eddy
clean-schema is a schema validator whose primary focus is the access
, modification
& the interaction
of properties of a model with one another.
Don't worry if that makes little sense. I'm still figuring out a way to concisely describe what clean-schema really does.
There are a lot of schema validators on the NPM registry and we can agree that a good number of them do their thing really well. So how is this one different?
Here's how to define a model:
const { Schema } = require("clean-schema");
const PostModel = new Schema({
approximateTimeToRead: {
default: "",
dependent: true
},
content: {
required: true,
onChange: setApproximateTimeToRead,
validator: validatePostContent
},
id: {
constant: true,
value: generatePostId
}
}).getModel();
function setApproximateTimeToRead({ content }){
// your logic to calculate time to read a post
// based on it's content
const approximateTimeToRead = calculateTimeToRead(content)
// update the value of 'approximateTimeToRead'
return { approximateTimeToRead }
}
This is what a simple post's model(with 3 properties; approximateTimeToRead, content & id) would look like.
Now, let me explain what I meant when I said clean-schema focuses on the access
, modification
& the interaction
of properties of a model with one another.
access & modification
-
approximateTimeToRead
is defined as a dependent property makes it impossible for it's value to be modified outside of the model(I'll explain with code later) -
id
is defined as a constant so only the value provided or generated would be considered
interaction of properties of a model with one another
As you can see approximateTimeToRead
's value is based on the value of the content
Creating a post
const { data, error } = await PostModel.create({
id: null,
content: "A test post",
approximateTimeToRead: "2 years"
});
console.log(data);
// {
// approximateTimeToRead: "0.25 seconds",
// content: "A test post",
// id: "generated-post-id-1"
// }
I'd like to add that this is a TypeScript-first module.
Here's the post model in TypeScript:
import { Schema } from "clean-schema"
type PostType = {
approximateTimeToRead: string;
content: string;
id: string;
}
const PostModel = new Schema<PostType>({
approximateTimeToRead: {
default: "",
dependent: true
},
content: {
required: true,
onChange: setApproximateTimeToRead,
validator: validatePostContent
},
id: {
constant: true,
value: generatePostId
}
}).getModel();
function setApproximateTimeToRead({ content }: PostType){
// your logic to calculate time to read a post
// based on it's content
const approximateTimeToRead = calculateTimeToRead(content)
// update the value of 'approximateTimeToRead'
return { approximateTimeToRead }
}
I hope it makes more sense now.
The only form of documentation available at the moment is the readme in the github repo. I'll explain more on how to use it in subsequent posts.
Thanks for reading
This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Kamto Eddy
Kamto Eddy | Sciencx (2022-12-24T21:02:34+00:00) What is clean-schema?. Retrieved from https://www.scien.cx/2022/12/24/what-is-clean-schema/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.