Skip to content

Commit

Permalink
ci: add string parse to boolean schema
Browse files Browse the repository at this point in the history
  • Loading branch information
ASafaeirad committed Oct 28, 2023
1 parent 80cbd43 commit c087aa7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/Schema/BooleanSchema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
3 changes: 2 additions & 1 deletion src/Schema/BooleanSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { Schema } from './Schema';
import type { SchemaOptions } from './SchemaOptions';

export class BooleanSchema<TInput = any> extends Schema<TInput, boolean> {
private falseRegex = /false/i;
constructor(options: SchemaOptions<boolean> = {}) {
super({
...options,
typeConstructor: n => {
console.log(`${n} to boolean`);
if (typeof n === 'string' && this.falseRegex.test(n)) return false;

return Boolean(n);
},
Expand Down

0 comments on commit c087aa7

Please sign in to comment.