Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
December 24, 2022 09:02 pm GMT

What is clean-schema?

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


Original Link: https://dev.to/kamtoeddy/what-is-clean-schema-41p7

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To