Skip to content

Commit

Permalink
Formats
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Dec 27, 2024
1 parent f687a49 commit 8329018
Show file tree
Hide file tree
Showing 19 changed files with 62 additions and 136 deletions.
14 changes: 4 additions & 10 deletions src/format/date-time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
The MIT License (MIT)
2020 Evgeny Poberezkin
2024 Haydn Paterson (sinclair) <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
Expand All @@ -30,19 +26,17 @@ THE SOFTWARE.

import { FormatRegistry } from '../type/index'

// ------------------------------------------------------------------
// This code ported form the ajv-format project for compatibility.
// All credit goes to Evgeny Poberezkin and contributors.
// ------------------------------------------------------------------

import { IsDate } from './date'
import { IsTime } from './time'

const DateTimeSeperator = /t|\s/i

/**
* Returns true of this string is a date-time
* @documentation https://datatracker.ietf.org/doc/html/rfc3339#section-5.6
* @spec https://datatracker.ietf.org/doc/html/rfc3339#section-5.6
* @see https://github.com/ajv-validator/ajv-formats
* @author Evgeny Poberezkin and contributors
* @license MIT
* @example `2020-12-12T20:20:40+00:00`
*/
export function IsDateTime(value: string, strictTimeZone?: boolean): boolean {
Expand Down
14 changes: 4 additions & 10 deletions src/format/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
The MIT License (MIT)
2020 Evgeny Poberezkin
2024 Haydn Paterson (sinclair) <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
Expand All @@ -30,11 +26,6 @@ THE SOFTWARE.

import { FormatRegistry } from '../type/index'

// --------------------------------------------------------------------------
// This code ported form the ajv-format project for compatibility. All credit
// goes to Evgeny Poberezkin and contributors.
// --------------------------------------------------------------------------

const Days = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
const Date = /^(\d\d\d\d)-(\d\d)-(\d\d)$/

Expand All @@ -43,7 +34,10 @@ function IsLeapYear(year: number): boolean {
}
/**
* Returns true of this string is a date
* @documentation https://datatracker.ietf.org/doc/html/rfc3339#section-5.6
* @spec https://datatracker.ietf.org/doc/html/rfc3339#section-5.6
* @see https://github.com/ajv-validator/ajv-formats
* @author Evgeny Poberezkin and contributors
* @license MIT
* @example `2020-12-12`
*/
export function IsDate(value: string): boolean {
Expand Down
16 changes: 5 additions & 11 deletions src/format/duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
The MIT License (MIT)
2020 Evgeny Poberezkin
2024 Haydn Paterson (sinclair) <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
Expand All @@ -30,16 +26,14 @@ THE SOFTWARE.

import { FormatRegistry } from '../type/index'

// ------------------------------------------------------------------
// This expression is borrowed the ajv-format project for compatibility.
// All credit goes to Evgeny Poberezkin and contributors.
// ------------------------------------------------------------------

const Duration = /^P(?!$)((\d+Y)?(\d+M)?(\d+D)?(T(?=\d)(\d+H)?(\d+M)?(\d+S)?)?|(\d+W)?)$/

/**
* `[Ajv-Formats]` Returns true if this string is a duration
* @documentation https://datatracker.ietf.org/doc/html/rfc3339#appendix-A
* Returns true if this string is a duration
* @spec https://datatracker.ietf.org/doc/html/rfc3339#appendix-A
* @see https://github.com/ajv-validator/ajv-formats
* @author Evgeny Poberezkin and contributors
* @license MIT
* @example `P3D`
*/
export function IsDuration(value: string): boolean {
Expand Down
14 changes: 4 additions & 10 deletions src/format/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
The MIT License (MIT)
2020 Evgeny Poberezkin
2024 Haydn Paterson (sinclair) <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
Expand All @@ -30,16 +26,14 @@ THE SOFTWARE.

import { FormatRegistry } from '../type/index'

// ------------------------------------------------------------------
// This expression is borrowed the ajv-format project for compatibility.
// All credit goes to Evgeny Poberezkin and contributors.
// ------------------------------------------------------------------

const Email = /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i

/**
* Returns true if this string is an email address.
* @documentation https://datatracker.ietf.org/doc/html/rfc5321#section-4.1.2
* @spec https://datatracker.ietf.org/doc/html/rfc5321#section-4.1.2
* @see https://github.com/ajv-validator/ajv-formats
* @author Evgeny Poberezkin and contributors
* @license MIT
* @example `[email protected]`
*/
export function IsEmail(value: string): boolean {
Expand Down
14 changes: 4 additions & 10 deletions src/format/hostname.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
The MIT License (MIT)
2020 Evgeny Poberezkin
2024 Haydn Paterson (sinclair) <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
Expand All @@ -30,16 +26,14 @@ THE SOFTWARE.

import { FormatRegistry } from '../type/index'

// ------------------------------------------------------------------
// This expression is borrowed the ajv-format project for compatibility.
// All credit goes to Evgeny Poberezkin and contributors.
// ------------------------------------------------------------------

const Hostname = /^(?=.{1,253}\.?$)[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[-0-9a-z]{0,61}[0-9a-z])?)*\.?$/i

/**
* Returns true if this string is a hostname
* @documentation https://datatracker.ietf.org/doc/html/rfc1123#section-2.1
* @spec https://datatracker.ietf.org/doc/html/rfc1123#section-2.1
* @see https://github.com/ajv-validator/ajv-formats
* @author Evgeny Poberezkin and contributors
* @license MIT
* @example `domain.com`
*/
export function IsHostname(value: string): boolean {
Expand Down
2 changes: 0 additions & 2 deletions src/format/idn-email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
The MIT License (MIT)
Copyright (c) 2017-2024 Haydn Paterson (sinclair) <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
Expand Down
2 changes: 0 additions & 2 deletions src/format/idn-hostname.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
The MIT License (MIT)
Copyright (c) 2017-2024 Haydn Paterson (sinclair) <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
Expand Down
18 changes: 6 additions & 12 deletions src/format/ipv4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
The MIT License (MIT)
2020 Evgeny Poberezkin
2024 Haydn Paterson (sinclair) <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
Expand All @@ -30,20 +26,18 @@ THE SOFTWARE.

import { FormatRegistry } from '../type/index'

// ------------------------------------------------------------------
// This expression is borrowed the ajv-format project for compatibility.
// All credit goes to Evgeny Poberezkin and contributors.
// ------------------------------------------------------------------

const pattern = /^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$/
const IpV4 = /^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$/

/**
* Returns true if this string is a ipv4 format.
* @documentation https://datatracker.ietf.org/doc/html/rfc2673#section-3.2
* @spec https://datatracker.ietf.org/doc/html/rfc2673#section-3.2
* @see https://github.com/ajv-validator/ajv-formats
* @author Evgeny Poberezkin and contributors
* @license MIT
* @example `127.0.0.1`
* ```
*/
export function IsIpv4(value: string): boolean {
return pattern.test(value)
return IpV4.test(value)
}
FormatRegistry.Set('ipv4', IsIpv4)
16 changes: 8 additions & 8 deletions src/format/ipv6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
The MIT License (MIT)
2020 Evgeny Poberezkin
2024 Haydn Paterson (sinclair) <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
Expand All @@ -31,18 +27,22 @@ THE SOFTWARE.
import { FormatRegistry } from '../type/index'

// ------------------------------------------------------------------
// This expression is borrowed the ajv-format project for compatibility.
// This expression is borrowed the ajv-format project for compatibility.
// All credit goes to Evgeny Poberezkin and contributors.
// ------------------------------------------------------------------

const pattern = /^((([0-9a-f]{1,4}:){7}([0-9a-f]{1,4}|:))|(([0-9a-f]{1,4}:){6}(:[0-9a-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9a-f]{1,4}:){5}(((:[0-9a-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9a-f]{1,4}:){4}(((:[0-9a-f]{1,4}){1,3})|((:[0-9a-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){3}(((:[0-9a-f]{1,4}){1,4})|((:[0-9a-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){2}(((:[0-9a-f]{1,4}){1,5})|((:[0-9a-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){1}(((:[0-9a-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|[1-9]?\d)){3}))|:))|(:(((:[0-9a-f]{1,4}){1,7})|((:[0-9a-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))$/i

/**
* Returns true if this string is a Ipv6 format.
* @documentation https://datatracker.ietf.org/doc/html/rfc2373#section-2.2
* @spec https://datatracker.ietf.org/doc/html/rfc2373#section-2.2
* @see https://github.com/ajv-validator/ajv-formats
* @author Evgeny Poberezkin and contributors
* @license MIT
* @example `::1`
*/
export function IsIpv6(value: string): boolean {
return pattern.test(value)
return Ipv6.test(value)
}
FormatRegistry.Set('ipv6', IsIpv6)
2 changes: 0 additions & 2 deletions src/format/iri-reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
The MIT License (MIT)
Copyright (c) 2017-2024 Haydn Paterson (sinclair) <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
Expand Down
2 changes: 0 additions & 2 deletions src/format/iri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
The MIT License (MIT)
Copyright (c) 2017-2024 Haydn Paterson (sinclair) <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
Expand Down
14 changes: 4 additions & 10 deletions src/format/json-pointer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
The MIT License (MIT)
2020 Evgeny Poberezkin
2024 Haydn Paterson (sinclair) <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
Expand All @@ -30,16 +26,14 @@ THE SOFTWARE.

import { FormatRegistry } from '../type/index'

// ------------------------------------------------------------------
// This expression is borrowed the ajv-format project for compatibility.
// All credit goes to Evgeny Poberezkin and contributors.
// ------------------------------------------------------------------

const JsonPointer = /^(?:\/(?:[^~/]|~0|~1)*)*$/

/**
* Returns true if this string is a json-pointer
* @documentation https://datatracker.ietf.org/doc/html/rfc6901
* @spec https://datatracker.ietf.org/doc/html/rfc6901
* @see https://github.com/ajv-validator/ajv-formats
* @author Evgeny Poberezkin and contributors
* @license MIT
* @example `/values/0/value`
*/
export function IsJsonPointer(value: string): boolean {
Expand Down
4 changes: 0 additions & 4 deletions src/format/regex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
The MIT License (MIT)
2020 Evgeny Poberezkin
2024 Haydn Paterson (sinclair) <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
Expand Down
9 changes: 4 additions & 5 deletions src/format/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
The MIT License (MIT)
2020 Evgeny Poberezkin
2024 Haydn Paterson (sinclair) <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
Expand Down Expand Up @@ -39,7 +35,10 @@ const TIME = /^(\d\d):(\d\d):(\d\d(?:\.\d+)?)(z|([+-])(\d\d)(?::?(\d\d))?)?$/i

/**
* Returns true of this string is a iso time.
* @documentation https://datatracker.ietf.org/doc/html/rfc3339#section-5.6
* @spec https://datatracker.ietf.org/doc/html/rfc3339#section-5.6
* @see https://github.com/ajv-validator/ajv-formats
* @author Evgeny Poberezkin and contributors
* @license MIT
* @example `20:20:39+00:00`
*/
export function IsTime(value: string, strictTimeZone?: boolean): boolean {
Expand Down
11 changes: 5 additions & 6 deletions src/format/uri-reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
The MIT License (MIT)
2020 Evgeny Poberezkin
2024 Haydn Paterson (sinclair) <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
Expand All @@ -31,15 +27,18 @@ THE SOFTWARE.
import { FormatRegistry } from '../type/index'

// ------------------------------------------------------------------
// This expression is borrowed the ajv-format project for compatibility.
// This expression is borrowed the ajv-format project for compatibility.
// All credit goes to Evgeny Poberezkin and contributors.
// ------------------------------------------------------------------

const URI_REFERENCE = /^(?:(?:[a-z][a-z0-9+\-.]*:)?\/?\/)?(?:[^\\\s#][^\s#]*)?(?:#[^\\\s]*)?$/i

/**
* Returns true if this string is a uri-reference
* @documentation https://datatracker.ietf.org/doc/html/rfc3986#section-4.1
* @spec https://datatracker.ietf.org/doc/html/rfc3986#section-4.1
* @see https://github.com/ajv-validator/ajv-formats
* @author Evgeny Poberezkin and contributors
* @license MIT
* @example `/path/to/resource`
*/
export function IsUriReference(value: string): boolean {
Expand Down
Loading

0 comments on commit 8329018

Please sign in to comment.