Giter Site home page Giter Site logo

Comments (4)

KhafraDev avatar KhafraDev commented on May 29, 2024 1

I came up with something like this, but a little too lazy to write tests

diff --git a/src/validators/BaseValidator.ts b/src/validators/BaseValidator.ts
index 60d719e..873ffca 100644
--- a/src/validators/BaseValidator.ts
+++ b/src/validators/BaseValidator.ts
@@ -80,6 +80,18 @@ export abstract class BaseValidator<T> {
                return this.constraints.reduce((v, constraint) => constraint.run(v).unwrap(), this.handle(value).unwrap()) as R;
        }
 
+       public is<R extends T = T>(value: unknown): value is R {
+              return this.run(value).isOk()
+       }
+
        /**
         * Sets if the validator should also run constraints or just do basic checks.
         * @param isValidationEnabled Whether this validator should be enabled or disabled. You can pass boolean or a function returning boolean which will be called just before parsing
         * 

from shapeshift.

imranbarbhuiya avatar imranbarbhuiya commented on May 29, 2024

this is already possible

import { s } from '@sapphire/shapeshift'

const schema = s.object({
  a: s.string,
  b: s.number
})

const obj: any = {
  a: 'hello',
  b: 1
}

const parsedObj = schema.parse(obj)

parsedObj // InferType<typeof schema>

from shapeshift.

KhafraDev avatar KhafraDev commented on May 29, 2024

this is already possible

That example doesn't work if the object doesn't match the schema (it will throw an error). I'd like a simple type guard without the extra baggage that throwing an error incurs.

from shapeshift.

imranbarbhuiya avatar imranbarbhuiya commented on May 29, 2024

ig schema.handle(obj) works this won't run https://github.com/sapphiredev/shapeshift/blob/main/src/validators/BaseValidator.ts#L80

Edit: I didn't read the issue properly.

from shapeshift.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.