1.3.3 • Published 2 years ago

@nextlvlup/object-schema v1.3.3

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

Object Schema

Inspired by Mongoose Schema to apply a predefined Schema to a javascript Object.

npm install @nextlvlup/object-schema

Very basic usage example

import ObjectSchema from '@nextlvlup/object-schema';

// Create User schema
interface User {
    firstName?: string;
    lastName?: string;
    age: number;
    birthday: Date;
    posts: [
        {
            id?: string;
            text: string;
        }
    ],
    permissions: string[];
}

const userSchema = new ObjectSchema<User>({
    firstName: String,
    lastName: String,
    age: { type: Number, default: 20 },
    birthday: { type: Date },
    posts: [
        {
            _id: { type: String, alias: "id" },
            text: { type: String, required: true },
        },
    ],
    permissions: [String],
});

/* Apply Schema an Options
 * the second parameter is optional,
 * valide options: reduce / strictType
 */
userSchema
    .filter(object, { reduce: false, strictType: false })
    .then((result) => /* filtered and reduced object */)
    .catch((err) => /* errors */);

Field Options

OptionInput
typeString / Number / Boolean / Date
requiredtrue / false
defaultany
alias"aliasName"
regex/regexPatern/

Filter Options

OptionDescription
reduceremove empty objects
strictTypeerror if type dont match
  • if reduce is disabled, every empty object will be filled with undefined
  • if strictType is disabled, every wrong type will be converted
1.2.0

2 years ago

1.3.3

2 years ago

1.2.4

2 years ago

1.3.2

2 years ago

1.2.3

2 years ago

1.3.1

2 years ago

1.2.2

2 years ago

1.3.0

2 years ago

1.2.1

2 years ago

1.1.9

3 years ago

1.1.8

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.1.2

4 years ago

1.0.5

4 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago