0.3.34 • Published 1 year ago

@phil-rice/validation v0.3.34

Weekly downloads
5
License
MIT
Repository
github
Last release
1 year ago

This is library for single field validation.

This is a common requirement in my applications: objects are parsed from JSON and I want to make sure that they meet a contract

I want a list of all the error messages, and I would like it if the code reads declarative.

The return type of the validation is a string[] or a Promise<string[]>

Approach

We wrap the object under test with a validate object then we can do things like the following. The validate object accumulates issues internaly and you can call .issues to see those issues

function validateProjectDetails(v: Validate<ProjectDetails>) {
    return v.isString("name").//
        isString("description").//
        isString("template").//
        isObject("details", validateDetails)
}

function validateDetails(v: Validate<Details>) {
    return v.isBoolean("publish", 'Should the project be published').//
        isArrayofObjects('links', v => v).//
        optObject("extraDeps", v => v, 'These are added to package.json dependencies').//
        optObject("extraDevDeps", v => v, 'These are added to package.json devDependencies').//
        optObject("extraBins", v => v, 'These are added to package.json bin')
}

Extra goodness

When working with CLIs I often want to validate that directories and files exist.

function validateTemplateDirectory(context: string, c: Config, templateDir: string): Promise<string[]> {
    let dir = path.join(c.templateDir, templateDir);
    return Validate.validateDirectoryExists(context, dir).then(dirErrors => dirErrors.length === 0 ?
        Validate.validateFile(`package.json in template directory ${templateDir}`, path.join(dir, 'package.json'), validatePackageJson) :
        dirErrors)
}```
0.3.31

1 year ago

0.3.30

1 year ago

0.3.34

1 year ago

0.3.33

1 year ago

0.3.32

1 year ago

0.2.18

1 year ago

0.3.29

1 year ago

0.2.17

1 year ago

0.3.20

1 year ago

0.3.28

1 year ago

0.2.16

1 year ago

0.3.27

1 year ago

0.2.15

1 year ago

0.3.26

1 year ago

0.2.14

1 year ago

0.3.25

1 year ago

0.3.24

1 year ago

0.3.23

1 year ago

0.3.22

1 year ago

0.3.21

1 year ago

0.3.0

1 year ago

0.3.19

1 year ago

0.3.18

1 year ago

0.3.6

1 year ago

0.3.8

1 year ago

0.3.7

1 year ago

0.3.2

1 year ago

0.3.1

1 year ago

0.3.4

1 year ago

0.3.9

1 year ago

0.3.17

1 year ago

0.3.16

1 year ago

0.3.15

1 year ago

0.3.14

1 year ago

0.3.13

1 year ago

0.3.12

1 year ago

0.3.11

1 year ago

0.3.10

1 year ago

0.2.13

2 years ago

0.2.12

2 years ago

0.2.11

2 years ago

0.2.10

2 years ago

0.2.7

2 years ago

0.2.9

2 years ago

0.2.8

2 years ago

0.2.6

3 years ago

0.2.5

3 years ago

0.2.4

3 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago