Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Source/Core/validation/evaluateRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import { DateOnly, Guid, TimeOnly, TimeSpan } from '@cratis/fundamentals';
import type { Rule } from './Rule.js';
import { compareValues } from './compareValues.js';

const whiteSpaceOnly = /^\p{White_Space}*$/u;

const empty = (value: unknown): boolean => value == null || value === '' || value === 0 || value === false ||
Array.isArray(value) && !value.length || typeof value === 'string' && !value.trim() ||
Array.isArray(value) && !value.length || typeof value === 'string' && whiteSpaceOnly.test(value) ||
value instanceof Guid && (value as Guid).equals(Guid.empty);

function equal(left: unknown, right: unknown): boolean {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) Cratis. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
import { BaseValidator } from '../../BaseValidator.js';
import { evaluateRule } from '../../evaluateRule.js';

describe('when evaluating empty with byte order mark', () => {
let result: boolean;
beforeEach(async () => {
const validator = new BaseValidator<{ value: unknown }>();
validator.ruleFor(model => model.value).empty();
result = await evaluateRule(validator.rules[0]!, '\uFEFF', { value: '\uFEFF' }, new AbortController().signal);
});
it('should reject the value', () => { result.should.equal(false); });
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) Cratis. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
import { BaseValidator } from '../../BaseValidator.js';
import { evaluateRule } from '../../evaluateRule.js';

describe('when evaluating empty with empty string', () => {
let result: boolean;
beforeEach(async () => {
const validator = new BaseValidator<{ value: unknown }>();
validator.ruleFor(model => model.value).empty();
result = await evaluateRule(validator.rules[0]!, '', { value: '' }, new AbortController().signal);
});
it('should accept the value', () => { result.should.equal(true); });
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) Cratis. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
import { BaseValidator } from '../../BaseValidator.js';
import { evaluateRule } from '../../evaluateRule.js';

describe('when evaluating empty with next line character', () => {
let result: boolean;
beforeEach(async () => {
const validator = new BaseValidator<{ value: unknown }>();
validator.ruleFor(model => model.value).empty();
result = await evaluateRule(validator.rules[0]!, '\u0085', { value: '\u0085' }, new AbortController().signal);
});
it('should accept the value', () => { result.should.equal(true); });
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) Cratis. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
import { BaseValidator } from '../../BaseValidator.js';
import { evaluateRule } from '../../evaluateRule.js';

describe('when evaluating empty with padded content', () => {
let result: boolean;
beforeEach(async () => {
const validator = new BaseValidator<{ value: unknown }>();
validator.ruleFor(model => model.value).empty();
result = await evaluateRule(validator.rules[0]!, ' a ', { value: ' a ' }, new AbortController().signal);
});
it('should reject the value', () => { result.should.equal(false); });
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) Cratis. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
import { BaseValidator } from '../../BaseValidator.js';
import { evaluateRule } from '../../evaluateRule.js';

describe('when evaluating empty with whitespace only', () => {
let result: boolean;
beforeEach(async () => {
const validator = new BaseValidator<{ value: unknown }>();
validator.ruleFor(model => model.value).empty();
result = await evaluateRule(validator.rules[0]!, ' \t\n', { value: ' \t\n' }, new AbortController().signal);
});
it('should accept the value', () => { result.should.equal(true); });
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) Cratis. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
import { BaseValidator } from '../../BaseValidator.js';
import { evaluateRule } from '../../evaluateRule.js';

describe('when evaluating notEmpty with byte order mark', () => {
let result: boolean;
beforeEach(async () => {
const validator = new BaseValidator<{ value: unknown }>();
validator.ruleFor(model => model.value).notEmpty();
result = await evaluateRule(validator.rules[0]!, '\uFEFF', { value: '\uFEFF' }, new AbortController().signal);
});
it('should accept the value', () => { result.should.equal(true); });
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) Cratis. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
import { BaseValidator } from '../../BaseValidator.js';
import { evaluateRule } from '../../evaluateRule.js';

describe('when evaluating notEmpty with empty string', () => {
let result: boolean;
beforeEach(async () => {
const validator = new BaseValidator<{ value: unknown }>();
validator.ruleFor(model => model.value).notEmpty();
result = await evaluateRule(validator.rules[0]!, '', { value: '' }, new AbortController().signal);
});
it('should reject the value', () => { result.should.equal(false); });
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) Cratis. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
import { BaseValidator } from '../../BaseValidator.js';
import { evaluateRule } from '../../evaluateRule.js';

describe('when evaluating notEmpty with next line character', () => {
let result: boolean;
beforeEach(async () => {
const validator = new BaseValidator<{ value: unknown }>();
validator.ruleFor(model => model.value).notEmpty();
result = await evaluateRule(validator.rules[0]!, '\u0085', { value: '\u0085' }, new AbortController().signal);
});
it('should reject the value', () => { result.should.equal(false); });
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) Cratis. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
import { BaseValidator } from '../../BaseValidator.js';
import { evaluateRule } from '../../evaluateRule.js';

describe('when evaluating notEmpty with padded content', () => {
let result: boolean;
beforeEach(async () => {
const validator = new BaseValidator<{ value: unknown }>();
validator.ruleFor(model => model.value).notEmpty();
result = await evaluateRule(validator.rules[0]!, ' a ', { value: ' a ' }, new AbortController().signal);
});
it('should accept the value', () => { result.should.equal(true); });
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) Cratis. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
import { BaseValidator } from '../../BaseValidator.js';
import { evaluateRule } from '../../evaluateRule.js';

describe('when evaluating notEmpty with whitespace only', () => {
let result: boolean;
beforeEach(async () => {
const validator = new BaseValidator<{ value: unknown }>();
validator.ruleFor(model => model.value).notEmpty();
result = await evaluateRule(validator.rules[0]!, ' \t\n', { value: ' \t\n' }, new AbortController().signal);
});
it('should reject the value', () => { result.should.equal(false); });
});
Loading