Skip to content

Async validation

Alan Berdinelli edited this page Apr 18, 2021 · 4 revisions

You can use the static validate method which is async:

// From v1.4.2 you can pass an object directly to validate method:
const someSchema = {
    name: {
        type: String
    }
};

const someSchemy = new Schemy({
    name: {
        type: String
    }
});

async function doStuff() {
    await Schemy.validate({name: 'Schemy'}, someSchema); // => { name: 'Schemy' }
}

Promisified version

Of course, schemy supports promise too:

function doStuff() {
    Schemy
        .validate(input, someSchema)
        .then(validatedBody => {
            console.log(validatedBody.name);
        })
        .catch(err => {})
}

Clone this wiki locally