From c087aa7078493751ca580437c56f0282f573520e Mon Sep 17 00:00:00 2001 From: Alireza Safaierad Date: Sat, 28 Oct 2023 20:07:09 +0400 Subject: [PATCH] ci: add string parse to boolean schema --- src/Schema/BooleanSchema.spec.ts | 8 ++++++++ src/Schema/BooleanSchema.ts | 3 ++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Schema/BooleanSchema.spec.ts b/src/Schema/BooleanSchema.spec.ts index cca9101..fa75622 100644 --- a/src/Schema/BooleanSchema.spec.ts +++ b/src/Schema/BooleanSchema.spec.ts @@ -37,4 +37,12 @@ describe('Boolean Schema', () => { expect(schema.parse()).toBe(true); expect(schema2.parse()).toBe(false); }); + + it('should parse string to boolean', () => { + const schema = new BooleanSchema().setValue('true'); + const schema2 = new BooleanSchema().setValue('false'); + + expect(schema.parse()).toBe(true); + expect(schema2.parse()).toBe(false); + }); }); diff --git a/src/Schema/BooleanSchema.ts b/src/Schema/BooleanSchema.ts index 5cc98a5..7716a27 100644 --- a/src/Schema/BooleanSchema.ts +++ b/src/Schema/BooleanSchema.ts @@ -2,11 +2,12 @@ import { Schema } from './Schema'; import type { SchemaOptions } from './SchemaOptions'; export class BooleanSchema extends Schema { + private falseRegex = /false/i; constructor(options: SchemaOptions = {}) { super({ ...options, typeConstructor: n => { - console.log(`${n} to boolean`); + if (typeof n === 'string' && this.falseRegex.test(n)) return false; return Boolean(n); },