6.0.3 • Published 4 months ago

ducktyper v6.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

DuckTyper

A tool for validating input based on the fundamental js idea of duck typing. The concept of this is to allow the fluidness of a dynamically typed language with the clarity of a static one. It takes advantage of a typing that is commonly known in js as duck typing. Meaning, If it walks and talks like a duck it's a duck.

Ducktyper has the single goal of having simple and clear syntax. It follows similar design patterns of styled-components and class-validator. With ducktyper, validators are easy, quick, and clear to make. By allowing complex validaton that stricter validation libraries don't provide, ducktyper puts validation in the hands of the developer as though it was a typing system.

This is a list of the main functions that ducktyper provides. The main two are makeDuck and duckfaults which allow you do make any validation you could hope for.

FunctionsDescription
makeDuck(...types): isDuckUsed to create a type. Any of the basic types can be used within it along with isDuck types
dtoToIsDuck(DuckDto): isDuckThis is used to convert any DuckDto to an isDuck function so that dtos can be used as valid types in a makeDuck
duckorate(isDuck, options): duckorator(val)this is used to make a new decorator to place on a class.
duckfaults(isDuck, options): isDuckThis is used to chain options into isDuck. Returns an isDuck that will be called with the provided options as its new defaults
isDuck(val, options): Bool/ErrorThis is the type that is used to check a value. This is generated by makeduck
classifyDuck(dto, options): Bool/ErrorThis is used in the same way that an isDuck is used. However it takes in a dto with duckorators as input.
initClassifyDuckOptions(obj)this is used to update the default options that are used when classifyDuck is called on a dto
initIsDuckOptions(obj)this is used to update the default options when makeDuck is called to initalize a isDuck
trimDuck(duck, DuckDto)this is used to remove any extra fields from the duck dto

These are frequently used types that will allow you to save overhead time rather then having to make a new validator every time you need a basic type. If there are any common types you would like added raise an issue in the github.

CommonDescription
isStringchecks if input is a string
isNumberchecks if an input is a number
isObjectchecks if an input is an object
isBooleanchecks if an input is a bool
isArraychecks if input is an array

These are a list of accepted types which you can put into makeDuck. Most of which are generic types that most languages have. They can be inserted by themselves, or they can be placed in an array or object.

Basic TypesDescription
initialized primitives ("string", 1234, ...etc)Will accept any value that is equal to the primitive passed in
Primitives (String, Number, ...etc)Will accept any primative value https://developer.mozilla.org/en-US/docs/Glossary/Primitive
ArrayHas two types of arrays: single type arrays and structured arrays
ObjectAn object with fields any of the accepted types
isDuckthe type returned when a type is created
Any: functionIndicates that anything is accepted
Class: objectYou can use any type of class that you would like. This includes custom and defaut js classes
function(val):booleanThis is used when a value has specifics that it must follow other then generic types
DuckDtoa Duck dto can be used as a type inside of a makeDuck function

These are a list of option that you can attach to a isDuck or call at runtime.

OptionsDescription
throw: BooleanValue indicating if a boolean for success will be returned or if it will throw a message on error
allowUndefiend: BooleanValue indicating if an undefined input will be accepted
message: StringThe message that is thrown when input fails to pass tests
allowEmpty: BooleanBy default this is true. However when you want to make sure an array or string is not empty set this to false
allowEmptyString: BooleanBy default this is not included. However when you want to make sure a string is not empty set this to false if it is okay set it to true. This takes precidence over allowEmpty
allowEmptyArray: BooleanBy default this is not included. However when you want to make sure an array is not empty set this to false if it is okay set it to true. This takes precidence over allowEmpty
childMessage: BooleanBy default this is set as true. What this does is signify that we want the message given by the child if any are given. If it is set to false it will return the parent message every time
forceDuckDto: BooleanThis is used to force that all functions which take a dto as a parameter to force the use of a duckDto.

functional usage

import {makeDuck, duckfaults, Any} from "ducktyper";
 
//here we create an object that could be passed into any of these types
let person = {
   name: "Benjamin",
   age: 22,
   address: ["city", "state", 1234, "road"]
   children: [{name:"goliath"}, {name:"fin"}],
   employed: true,
   single: true,
}
 
 
const isName = duckfaults(isString, {
   message: "name must be a string"
});
 
//we can attach default options
//and we can use custom validation functions
const isAge = duckfaults(makeDuck(v=>v>=0), {
   throw: true,
   error: "failed to provide age field"
});
 
//we can do a structured array
const isAddress = makeDuck([String, String, Number, String]);
 
const isChildren = makeDuck([isNamed]);

const hasId = makeDuck({
   id: Number,
});
 
//can combine ducks into one big duck and also sinply define a schema.
const isPerson = makeDuck({
   name: isName,
   age: isAge,
   address: isAddress,
   children: isChildren
}, hasId);
 
//usage
isPerson(person)

dto usage

Ducktyper can now be used to decorate classes with the use of dtos in mind. This is supported in both js and ts!

import {makeDuck, duckorate, classifyDuck, DuckDto} from "ducktyper";

const isQuack = makeDuck((val)=>"quack"===val);
const isEats = makeDuck((val)=>val==="bread" || val==="seeds");

class Duck extends DuckDto {
   @duckorate(isQuack)
   sound;

   @duckorate(isEats)
   eats;   
}

let duck = new DuckDto();
duck.sound = "quack";
duck.eats = "bannanas";

try {
   classifyDuck(duck, {throw:true});
} catch (error) {
   alert(error.message);
}
6.0.3

4 months ago

6.0.2

4 months ago

6.0.1

7 months ago

6.0.0

7 months ago

5.1.13

7 months ago

5.1.12

7 months ago

5.1.11

7 months ago

5.1.10

7 months ago

5.1.9

7 months ago

5.1.8

7 months ago

5.1.7

7 months ago

5.1.5

10 months ago

5.1.4

1 year ago

5.1.6

10 months ago

5.1.3

1 year ago

5.1.2

1 year ago

5.1.1

1 year ago

5.0.2

1 year ago

5.1.0

1 year ago

5.0.1

1 year ago

5.0.0

1 year ago

2.2.0

2 years ago

2.1.0

2 years ago

3.0.3

2 years ago

3.2.0

1 year ago

3.1.1

1 year ago

3.0.2

2 years ago

3.1.0

1 year ago

3.0.1

2 years ago

3.0.0

2 years ago

4.3.2

1 year ago

4.3.1

1 year ago

4.2.2

1 year ago

4.1.0

1 year ago

4.0.1

1 year ago

4.0.0

1 year ago

4.3.0

1 year ago

4.2.1

1 year ago

4.2.0

1 year ago

1.1.0

2 years ago

2.0.0

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago